Xbee series 1 sender/receiver with pot and Led

Hi,
Scott from Salem.
I want to control a servo with two xbee series 1 radios. I started trying to to control the brightness of an led first as I want to understand what is happening, but alas, after several hours and internet searching, could not get the led brightness working. First the LED, then I’ll just substitute it with a servo using the servo.h library. I am sure the xbees are transmitting. I have power and association and recieve lights (xbee pins 13,15,6) all working fine. Can someone please help? I have downloated the latest arduino 1.0.1 Thanks
Here is my code:

// SENDER
int potPin = 0;
int val = 0;
int mapVal = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(potPin, INPUT);
}

void loop()
{
  val = analogRead(potPin);
  
  mapVal = map(val,0,1023,0,255);
  
  Serial.println(mapVal, BIN);
}

and receiver

//RECEIVER
byte incomingByte = 0;  //initializes incoming Byte data from pot
int Led = 11;

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

void loop()
{
  if(Serial.available() > 0);
  {incomingByte = Serial.read();
  
  analogWrite(Led,incomingByte);
  }
}

Hello, Scott.

I see a problem in your receiver code:

  if(Serial.available() > 0);
  {incomingByte = Serial.read();
  
  analogWrite(Led,incomingByte);
  }

You have an extra semicolon after the if statement. Because of that, the block of code under it is not associated with it; the if statement has no effect, and the block is always run. Could you try again with that semicolon removed and see if your system behaves better?

- Kevin

HI Kevin,
Thank you so much for responding!! :slight_smile:
I removed the semicolon and it did not work. I then added a couple of delays thinking it might need it, and still it did not work. Meaning when I turn the pot on the sending side, nothing happens on the receive side.
The Led is connected to pin 11 on the receiver end of the arduino. On the sender the pot is a 10k ohm, and I am using the 3.3 volt pin for the positive side (same as the xbee). For some reason the Led is on all the time.
Here is the code with changes. I am assuming when I Serial.print(mapVal), it is sending the decimal number in a byte, and on the Serial.read(), it is taking that byte and making it a decimal number again for analogWrite to the Led?
Thank you.
Scott

// SENDER
int potPin = 0;
int val = 0;
int mapVal = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(potPin, INPUT);
}

void loop()
{
  val = analogRead(potPin);
  
  mapVal = map(val,0,1023,0,255);
  
  Serial.print(mapVal);
  delay(50);
}

Receive:

//RECEIVER
int incomingByte = 0;  //initializes incoming Byte data from pot
int Led = 11;

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

void loop()
{
  if(Serial.available() > 0)
  {
    incomingByte = Serial.read();
    delay(50);
    analogWrite(Led,incomingByte);
    delay(50);
  }
}

Kevin, One more thing:
When I configured the radios, for the sending radio I did not configure the code ATIR64. Could this possibly make a difference?
Scott

Hi Kevin,
More info for you to help me. I took the receiving xbee and disconnected it from the breakout board and arduino, and plugged it into an adapter board and then into a terminal program on my computer. It works this way!! I can see the pot values in the terminal window of the receiving xbee, that are sent from the sending xbee. Progress! But I still can’t get the led in brightness when I hook it back up again. Good progress. Any ideas?
thanks so much,
Scott
my background is in psychology so this is a great struggle but worth it. I want to learn and learn…

Actually, Serial.print() sends the number in a human-readable form, not as a byte containing the raw value. For the latter, you should use Serial.write(). See if changing that makes it start working.

Thanks for pointing that out; I didn’t catch it the first time around.

- Kevin

HI Kevin,
Yes, that was the problem. Serial.print did not work. Serial.write did work. I am a little confused though.

Am I correct in my thinking…

  1. Serial.print (25) must be sent as a binary byte even though it’s human readable?. Its the only way to travel through the air. Yet…
  2. Serial.write (25) also must sent as a binary byte. So both are sent as binary byte, so how is serial.print human readable?

it would seem that both would be read by Serial.read on the receiving end, then converted to a decimal number for the arduino command of analogWrite to change the value of the led. Is analog.write sending a human readable number or binary?

I am over my head here but starting to get it. Can you please explain? Thank you so much!
Scott

Kevin,
I think I figured it out:

Serial.print(123) sends out three seperate bytes a 1 in byte form; a 2 in byte form; and a 3 in byte form.

Serial.write(123) sends out one byte 01111011 and Serial.read reads just one byte, then I can do a analog.write with that one byte

Yes???
Scott

Hi, Scott.

You are correct that this sends three bytes, but those bytes are ‘1’, ‘2’, and ‘3’; these are ASCII representations of the digits. For example, ‘1’ has the value 49, and ‘A’ has the value 65.

- Ben

Thank’s Ben and Kevin!! I’ll be back with more questions :slight_smile: