Hi everyone,
I am trying to send via serial the state of some buttons from the Arduino to Processing.
What I get in the serial monitor in Arduino and in Processing is correct, but I am not able to use the information coming from Arduino in Processing. Let me show the code and explain further more below:
Arduino code:
// constants won't change. They're used here to
// set pin numbers:
int buttonPins[] = {
2,4,7,8,12}
; // the number of the pushbutton pin
int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonStates[5]; // variable for reading the pushbutton status
int i;
void setup() {
// initialize the pushbutton pin as an input:
for (i=0; i<5; i++){
pinMode(buttonPins[i], INPUT);
}
Serial.begin(9600);
}
void loop(){
for (int i=0; i<5; i++){
// read the state of the pushbutton value:
buttonStates[i] = digitalRead(buttonPins[i]);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonStates[i] == HIGH) {
Serial.print("ON");
Serial.println(buttonPins[i]);
Serial.print ("VAL");
Serial.println (buttonStates[i]);
Serial.write (buttonStates[i]);
}
else {
// do nothing
}
}
delay(5000); // just for having the time to check
}
Processing code:
// Example by Tom Igoe
import processing.serial.*;
Serial myPort; // The serial port
void setup() {
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
// Expand array size to the number of bytes you expect
byte[] inBuffer = new byte[5];
while (myPort.available() > 0) {
inBuffer = myPort.readBytes();
myPort.readBytes(inBuffer);
if (inBuffer != null) {
String myString = new String(inBuffer);
println(myString);
}
}
}
That's the example code in Processing. I tried a // if string == "bla bla bla" // but it doesn't work and it's also quite not good I think to go for that (easy to make mistakes).
What I need is to understand
- if I am sending the data from the Arduino correctly, and if not how should I send it to Processing
- how to use the data in Processing. Basically something like being able to define a
buttonStates in Processing and saying
if buttonStates[0] == 1 > do A
if buttonStates[1] == 1 > do B
*if buttonStates[2] == 1 > do Z *
Thank you so much for the help!
Cheers