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.

When you're trying to right to the serialport, you're missing a period!

Serial.write()    Arduino is case sensitive, and it will show up as a keyword if you get it correct. Serial is alawys capitalized, the second isn't, but you need the period too! Or it won't work!

 

Also, you should use an Else statement, that sends another key, so you can tell the difference between the states.  For example, you could just use the lower case 'a'.   When you're using the Write function, or print   you can use aprostrophies on each side to convert it into the ASCII value when it sends out. For example, Serial.write('a') will print 97 ('a' has a decimal value of 97 in the ASCII charts.)  But you need at least two, to show when it should and when it shouldn't twitter.

 

After looking at the ASCII chart, the Processing code you have doesn't match, you should use == 'B'     in the processing program.  Or change the Arduino side to match.

just add this part after 

if (buttonstate == HIGH){

Serial.write(66);    // can also use Serial.write('B')

}  else   {

Serial.write(97);    // can also use Serial.write('a');

 }

 

 

Ok, thanks. Now the arduino code works. That was a pretty stupid mistake, and so was the ascII thing, off by one number. Anyway, I still get the error with the processing code expecting LPAREN, found "inByte". Is this another stupid mistake like forgetting the dot somewhere? (Note the only thing I changed from earlier was "if inByte = 'A'" to "if inByte = 'B'")

Sorry, I totally forgot that part!

 

Yes actually, with the inByte = A , what you're saying is.. inByte equals A.   You need to use the double ==, which means, inByte (IS EQUAL TO) A.

You want to use = when you're defining thigns :   const int ledPin = 13;

You want to use == when you're comparing things :  if (inByte == A)

 

This was copied from the Arduino page :

 x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)

 

http://arduino.cc/en/Reference/If

Also, if you EVER have any problems with any of your sketches and know the general area, this is the best reference for both Arduino and Processing code:  (seeing as they're nearly identical)

http://processing.org/reference/alpha.html

EDIT:

I should have tried the code before I said it's okay.. sorry:

Also, in your processing code: it's not printIn (capital I), it's println (lowercase l, ln stands for line)  println prints a new line for each data, print just sends data right after eachother, no line. 

so change  printIn(Serial.list());

to    println(Serial.list.());

 

And in your draw function:

while (myPort.available() > 0)     is only going to run one time. (while checks until it's needs are met) So it should only work the first time you input, and not again.

You should use the if statement there, only need to change the while, to an if and you should be good.

 

Also, you forgot to include the Serial library!! (you can import it on the toolbar, or type this at the top of your sketch):

import processing.serial.*

 

I haven't tried with an arduino, but I have the sketch of your face up. Let me know how it works out :D

Thats weird for some reason I still get the expecting LPAREN error after putting another = sign. By the way that processing link was very helpful.

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));

}

 

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

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));
}
}
}
 

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?

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

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.