My sister and I are trying to modify the code from the Networked Lamp in the Getting Started book. Instead of sending a string like "#aa3b78" from Processing which is then picked apart by Arduino, we're sending a string like "#075" which we then pick apart. We can verify that we're sending that string from Processing to the Serial port.
We're confused about how Arduino is receiving the information from the Serial port. In the original example, it looks for the marker (#) and then gets the next six characters. First, we tried sending an integer without the marker, then the integer with the marker, then we converted it to a string, figuring maybe that's the form Arduino was looking for.
As it stands, first we make sure its enough characters - so the return string is in the form of "#---" So, if its only two characters, we insert an extra zero at the start, such as "#0--" If its negative, we set it to zero: "#000" This is so we can break it apart by characters, and remake it into an integer in Arduino. (First character multiplied by 100 + next multiplied by 10 + last character) No matter what, the number we send from Processing does not come out as we expect it to from Arduino.
We've been troubleshooting what number it's sending by turning on an LED. The RX/TX lights are blinking and we have gotten the LED to turn on, but when that happened it was a negative number (and it shouldn't have been). Between the two of us one knows Arduino moderately and the other knows Java, PHP, ASP.NET, etc. and we're fumbling through Processing.
Our questions would be what form is Arduino expecting to read from the Serial port - is it expecting a byte translation of a integer? Or what? Also, as we've been staring at this forever, do you spot any other errors?
Arduino sketch (it's a bit messy...):
// Example 08B: Arduino Networked Lamp
//
// Copy and paste this example into an empty Arduino sketch
//#define SENSOR 0
#define R_LED 13
//#define G_LED 10
//#define B_LED 11
//#define BUTTON 12
//int val = 0; // variable to store the value coming from the sensor
//int btn = LOW;
//int old_btn = LOW;
//int state = 0;
char buffer[4] ;
int pointer = 0;
byte inByte = 0;
int val = 0;
//byte r = 0;
//byte g = 0;
//byte b = 0;
void setup() {
Serial.begin(9600); // open the serial port
//pinMode(BUTTON, INPUT);
pinMode(R_LED, OUTPUT);
}
void loop() {
//val2 = digitalRead(6); // read the value from the sensor
//Serial.println(val2); // print the value to
// the serial port
if (Serial.available() >0) {
// read the incoming byte:
inByte = Serial.read();
// If the marker's found, next 6 characters are the colour
if (inByte == '#') {
while (pointer < 4) { // accumulate 6 chars
buffer[pointer] = Serial.read(); // store in the buffer
pointer++; // move the pointer forward by 1
}
{
//val = int(inByte);
val = (int(buffer[0])*100) + (int(buffer[1])*10) + (int(buffer[2]));
Serial.println(val);
// now we have the 3 numbers stored as hex numbers
// we need to decode them into 3 bytes r, g and b
//r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
//g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
//b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0; // reset the pointer so we can reuse the buffer
}
}
//btn = digitalRead(BUTTON); // read input value and store it
// Check if there was a transition
//if ((btn == HIGH) && (old_btn == LOW)){
// state = 1 - state;
//old_btn = btn; // val is now old, let's store it
//if (state == 1) { // if the lamp is on
{
if(val <= 1) {
analogWrite(R_LED, HIGH); // turn the leds on
/*delay(100);
analogWrite(R_LED, LOW);
delay(100);
analogWrite(R_LED, HIGH); // turn the leds on
delay(100);
analogWrite(R_LED, LOW);*/
}
/*
if(val > 401 && val < 700) {
analogWrite(R_LED, HIGH); // turn the leds on
delay(200);
analogWrite(R_LED, LOW);
}
if(val > 701) {
analogWrite(R_LED, HIGH); // turn the leds on
delay(300);
analogWrite(R_LED, LOW);
}
if(val < 100) {
analogWrite(R_LED, LOW);
}
// analogWrite(G_LED, g); // at the colour
// analogWrite(B_LED, b); // sent by the computer
// } else {
//analogWrite(R_LED, 0); // otherwise turn off
//analogWrite(G_LED, 0);
//analogWrite(B_LED, 0);
//}
*/
delay(100); // wait 100ms between each send
}
}
}
I'll post the whole processing sketch in a reply, but here is the portion where we manipulate the string we're sending to the serial port:
llstr = Integer.toString(water); // Prepare a string to be sent to Arduino
if (llstr.length() == 3) {
ll = "#" + llstr;}
if (llstr.length() == 2 ){
ll = "#0" + llstr;}
if (llstr.length() == 1) {
ll = "#00" + llstr;}
if (abs(water) != water) {
water = 0;}
println(ll);
We're running Arduino-0017 and Processing1.0.7 on Windows XP SP2 with an Arduino Duemilanove.