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.
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.

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

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.

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.

 

I'm afraid i'm a bit confused. :P. Currently I don't have anything plugged into any of the usb ports in my computer except my arduino. I think I may have misworded my previous post. In the line "myPort = new Serial(this, Serial.list()[0], 9600);"
If I change it to this "myPort = new Serial(this, Serial.list()[3], 9600);" I get an error. Because of this I just totally replaced that line with "myPort = new Serial(this, "COM3", 9600);" and it compiles fine. However now the face comes on fine, but it doesn't jitter. I know the arduino sketch works because in the arduino serial monitor there is a bunch of lowercase a's and when I press the button there is a bunch of B's. And yes I did change my processing code to if (inByte == 'B'). I think it must be something to do with the drawing/animation. I should go do some research on it.

 

Do keep in mind when you're using that code, it's an array.. starting from 0 to whatever..

Mine is Com1 to my Arduino, since it's always the second thing hooked up.  It's not necessarily the name of the port, but where it's located in terms of the array. But as far as changing it to com 0, you'd have to change your USB settings, and it's just easier changing the code.

Try using port 1 instead, or 2. It's not very likely (for me anyway) to have that many USB connections, using COM3 like you're suggesting, means you have 3 other USB connections, other than the Arduino. (4 total)

My mouse is my first connection (com0), my first arduino is com1, and second arduino is com2, only when they're all connected.  If you unplug the First arduino, the second arduinos comport becomes com1.

 

I'm sure I missed something again but hope this kind of explains! :P

For some reason when I use your code it compiles fine, but when I change the serial port from 0, to 3 (default for my arduino) it doesn't work. Is there anyway to change the arduino default to com 0?

I'm not really sure.. I copied and pasted your code and got the same error, but I started from the first sketch, and compiled with no errors, try this code :

 Keep in mind, I only know some of the code because of the Arduino, as far as the Drawing goes and such, you're going to have to research that part.

 

import processing.serial.*;
Serial myPort;

void setup(){
 println(Serial.list());
 myPort = new Serial(this, Serial.list()[0], 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);
if (myPort.available() > 0) {
 int inByte = myPort.read();
if (inByte == 'A') {
translate(random(-5, 5),random(-5,5));
}
}
}
 

Ok. I did your suggestions and they get rid of some of the errors I was getting except now I get some new ones. Here is my current code:

import processing.serial.*
//A face that jitters when a switch on a robot is on.
Serial myPort;
void setup(){
 
 println(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);
if (myPort.available() > 0) {
 int inByte = myPort.read();
if (inByte=='B'){
translate(random(-5, 5),random(-5,5));
}
}
}

With this code I get an error saying expecting EOF, found "void". When I googled this error it brought me to a processing bug page...

Omg... I forgot the most important part in my last post...........

 

in your if statement, you almost have it right! You just need to add the { brackets after you begin the if statement.

 so it should look like :

 

if (inByte == 'A'){

translate(random-5, 5), random-5,5));

}