Let's Make Robots!

Arduino Serial to Processing

Ok, thanks for helping me with my last question, but now I have another one...

I am currently trying to make a robot with a tactile switch at the front. When that tactile switch is pushed down the robot reverses and turns around. Here is the cool part though, when the switch is pushed down it will also send data to processing, causing a face to jitter onscreen. So far I have been able to make the code for the face jitter (except on mouse over) Here is the code if anybody is curious:

void setup(){
 size(400, 400);
}
void draw() {
background (255);
if (mouseX >100 && mouseX < 300 && mouseY >= 100 && mouseY <= 300)
translate(random(-5, 5),random(-5,5));
fill(0);          
 rect(100,100,200,200);
 fill(255);
 rect (140, 120, 45, 45);
 rect (220, 120, 45, 45);
 fill(255,0,0);
 rect (140, 250, 120, 30);
}

Anyways, now I am trying to get the face to jitter when a button is pushed down on the arduino.
Here is the processing code for it:

//A face that jitters when a switch on a robot is on.
Serial myPort;
void setup(){
 printIn(Serial.list());
 myPort = new Serial(this, Serial.list()[3], 9600);
  size(400, 400);

}
void draw() {

background (255);
fill(0);         
 rect(100,100,200,200);
 fill(255);
 rect (140, 120, 45, 45);
 rect (220, 120, 45, 45);
 fill(255);
 rect (140, 250, 120, 30);
while (myPort.available() > 0) {
 int inByte = myPort.read();
if inByte = 'A'{
translate(random(-5, 5),random(-5,5));
}
}
}

With this code I keep getting the error expecting LPAREN found inByte. And now the arduino code to match that:


const int buttonPin = 12;
int buttonState = 0;
void setup()
 {
   Serial.begin(9600);
pinMode(buttonPin, INPUT);
 }
 void loop() {
   buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH) {
     serialWrite(66);
   }
 
 }
With this code I get multiple errors including "couldn't determine program size" and "undefined refrence to serialWrite"

Sorry for the super long post/ confusing writing.
 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I'm sorry if I wasn't clear..

Did you try setting the com port to 1, or 2?

In my Arduino IDE, my two arduinos come up as COM3, and COM4. But you do NOT use those names in the Processing IDE.   When you're using the Serial.list, you're accessing a list of the Serial ports available. Starting with 0, and counting up.

The use of [  and   ]   accesses an array.

 

 If you have nothing else connected to your computer (well, chances are your mouse or keyboard still count if they're using USB), then it should be COM1, or COM0 So, would look like this:

 myPort = new Serial(this, Serial.list()[0], 9600);

or

 myPort = new Serial(this, Serial.list()[1], 9600);

 the 0 connects to my Mouse, since my mouse is the first USB connection. (doesn't receive any actual information)  and the 1 connects to my Arduino. If I have Two arduinos connected, it's either 1, or 2 respectively.

 

Well, I am using a laptop so I don't believe that the keyboard or mouse use any ports.  Anyway, thanks for clearing that up I think i understand it now. And when the program uploads it says [0] "COM1" [1] "COM3". This makes me think that 1 is the right number as COM3 is my arduino. However I can't tell because the face doesn't move. I am now pretty sure that it is something to do with the drawing/animation. I will try to do some research on it, and if I can't figure it out I will ask the processing forum.

Just for a heads up, I tried the sketch just a few minutes ago.. I got it to work, but I had to up the Translate random values to about -20 and 20 each.. and it's not really a twitter, it doesn't move the mouth itself, but actually puts another box up, and it disappears just as fast as it came in.

Indeed, you may not have any other items connected.. so a simple way to test that is just use the Processing Example under File->Examples->Libraries->Serial  and choose the Serial read.  It has the Arduino code at the bottom so you can just upload it, this way you can test the number to see which com port you need to connect to. (The examples work without any adjustments, other than the COM port, so should be fairly easy)

But yeah, I'm no good in processing yet, so I'm afraid I'm no help in terms of programming! (other than finding minor errors)

 

But yeah, in terms of using [ and ]   look at the Array section on Arduino, will kind of explain it to you:

http://arduino.cc/en/Tutorial/Array

Ok, so I tried the simple read program and it worked fine with [1]. However when I use [1] with my program nothing happens. Even when I adjust the values of translate to -20 and 20. I don't even get the box flash like you were talking about. I will go ask the processing forum to see if they can help me with the drawing/animation.