How in processing can I get it to choose one of two different codes for webcam?

edited February 3 in Library Questions

**Hello, so i'm fairly new to processing and wanted to incorporate more than one function when the webcam is on. I have some examples of code I'll use but I wondered if there is a way for processing to randomly choose whether to use one example or the next? I've tried looking at functions but I don't seem to be getting anywhere. **

// Example one 

import processing.video.*; 
Capture video;

PImage img1;
int w=640, h=480;

boolean bright = true;
boolean greyScale;
int shiftAmount = 4;
int grid = 1;
void setup() {
  size(640, 480);
  video = new Capture(this, 640, 480); 
  video.start();
}

void draw() { 
  loadPixels(); // Fills pixelarray
  float mouseMap = (int) map(mouseX, 0, width, 0, 255*3); // Brightness threshold mapped to mouse coordinates

if(shiftAmount > 45 || shiftAmount < 0){shiftAmount = 0;};

  for (int y = 0; y< h; y++)
  {
    for (int x = 0; x< w; x++)
    {
      color c = video.pixels[y*video.width+x]; 

      int a = (c >> 150) & 0xFF;
      int r = (c >> 12) & 0xFF;  
      int g = (c >> 2) & 0xFF;  
      int b = c & 0xFF; 

      if (y %grid == 0) {

        if (bright)
        {
          if (r+g+b > mouseMap) {
            pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
          }
        }

        if (!bright)
        {
          if (r+g+b < mouseMap) {
            pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
          }
        }
      }
    }
  }
  updatePixels();

  if (greyScale) {
    filter(GRAY);
  }

  println("Shift amount: " + shiftAmount + " Frame rate: " + (int) frameRate + " Greyscale: " + greyScale) ;
}

void keyPressed()
// Keyboard controls
{
  switch(keyCode) {
  case UP:
    shiftAmount++;
    break;
  case DOWN:
    shiftAmount--;
    break;
  case LEFT:
    if (grid > 1) {
      grid--;
    }    
    break;
  case RIGHT:
    grid++;    
    break;
  case TAB:
    if (bright) {
      bright = false;
    }
    if (!bright) {
      bright = true;
    }
    break;
  case ENTER:
    if (!greyScale) {
      greyScale = true;
      break;
    }
    if (greyScale) {
      greyScale = false;
      break;
    }
  }
}

void captureEvent(Capture c) { 
  c.read();
} 


// Example Two

import processing.video.*;

Capture cam;

void setup() {
  size(640, 480);

  String[] cameras = Capture.list();
  cam = new Capture(this, width, height);
  cam.start();
  ellipseMode(CENTER);
}

void draw() {
  noStroke();
  background(255);
  for (int i = 0; i < width; i = i+20) {
    for (int j = 0; j < height; j = j+20) {
      fill(cam.get(i, j) * 4);
      ellipse(i, j, 20, 20);
    }
  }

  if (cam.available() == true) {
    cam.read();
  }
  //image(cam, 0, 0);
}

IF anyone would could help and tell me how I would merge both the codes so either one runs or the other each time I randomly load processing that would be a huge help!

Tagged:

