Select images with Pushbutton

edited September 2014 in Arduino

Hello everyone,

I'm working on a project where I have to display 2 images on my processing window. I work with Arduino Uno , 2 pushbuttons and a led. On processing, I chose 2 images of the same size (250.425) which separate my window (540.425) into 2 equal parts. I have to choose one of my 2 images, throught 2 pushbuttons which are on my Arduino.

Ideally, when i push the first one, my window will display the image which is on the left, at the window size (540*425). In the same way, when i push the second one, my window will display the image which is on the right (at the window size).

Before, I worked on a project with a mouse and it was easier because there were a special function for it (mouseButton). Does somebody know if there is something special for the use of a pushButton ?

I added a led on my electric circuit to check that it detects the push. No worry for the 2 pushbuttons, it works. Finally, I succeeded in displaying the 2 images on the window but i don't succeed in detecting the impact of the push (apart with the led). I tried something with the function buttonPressed but it doesn't work. Could somebody tell me where I'm mistaken ?

Thank you in advance for your answer!!

This is my code: import processing.serial.*;

Serial myPort; //L'objet qui gère le port série
int ETAT_BP; // variable d'état du bouton poussoir
int ETAT_BP1;
int APPUI;
PImage img;

void setup(){
  size(540,425);
  img=loadImage("quinze.jpg");
  image(img,0,0); // display the image in img at co-ordinate (0,0)
  image(img,0,0,250,425);
  img=loadImage("ecrannoir.jpg");
  image(img,250,0,40,425);
  img=loadImage("cinq.jpg");
  image(img,290,0,250,425);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0],9600);
  myPort.buffer(1);
  //size(200,500);
  //background(51);
}

void draw(){
}

void buttonPressed(){
  if( ETAT_BP == APPUI){
      img=loadImage("quinze.jpg");
      image(img,0,0,540,425);
      println("square 1");
      myPort.write(1);
    }

  if( ETAT_BP1 == APPUI){
    img=loadImage("cinq.jpg");
    println("square 2");
    myPort.write(2);
  }
}

1 photo

