Processing & Arduino Buttons

edited May 2015 in Arduino

Hello, I am relatively new to Processing and even newer in Arduino. I have two buttons on my Arduino board and want to be able to move left & right a rectangle in Processing.

My mini Processing sketch looks like this

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
int x=50;
void setup() 
{
  size(200, 200);

  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }

  background(255);             // Set background to white

if (val == 1) {
x = x-1;
}  

if (val ==2) {
x = x+1;
}
rect(x, 50, 100, 100);
}

and my Arduino sketch like this

int switchPin1 = 2;  // Switch connected to pin 4
int switchPin2 = 3;   // Switch connected to pin 4

void setup() {

  pinMode(switchPin1, INPUT);             // Set pin 0 as an input
  pinMode(switchPin2, INPUT); 
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin1) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
    if (digitalRead(switchPin2) == HIGH) {  // If switch is ON,
    Serial.write(2);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(3);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

It is working but I don't think that is the right way to do it since when I move to the right, it moves smoothly but when I try the x = x-1, it goes very slowly, can someone tell me what am I doing wrong?

Tagged:

Answers

  • this

    if (val == 1) {
    x = x-1;
    }  
    
    if (val ==2) {
    x = x+1;
    }
    

    better

    if (val == 1) {
    x = x-1;
    }  
    else if (val ==2) {
    x = x+1;
    }
    

    this

    if (digitalRead(switchPin1) == HIGH) {  // If switch is ON,
        Serial.write(1);               // send 1 to Processing
      } else {                               // If the switch is not ON,
        Serial.write(0);               // send 0 to Processing
      }
        if (digitalRead(switchPin2) == HIGH) {  // If switch is ON,
        Serial.write(2);               // send 1 to Processing
      } else {                               // If the switch is not ON,
        Serial.write(3); // ?????????              // send 0 to Processing
      }
      delay(100);             
    

    better

    if (digitalRead(switchPin1) == HIGH) {  // If switch is ON,
        Serial.write(1);               // send 1 to Processing
      } else if (digitalRead(switchPin2) == HIGH) {  // If switch is ON,
        Serial.write(2);               // send 2 to Processing
      } else {                               // If the switches are not ON,
        Serial.write(0);               // send 0 to Processing
      }
      delay(2);          // !!!!!!!!!!!!!!!!!!!!!!!!
    
  • I am afraid that only makes things worse! :(

  • sorry...

    I don't know....

    why don't include all in the if clause?

    void draw()
    {
      if ( myPort.available() > 0) {  // If data is available,
            val = myPort.read();         // read it and store it in val
    
    
          background(255);             // Set background to white
    
        if (val == 1) {
        x = x-1;
        }  
    
        if (val ==2) {
        x = x+1;
        }
        rect(x, 50, 100, 100);
        }
    }
    
  • with the delay (100); You send only one "write (2)" and various "write (3) (else the first button) or eliminate the "else", or enter two delay, to make the first and second button with equal time

    void loop() {
      if (digitalRead(switchPin1) == HIGH) {  // If switch is ON,
        Serial.write(1);               // send 1 to Processing
      } else {                               // If the switch is not ON,
        Serial.write(0);               // send 0 to Processing
      }
      delay(100);
        if (digitalRead(switchPin2) == HIGH) {  // If switch is ON,
        Serial.write(2);               // send 1 to Processing
      } else {                               // If the switch is not ON,
        Serial.write(3);               // send 0 to Processing
      }
     delay(100); 
    }
    
  • Thanks for your reply, I ended up using the Serial Call Response example from the Arduino communication examples!

Sign In or Register to comment.