Answers

  • int sketchToDo;
    
    GlobalVariables allSketchesGlobalVariables;
    
    void setup(){
      size(...);
      sketchToDo = int( random( numSketches ) );
      switch( sketchToDo ){
        case 0:
          setupSketch0(); // No size() calls!
          break;
        case 1:
          setupSketch1();
          break;
       }
    }
    
    void draw() {
      switch( sketchToDo ){
        case 0:
          drawSketch0();
          break;
        case 1:
          drawSketch1();
          break;
       }
    }
    
    void setupSketch0(){ ... }
    
    void setupSketch1(){ ... }
    
    void drawSketch0(){ ... }
    
    void drawSketch1(){ ... }
    
  • Thanks for the response, does this input look right? I'm getting a message about cannot find a class or type named "globalvariables"

    int sketchToDo;
    
    GlobalVariables allSketchesGlobalVariables;
    
    void setup(){
      size(640, 480);
      sketchToDo = int( random( numSketches ) );
      switch( sketchToDo ){
        case 0:
          setupSketch0(); // No size() calls!
          break;
        case 1:
          setupSketch1();
          break;
       }
    }
    
    void draw() {
      switch( sketchToDo ){
        case 0:
          drawSketch0();
          break;
        case 1:
          drawSketch1();
          break;
       }
    }
    
    void setupSketch0()
    {   size(640, 480);
    
      String[] cameras = Capture.list();
      cam = new Capture(this, width, height);
      cam.start();
      ellipseMode(CENTER);
    }
    
    void setupSketch1()
    {
      size(640, 480);
      video = new Capture(this, 640, 480); 
      video.start();
    }
    
    
    void drawSketch0()
    {
      noStroke();
      background(255);
      for (int i = 0; i < width; i = i+20) {
        for (int j = 0; j < height; j = j+20) {
          fill(cam.get(i, j) * 4);
          ellipse(i, j, 20, 20);
        }
      }
    
      if (cam.available() == true) {
        cam.read();
      }
      //image(cam, 0, 0);
    }
    
    void drawSketch1()
    { 
      loadPixels(); // Fills pixelarray
      float mouseMap = (int) map(mouseX, 0, width, 0, 255*3); // Brightness threshold mapped to mouse coordinates
    
    if(shiftAmount > 45 || shiftAmount < 0){shiftAmount = 0;};
    
      for (int y = 0; y< h; y++)
      {
        for (int x = 0; x< w; x++)
        {
          color c = video.pixels[y*video.width+x]; 
    
          int a = (c >> 150) & 0xFF;
          int r = (c >> 12) & 0xFF;  
          int g = (c >> 2) & 0xFF;  
          int b = c & 0xFF; 
    
          if (y %grid == 0) {
    
            if (bright)
            {
              if (r+g+b > mouseMap) {
                pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
              }
            }
    
            if (!bright)
            {
              if (r+g+b < mouseMap) {
                pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
              }
            }
          }
        }
      }
      updatePixels();
    
      if (greyScale) {
        filter(GRAY);
      }
    
      println("Shift amount: " + shiftAmount + " Frame rate: " + (int) frameRate + " Greyscale: " + greyScale) ;
    }
    
    void keyPressed()
    // Keyboard controls
    {
      switch(keyCode) {
      case UP:
        shiftAmount++;
        break;
      case DOWN:
        shiftAmount--;
        break;
      case LEFT:
        if (grid > 1) {
          grid--;
        }    
        break;
      case RIGHT:
        grid++;    
        break;
      case TAB:
        if (bright) {
          bright = false;
        }
        if (!bright) {
          bright = true;
        }
        break;
      case ENTER:
        if (!greyScale) {
          greyScale = true;
          break;
        }
        if (greyScale) {
          greyScale = false;
          break;
        }
      }
    }
    
    void captureEvent(Capture c) { 
      c.read();
    }
    
  • Answer ✓

    Here I made some minimum changes so it can run.

    Kf

    import processing.video.*; 
    
    final int numSketches=2;
    int w=640;
    int h=480;
    
    int sketchToDo;
    Capture cam;
    
    boolean bright = true;
    boolean greyScale;
    int shiftAmount = 4;
    int grid = 1;
    
    //GlobalVariables allSketchesGlobalVariables;
    
    void setup() {
      size(640, 480);
      sketchToDo = int( random( numSketches ) );
      switch( sketchToDo ) {
      case 0:
        setupSketch0(); // No size() calls!
        break;
      case 1:
        setupSketch1();
        break;
      }
    }
    
    void draw() {
      switch( sketchToDo ) {
      case 0:
        drawSketch0();
        break;
      case 1:
        drawSketch1();
        break;
      }
    }
    
    void setupSketch0()
    {   
      size(640, 480);
    
      String[] cameras = Capture.list();
      cam = new Capture(this, width, height);
      cam.start();
      ellipseMode(CENTER);
    }
    
    
    
    void setupSketch1()
    {
      size(640, 480);
      cam = new Capture(this, w, h); 
      cam.start();
    }
    
    
    void drawSketch0()
    {
      noStroke();
      background(255);
      for (int i = 0; i < width; i = i+20) {
        for (int j = 0; j < height; j = j+20) {
          fill(cam.get(i, j) * 4);
          ellipse(i, j, 20, 20);
        }
      }
    
      if (cam.available() == true) {
        cam.read();
      }
      //image(cam, 0, 0);
    }
    
    void drawSketch1()
    { 
      loadPixels(); // Fills pixelarray
      float mouseMap = (int) map(mouseX, 0, width, 0, 255*3); // Brightness threshold mapped to mouse coordinates
    
      if (shiftAmount > 45 || shiftAmount < 0) {
        shiftAmount = 0;
      };
    
      for (int y = 0; y< h; y++)
      {
        for (int x = 0; x< w; x++)
        {
          color c = cam.pixels[y*cam.width+x]; 
    
          int a = (c >> 150) & 0xFF;
          int r = (c >> 12) & 0xFF;  
          int g = (c >> 2) & 0xFF;  
          int b = c & 0xFF; 
    
          if (y %grid == 0) {
    
            if (bright)
            {
              if (r+g+b > mouseMap) {
                pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
              }
            }
    
            if (!bright)
            {
              if (r+g+b < mouseMap) {
                pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
              }
            }
          }
        }
      }
      updatePixels();
    
      if (greyScale) {
        filter(GRAY);
      }
    
      println("Shift amount: " + shiftAmount + " Frame rate: " + (int) frameRate + " Greyscale: " + greyScale) ;
    }
    
    void keyPressed()
      // Keyboard controls
    {
      if (key=='1')
        sketchToDo=0;
    
      if (key=='2')
        sketchToDo=1;
    
    
      switch(keyCode) {
      case UP:
        shiftAmount++;
        break;
      case DOWN:
        shiftAmount--;
        break;
      case LEFT:
        if (grid > 1) {
          grid--;
        }    
        break;
      case RIGHT:
        grid++;    
        break;
      case TAB:
        if (bright) {
          bright = false;
        }
        if (!bright) {
          bright = true;
        }
        break;
      case ENTER:
        if (!greyScale) {
          greyScale = true;
          break;
        }
        if (greyScale) {
          greyScale = false;
          break;
        }
      }
    }
    
    void captureEvent(Capture c) { 
      c.read();
    }
    
  • thank you so much really appreciate it!

  • Answer ✓
    // No size calls!
    

    This means you sould remove the calls to size() from setupSketch0() and setupSketch1(). You should only have the one size() function call in the real setup().

    GlobalVariables allSketchesGlobalVariables
    

    This was supposed to inform you that you should put all the global variables from all the sketches in at the global level - and remove this placeholder, of course.

Sign In or Register to comment.