We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Arduino to Processing Problems
Page Index Toggle Pages: 1
Arduino to Processing Problems (Read 1071 times)
Arduino to Processing Problems
Sep 4th, 2009, 5:05pm
 
Ok, I am trying to get a face to jitter when a button is pressed on the arduino. Unfortunately I am encountering some problems.
Here is my Arduino code:
Code:
const int buttonPin = 12;
int buttonState = 0;
void setup()
{
  Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Serial.write(66); }else{
    Serial.write(97);    
  }
 
}

I do not believe there is any problem with this code or the circuit because when I open the Arduino serial monitor I get lowercase a's and when I press the button these a's are replaced with capital B's.
And here is the processing code for that Arduino sketch:
Code:
import processing.serial.*;
Serial myPort;
int inByte;
void setup(){
 size(400, 400);
 String portName = Serial.list()[2];
 myPort = new Serial(this, portName, 9600);

}
void draw() {
 fill(0);        
rect(100,100,200,200);
fill(255);
rect (140, 120, 45, 45);
rect (220, 120, 45, 45);
rect (140, 250, 120, 30);
fill(255);
if (myPort.available() > 0) {
inByte = myPort.read();
if (inByte == 'B') {
translate(random(-20, 20),random(-20,20));
}
}
}

This code runs fine except when a button is pressed on the arduino the face does not do anything. I know it is not a problem with the serial port because I was able to get the simple read program from the library working. This leads me to think it may be something with drawing/animation, but I'm not sure. Anybody have any suggestions?
Re: Arduino to Processing Problems
Reply #1 - Sep 22nd, 2009, 5:22pm
 
Some suggestions:

1) Try sending data on the line _only_ when the button is pressed - that way the line is not flooded with data.

2) Try changing the Processing sketch to read from the serial port in a callback instead of inside the draw() method:
Code:
void serialEvent(Serial p) { 
  inByte = myPort.read();
}

That way drawing the screen and reading the serial port is discoupled.

3) In your Processing sketch, add another trigger for the event so that you can determine if it works without the Arduino. E.g. set up a key handler that toggles the value of inByte when you press a certain (computer keyboard) key.
Page Index Toggle Pages: 1