Comments

  • Look at using Processing's Arduino library along with Arduino's Firmata software. See http://playground.arduino.cc/Interfacing/Processing . The other way is to set up serial communication between the two, which is kind of a pain but puts you in touch with what's really going on.

  • Function buttonPressed() isn't a valid Processing's callback event I'm afraid.
    Have you read Processing's reference about keyPressed() callback event?
    http://processing.org/reference/keyPressed_.html

  • edited September 2014

    Thanks for your advice. I hadn't read the keyPressed callback. So, I adapted my code to this function, it's working but... with a keyboard not with a pushbutton. Do you know how to adapt my code for working with my pushbuttons ?

    This is my code updated:

        import processing.serial.*;
    
        Serial myPort; //L'objet qui gère le port série
        //int ETAT_BP; // variable d'état du bouton poussoir
        //int ETAT_BP1;
        int APPUI;
        PImage img;
    
        void setup(){
          size(540,425);
          img=loadImage("quinze.jpg");
          image(img,0,0); // display the image in img at co-ordinate (0,0)
          image(img,0,0,250,425);
          img=loadImage("ecrannoir.jpg");
          image(img,250,0,40,425);
          img=loadImage("cinq.jpg");
          image(img,290,0,250,425);
          println(Serial.list());
          myPort = new Serial(this, Serial.list()[0],9600);
          myPort.buffer(1);
          //size(200,500);
          //background(51);
        }
    
        void draw(){
          if(keyPressed){
            if(key == 'c'){
              img=loadImage("quinze.jpg");
              image(img,0,0,540,425);
              println("square 1");
              myPort.write(1);
            }
            if(key == 'b'){
              img=loadImage("cinq.jpg");
              image(img,0,0,540,425);
              println("square 2");
              myPort.write(2);
            }
            if(key == 'a'){
              img=loadImage("quinze.jpg");
              //image(img,0,0); // display the image in img at co-ordinate (0,0)
              image(img,0,0,250,425);
              img=loadImage("ecrannoir.jpg");
              image(img,250,0,40,425);
              img=loadImage("cinq.jpg");
              image(img,290,0,250,425);
            }
    

    And the results that I get:

    (with b pushed): 10

    (with c pushed): 11

  • edited September 2014

    There are some excellent examples of Processing<->Arduino serial communication in Processing's Examples, in the File menu. Look in Libraries>Serial. The SimpleRead example looks like just what you need.

  • (Don't call loadImage inside draw() - it's too slow. Load the images to 3 separate pimages in setup and then just display the correct one in draw())

  • Thanks for your reply. Indeed, the SimpleRead has helped me. When I work with just one pushbutton, no problem: i succeed in switching on the led (Arduino) and playing with the images (Processing). However, when I work with 2 pushbuttons, I don't succeed in switching on the led (for a push on the first or on the second pushbutton), so the push is not detected.. Does someone have an explanation ?

    This is my Arduino code without any problem (1 pusbutton):

    int switchPin1 = 4;                       // Switch connected to pin 4
    //int switchPin2 = 2;
    const int LED=3; //declaration constante de broche 
    
    void setup() {
    
      pinMode(LED, OUTPUT); //met la broche en sortie
    
      pinMode(switchPin1, INPUT);             // Set pin 0 as an input
      digitalWrite(switchPin1, HIGH) ; // activation du pullup de la broche en entrée
      /*
      pinMode(switchPin2, INPUT);             // Set pin 0 as an input
      digitalWrite(switchPin2, HIGH) ; // activation du pullup de la broche en entrée
      */
      Serial.begin(9600);                    // Start serial communication at 9600 bps
    }
    
    void loop() {
      if (digitalRead(switchPin1)) {  // If switch is ON,
        digitalWrite(LED,0); // allume la LED
        Serial.write(0);               // send 1 to Processing
      } else {                                // If the switch is not ON,
        digitalWrite(LED,1); // éteint la LED
        Serial.write(1);               // send 0 to Processing
      }
      /*
      if (digitalRead(switchPin2) == HIGH) {  // If switch is ON,
        digitalWrite(LED,0); // allume la LED
        Serial.write();               // send 1 to Processing
      } else {                               // If the switch is not ON,
        digitalWrite(LED,1); // éteint la LED
        Serial.write(1);               // send 0 to Processing
      }*/
      delay(100);                            // Wait 100 milliseconds
    }
    

    This is my Arduino code for 2 pusbuttons, it doesn't work:

    int switchPin1 = 4;                       // Switch connected to pin 4
    int switchPin2 = 2;
    const int LED=3; //declaration constante de broche 
    
    void setup() {
    
      pinMode(LED, OUTPUT); //met la broche en sortie
    
      pinMode(switchPin1, INPUT);             // Set pin 0 as an input
      digitalWrite(switchPin1, HIGH) ; // activation du pullup de la broche en entrée
    
      pinMode(switchPin2, INPUT);             // Set pin 0 as an input
      digitalWrite(switchPin2, HIGH) ; // activation du pullup de la broche en entrée
    
      Serial.begin(9600);                    // Start serial communication at 9600 bps
    }
    
    void loop() {
      if (digitalRead(switchPin1) || (digitalRead(switchPin2) == HIGH)) {  // If switch is ON,
        digitalWrite(LED,0); // allume la LED
        Serial.write(0);               // send 0 to Processing
      } else {                                // If the switch is not ON,
        digitalWrite(LED,1); // éteint la LED
        Serial.write(1);               // send 1 to Processing
      }
      /*
      if (digitalRead(switchPin2) == HIGH) {  // If switch is ON,
        digitalWrite(LED,0); // allume la LED
        Serial.write();               // send 1 to Processing
      } else {                               // If the switch is not ON,
        digitalWrite(LED,1); // éteint la LED
        Serial.write(1);               // send 0 to Processing
      }*/
      delay(100);                            // Wait 100 milliseconds
    }
    
  • edited September 2014

    It looks like you're sending a Serial.write(1) to Processing for both switches. Shouldn't it be Serial.write(2) for switchPin2? Then your Processing program can decide what to do with each.

  • edited September 2014

    I tried to change switchPin(2) to put Serial.write(2) but it doesn't work :-/

                void loop() {
                  if (digitalRead(switchPin1) == HIGH) {  // If switch is ON,
                    digitalWrite(LED,0); // allume la LED
                    Serial.write(0);               // send 0 to Processing
                  } else {                                // If the switch is not ON,
                    digitalWrite(LED,1); // éteint la LED
                    Serial.write(1);               // send 1 to Processing
                  }
                  if (digitalRead(switchPin2) == HIGH) {  // If switch is ON,
                    digitalWrite(LED,0); // allume la LED
                    Serial.write(3);               // send 1 to Processing
                  } else {                               // If the switch is not ON,
                    digitalWrite(LED,1); // éteint la LED
                    Serial.write(2);               // send 0 to Processing
                  }
                  delay(100);                            // Wait 100 milliseconds
                }
    
  • or modify "virtualcolormixer" or
    try this ( I can not try it now)

     if (digitalRead(switchPin1) == HIGH) {
            state = 1;
          } else {
            state = 0;
          }
          if (digitalRead(switchPin2) == HIGH) {  
            statee = 1;
          } else {
            statee = 0;
          }
          Serial.write(state);
          Serial.write(statee);
          delay(100);
    

    processing

        int[]val = new int[2];
        ----
          if ( myPort.available()>2 ) {  
            val[0]  =  myPort.read();  
            val[1] =  myPort.read();
        }
        if (val[0] == 0) { 
        //whatever you want
        }
        if (val[1] == 0) { 
        //whatever you want
        }
    
  • Yes, I did a if...else, and it works! Thanks !!

Sign In or Register to comment.