Let's Make Robots!

Processing-Arduino serial port problem

I've picked up an arduino to start working with, and have currently slung together a bit of code to toggle the on-board LED over serial. I've also modified one of the Processing demos to use as a GUI to turn it on and off. However, my arduino is on COM6, and if I try to change the port on Processing to anything other than 0, it comes up with an error. I know the arduino code works, as I've tried it with the arduino serial terminal, however the processing app doesn't want to play ball. I also downloaded this processing sketch (links to a web page, not a download) that shows the availiable ports, and allows you to switch between them, but this also dies when I try COM6.

This is my code;

Arduino:

int LED = 13;
boolean light = false;
char serVal;

void setup() {
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
}

void loop() {
  while (Serial.available() > 0) {
    serVal = Serial.read();
    if (serVal == 'o') {
      light = !light;
      serVal = 'n';
      digitalWrite(LED,light);
    }
  }
}

Processing:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

void setup()
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];       //I HAVEN'T CHANGED THE PORT ON THIS VERSION OF THE SKETCH
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(204);                    // change color and
    myPort.write('o');              // send an H to indicate mouse is over square
  }
  rect(50, 50, 100, 100);         // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

 

 

Any Ideas?

Thanks in advance

 

More problems...

I've got the serial comunication going so that I can toggle the on-board LED from my processing sketch, and this works fine. However, I've now had a shot at sending an analog value from the arduino to processing, and the sketch seems to be stuck flickering between 2 values. The arduino is sending the right data, so what am I doing wrong?

Processing:

import processing.serial.*;
Serial myPort;
int val = 0;
PFont f;
void setup() {
  size(500,250);
  float x = 0;
  float y = 0;
  f = createFont("arial",16,true);
  myPort = new Serial(this, "COM6", 9600);
}

void draw() {
  if(myPort.available() > 0) {
    val = myPort.read();
  }
  background(255);
  stroke(0);
  strokeWeight(1);
  fill(0);
  textFont(f);
  textAlign(LEFT);
  text(val,10,20);
  ellipseMode(CENTER);
  ellipse(250,240,420,420);
  stroke(255);
  strokeWeight(5);
  float x = cos(radians(360-val))*200;
  float y = sin(radians(360-val))*200;
  line(250,240,(250 + x),(240 + y));
}

 

Arduino:

int LDR = A0;
int val;

void setup() {
  Serial.begin(9600);
}
void loop() {
  int val = analogRead(LDR);
  Serial.println(val);
}

 

Thans again.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chris the Carpenter's picture

You are having an issue with sending one thing and getting another. This is very common and there is a very good reason for it. Put simply, when dealing with serial data transfer, you can only send a byte at a time. Everything gets sent as a series of bytes. 

In your case, you are taking the reading and sticking it into an int (which is made up of multiple bytes). This int must be broken into bytes, sent, then reassembled on the other side. The good news is that you are probably only using 2 bytes of the int, and Arduino has a handy way of dealing with them... Here's where to go:

Hit the Arduino website and start looking for the highByte() and lowByte() commands --this is the basis of what you need to do. 

Dig around there (with the highbyte/ lowbyte stuff) and call me in the morning.

Thanks for the reply. I haven't had a chance to play around with it again yet, but I think I get the picture. I'll reply again when I've had a chance to edit the code.

Chris the Carpenter's picture

The "list" is an array of strings, each string is the name of a com port. Because it is an array, it is always going to strart at 0. The actual com port numbers are generally not going to start at 0 and might not even be sequential. Trying 1 or 2 will only work if the com port he is trying to use is second or third on the list. From what has been described, this is not the case.

6677's picture
I don't think the list numbers correspond to com numbers. Have you tried a value of 1 or maybe 2?
Chris the Carpenter's picture

I would assume that com 6 is indeed your first com port and thus shows up at 0 on the serial list.

I personally have never liked this whole "serial list" thing and I usually just import my com number manually.

myPort = new Serial(this,"COM6", 9600);

Thanks for the replies. I've just tried this one, and it's worked perfectly. However, the processing code causes the light to flicker, so I'll need to rewrite...