What's wrong with this Arduino code?

I’m new to Arduino (and crap, I love these things) and I’m wanting to start working towards more communication apps. With that said, I’m starting with GUI’s ( http://lab.guilhermemartins.net/?p=346 ) example on adjusting an LED’s brightness on the “receive” board by adjusting a pot on the “send” side. So I hooked up the boards as instructed and nothing’s happening. So I tried a loop back on RX/TX on the send board and found (via the serial monitor) that it’s outputting “junk” no matter what baud rate I set. The code is as follows…

Sender:

int analogValue5, val5;

void setup() {

// abertura da porta Serial

Serial.begin(19200);

}

void loop() {

// leitura do pin analógico 5

analogValue5 = analogRead(5);

// remapear valores para 0 / 255

val5 = map(analogValue5, 0, 1023, 0, 255);

// enviar valores para porta Serial

Serial.println(val5, BYTE);

}


// RECIEVER

byte incomingByte;

void setup() {

// abertura da porta Serial

Serial.begin(19200);

// declarar pin 11 como output, aqui está o LED

pinMode (11, OUTPUT);

}

void loop() {

// se houver bytes a entrar na porta serial

if (Serial.available()) {

// atribuir valores à variavel ‘incomingByte’

incomingByte = Serial.read();

// escreve valor no pin 11

analogWrite(11, int(incomingByte));

}

}

Does anyone see what’s wacky. This rookie appreciates the help…

I’d start by connecting the send board to the computer. Have it send readable characters:

map(analogValue5, 0, 1023, 65, 90)

That should give you letters from A to Z as you turn the pot.

If that works, do the reverse on the receive side:

map(incomingByte, 65, 90, 0, 255)

Then you can connect the serial monitor and send letters to the board and see changing brightness on the LED.

You should understand that the Arduino serial monitor talks to the board using pin1 and pin2. You don’t need anything connected on those pins when you’re using the serial monitor.

Just get one side working at a time, then try putting the two together.

Just noticed that the send side does

Serial.println(val5, BYTE);

and the receive side does

incomingByte = Serial.read();

Should use Serial.print(val5, BYTE) instead. Println will add a \n after your byte value, and the receive side will get the first byte fine, then it gets the \n and sets the output value to 13. Not exactly what you want.