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.

When you’re trying to right

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

}

 

 

Oh
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’”)

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

Sorry, I totally forgot that

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 :smiley:

Omg… I forgot the most

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

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

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

Thats weird…
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

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! :stuck_out_tongue:

I’m afraid i’m a bit

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.

 

I’m sorry if I wasn’t

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

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