Dear All,
I have created a Xbee + Arduino wireless system to remotely control the LED brightness by a potentiometer. The system components are 2 Xbee chips ( series 1) and 1 Xbee shield (from Sparkfun), 1 Arduino Leonardo, a potentiometer (20k), a LED.
Xbee is configured using API 2 mode (Escape mode). Xbee router is configured as end device. Xbee coordinator is as coordinator.
Router ( transimitter): 1 Xbee and 1 pot. The pot connects to DIO0 pin.
Coordinator ( receiver): 1 Xbee, 1 Xbee Shield, 1 Leonardo, 1LED.
The code is running properly. I can see the serial readout from serial monitor. But the parsing part of code doesnt function well or at all (I guess???). Because when I turn the knob of my pot from the xbee transmitter end, the LED brightness doenst change (actually the LED doesnt light on even). Could someone take a look at the code to see where the problem is?
Also since the data type is 16 bit (API ID identifer: 0x83), I can use the last two bits of infomation before the checksum to find the analog value of pot voltage. But from browsing through the serial readout from serial monitor, the serial readout sometimes (not always) displays 03 255 (which are max value of analog voltage 3.3V) even if the actual voltage stays at 1.2v. I dont know where this readout inconsistency comes from? Thank you guys indeed.
The code I use to receiver RX data is
int myData = 0;
int const LEDPIN = 11;
void setup(){
// Start up our serial port, we configured our XBEE devices for 9600 bps.
Serial.begin(9600);
Serial1.begin(9600);
pinMode(LEDPIN,OUTPUT);
// while the serial stream is not open, do nothing:
while (!Serial) ;
}
void loop(){
while(Serial1.available()>0){
Serial.println(Serial1.read()); //input from Serial1 to Serial
}
// send data only when you receive data:
//if(Serial.available() > 0){
// myData = Serial.read();
// Serial.println(myData);
// }
if (Serial.available() >= 20) { // Wait until we have a mouthful of data
if (Serial.read() == 126) { // Start delimiter of a frame
// Skip over the bytes in the API frame we don't care about
for (int i = 0; i < 10; i++) {
Serial.read();
}
// The next two bytes are the high and low bytes of the sensor reading
int analogHigh = Serial.read();
int analogLow = Serial.read();
int analogValue = analogLow + (analogHigh * 256);
// Scale the brightness to the Arduino PWM range
int brightness = map(analogValue, 0, 1023, 0, 255);
// Light the LED
analogWrite(LEDPIN, brightness);
}
}
}
126
0
10
131
18
52
34
0
1
2
0
1
24 (01 24 are values for analog voltage)
248
126
0
10
131
18
52
34
0
1
2
0
3
255 (03 255 which corresponds to 3.3v are way off the actual voltage input)
15
126