If statement between two codes!

Hello, I have a project that I made and the first code is like controlling a servo manually and the 2nd code is to control a servo automatically, now the problem is that I have tried to combine both codes into one code and make an if statement to choose if I want to control the servo manually or automatically.

I tried to input a key like this

void keyPressed() {
   if (key = '1') {
  mode = key ;
} 
else if (key = '2') {
  mode = key ;
}
}

and I put this code right after void draw

    if (mode = '1'){*the manual control code}
    else if(mode = '2'){*the auto control code*}t

I do not know how to convert this if statement to GUI too as I do need this too.

I just need to know where should I put the if statement (if statement is easier than button GUI I think..)

PS: my main project is sentry gun and I can control it manually via mouse or automatically via camera (tracking).

I hope you guys get this.

Thank You

Tagged:

Answers

  • Answer ✓

    Format your code for the forums. Select it and press the C button above the text entry box.

    The error you are getting seems to indicate that you have mis-matched braces - maybe void draw should be void draw(){ ? Press Ctrl-t in the Processing editor to format your code.

  • here it is but it wont work without an arduino.. this is the 2 codes (inside if (mode=...etc) statement )I want to make a key to chose between them

    ps: I chose your answer as "Accepted Answer" I thought it was making me allow your question to be posted here.. ^^

    import processing.video.*;
    import processing.serial.*;  
    
    Capture video;
    int xpos=0; 
    int ypos=0;
    Serial port; 
    PImage prevFrame;
    
    float threshold = 100;
    int Mx = 0;
    int My = 0;
    int ave = 0;
    
    int ballX = width*8;
    int ballY = height*8;
    int rsp = 5;
    void setup() {
      size(320, 240);
      video = new Capture(this, width, height, 30);
      video.start();
      prevFrame = createImage(video.width, video.height, 255);
      println(Serial.list()); 
      port = new Serial(this, "COM4", 19200);
    }
    
    void draw() {
    
      if (mode='1') {
        if (video.available()) {
    
          prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); 
          prevFrame.updatePixels();
          video.read();
          image(video, 0, 0, 360, 360);
          update(Mx, My);
    
          cursor(CROSS);
        }
    
        loadPixels();
        video.loadPixels();
        prevFrame.loadPixels();
    
        Mx = 0;
        My = 0;
        ave = 0;
    
    
        for (int x = 0; x < video.width; x ++ ) {
          for (int y = 0; y < video.height; y ++ ) {
    
            int loc = x + y*video.width; 
            color current = video.pixels[loc];      
            color previous = prevFrame.pixels[loc]; 
    
    
            float r1 = red(current); 
            float g1 = green(current);
            float b1 = blue(current);
            float r2 = red(previous);
            float g2 = green(previous); 
            float b2 = blue(previous);
            float diff = dist(r1, g1, b1, r2, g2, b2);
    
    
            if (diff > threshold) { 
              pixels[loc] = video.pixels[loc];
              Mx += x;
              My += y;
              ave++;
            } 
            else {
    
              pixels[loc] = video.pixels[loc];
            }
          }
        }
        rect(0, 0, width, height);
        if (ave != 0) { 
          Mx = Mx/ave;
          My = My/ave;
        }
        if (Mx > ballX + rsp && Mx > 50) {
          ballX+= rsp;
        }
        else if (Mx < ballX - rsp && Mx > 50) {
          ballX-= rsp;
        }
        if (My > ballY + rsp && My > 50) {
          ballY+= rsp;
        }
        else if (My < ballY - rsp && My > 50) {
          ballY-= rsp;
        }
    
        updatePixels();
        strokeWeight(1.0);
        stroke(0);
        fill(99, 255, 99, 90);
        ellipse(ballX, ballY, 15, 15);
    
    
    
        void update(int Mx, int My)
        {
          xpos= Mx/2;
          ypos = My/2;
    
          port.write(xpos+"Mx");
          port.write(ypos+"My");
        }
      }
    
      else if (mode = '2') {
        fill(175);
        rect(0, 0, 360, 360);
        fill(255, 255, 255);
        rect(180, 175, mouseX-180, 10);
        fill(0, 255, 255);
        rect(180, 180, 10, mouseY-180, 10);
        update(mouseX, mouseY);
      }
    
      void update(int x, int y)
      {
        xpos= x/2;
        ypos = y/2;
    
        port.write(xpos+"x");
        port.write(ypos+"y");
      }
    }
    
  • edited April 2014

    I have edited the void update() to this and put it in the end outside of the if statement ps:I replaced all Mx and My to x and y

        void update(int x, int y)
        {
          xpos= x/2;
          ypos = y/2;
    
          port.write(xpos+"x");
          port.write(ypos+"y");
        }
    
  • = is assignment. == is comparison test.

  • sorry I did not get you? I am sort of a beginner in processing

  • edited April 2014

    O.O it worked! thank you so much GoToLoop and TfGuy44! you saved me "Equality operator == is not the same as assignment operator"

Sign In or Register to comment.