I'm now working on how to communicate arduino with processing in both way. means the processing can read value from arduino to trigger certain animation, and the arduino can trigger some events ( like led on) when the animation runs in a certain point.
I wrote two quick sketches in arduino and processing. it is a snake goes from left to the right side of the screen. when it hits the right side, it will stop, and the led of arduino will be on for a certain period (3 seconds). after that, the snake goes from left to right again, and the led should be off for the next round.
the problem of my sketch is, the led will light up once, but never turns off again. i tried everything I could yet it still wont work.
does anyone have the similar problem or can anyone help me out ?
i'm really appreciated!
here is the code in arduino int signal2 = '1'; int signal3 = '0'; void setup() {
** Serial.begin(9600);**
** pinMode(13, OUTPUT);**
** Serial.write(signal3);** } void loop() {
** byte check;**
** if (Serial.available()>0) {**
** check = Serial.read();**
** if(check == '0'){**
** digitalWrite(13, HIGH);**
** delay(3000); // wait for a second**
** Serial.write(signal2);**
** }**
** else {**
** digitalWrite(13, LOW);**
** Serial.write(signal3);**
** }**
** }** }
for(int i=0; i<nummover; i++) {
mover=new Mover();
}
This is why the code buttons exists. Either the forum software or you screwed up this code. I'll assume that it was you posting the code incorrectly rather than you screwing up the code.
But, why do you have a one element array?
Serial data transfer is asynchronous. That means that you can never tell when it will happen. Yet, in the processing sketch, you assume that there will be data to read on every pass through draw().
You need to implement the serialEvent method, to handle serial data coming in. You'll need to figure out a way (global variables, typically) to make draw() and serialEvent() share data.
thank you paul!
the mover = new Mover was a mistake when i paste to the forum. i have . i just set one becoz it's more clean for the first test. i actually will have more of this object. I just tried with serialEvent as you suggested. but the problem is still there. here is my modified code in processing. I'll skip the mover class import processing.opengl.*; import processing.serial.*; Serial myPort; String signal; Mover[] mover; int nummover = 7; int flag; void setup() {
frameRate(30);*
size(1000,1000,OPENGL);*
smooth();*
background(0);*
println(Serial.list());*
myPort = new Serial(this, Serial.list()[0], 9600);*
the mover = new Mover was a mistake when i paste to the forum
So you repeated it?
Modify your posts. Select the code, and press the button with the # icon on it.
If you didn't change your Arduino code, your serialEvent() method is never called. It won't be called until the Arduino sends a carriage return, which it currently does not.
hi paul. I looked through the communication example in arduino. i did modified the code in arduino, yet it still stuck in the middle. It would be really helpful if u could tell me what kind of problem it is. i assume it might be the logic in processing code.
hello paul, thanks for your replying again. however it is still not working. I change the Serial.println to Serial. write in arduino, becaus it keeps sending weird numbers.
I think the problem here is the arduino always read '1' from processing. I test them separately they work fine. the processing sending the correct number, and if i type in serial monitor in arduino, it response well too.
That would require that I do a lot of work that I shouldn't need to do. You have the hardware setup with the sketches uploaded.
Just show me the latest code on the Arduino, the latest Processing sketch, and the output from the println() commands in Processing.
What is possibly causing an issue is that the Arduino is sending '1' as an int as a string, which means that it is sending the string "49". The string "49" is then being converted to an int, as 49, which, of course doesn't look like 1.
I dont think the problem is '1' or 1. I changed the value like u said, however it is still not working.
the processing is printing 0 when it hits the boundary and 3 when it is not. the problem is the arduino just cannot receive the 3 sent from processing....
I copied your processing sketch to the Processing IDE.
I copied your Arduino code to to the Arduino IDE. I compiled and linked it for my Mega, since that was handy. I uploaded it.
I started the Processing application, and watched the Processing application receive nothing from the Arduino.
I tried making a bunch of changes to both apps. No luck getting the Mega to do anything other than light the LED on pin 13 regardless of what was sent.
I unplugged the Mega, and plugged a Duemilanove in, and removed all of my changes to the Arduino code. I kept the change to the Processing code, that showed that the serialEvent method had been called, and everything worked.
The snakes stop moving when they hit the right wall, and start over.
Why this works for me on a Duemilanove, and not for you, and why is does not work for me on a Mega is a mystery.
you mean, you use the code i posted and it works ?
What I want to achieve is, when the snake hit the edge, it will restart from left side, and hold for 3 seconds, during which the led will be on. and then the LED will be off until the snake hits the edge again