I have a project with my university, i have to create à quizz which you can chose the answer by using Arduino button. For the moment i can’t send the stat of my button to processing and i dont know why.
Arduino :
int bouton = 4;
void setup() {
pinMode(bouton, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(bouton) == HIGH) { // If switch is ON,
Serial.write(1);
Serial.println("oui"); // send 1 to Processing
} else { // If the switch is not ON,
Serial.write(0);
Serial.println("non"); // send 0 to Processing
}
Serial.println(bouton);
delay(100);
}
Processing :
import processing.serial.*;
int val;
Serial myPort; // Create object from Serial class
void setup()
{
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
printArray(Serial.list());
String myPortName = Serial.list()[0];
}
void draw()
{
if ( myPort.available() > 0) {
val = myPort.read();
}
if ( val == 1) {
fill(127, 0, 0);
} else {
fill(255, 200, 200);
}
}
Hi @Rayenblue, I can see that you started with the Processing example ‘SimpleRead’. This does work, I’ve just tried it, but there are ways that it can be messed up. You’ve added some println to see if the Arduino is working. There’s a conflict here because those prints are also going to Processing and confusing things. You want only the 1 and 0 to go to Processing. Once you’ve seen the Ard prog is working (you’re using the serial monitor?), please remove them all. (We’ll talk about how to have both later.)
Are you sure your Processing is connecting to the port? Don’t have the Serial Monitor running when you start your Processing sketch. Do you have the com port name correct? Suggest you use this line
myPort = new Serial(this, “COM13”, 9600);
instead of the serial.list[] method. Change the port name to same as in the Ard IDE.
But unfortunely that didn’t work , i removed Serial.print and try your methode on Process, can you explain me what do you mean when your say " Are you sure your Processing is connecting to the port?", becauce I sucessed to send text .
If you are in fact connecting you need a shape to fill() !
Your code is missing one.
You could also add:
val = myPort.read();
println(val);
This will show all the extra characters you are sending; you are sending '0' or else… the else is all the extra characters from your Serial.print() statements.
@Rayenblue Yes, from your IDE image COM3 is the correct name. I would use it direct. The list()[] method works but the list can change if any other devices are added/removed. Is your Arduino a real one? The cheap Chinese ones take a different COMn number in different USB sockets.
Have you added the print as @glv said? Is anything printing?
The Ard sketch only sends every 100 mS. If you press and release the button quickly it may happen within 100 mS and be ignored. I think for the quiz you will find it too slow to react. The 100 delay at the end of the Ard sketch is too big. Suggest you change it to 40 mS. You could go less but it must be greater than the loop delay of the Processing, and we don’t know that yet. Once you get it working I think we should alter the Ard sketch to send on change, and on a slow frequency.
Thank you @RicharDL, my arduino is not a fake and my COM is the good , i add @glv code ( thank you by the way) and it show me values. 40 ms was a very good call , thank you again.