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.