Dear all,
I have been working on this project for a while. Again,
Two xbee series 1 chip. 1 Xbee shield. 1 leonardo board, 1 potentiometer.
Objective: use the voltage of a pot to wireless control the mouse cursor on my laptop. The pot connects to the DIO0/AD0 pin in the transmitting module.
I did my Xbee configuration correctly (thanks a lot for paul's suggestions). I try to use the serial data (I parsed to obtain from the Xbee communication system ) to drive the mouse cursor on my laptop. I attach my code here. I have worked out the serial data code and mouse control code seperatedly. But it didnt work when I put them together. May you kindly take a look at it. Let me know what would be the problem there. Thank you very much.
float mydata = 0;
const int switchPin = 2;
const int ledpin = 13;
//SoftwareSerial Serial1(2,3);
int range = 2000;
int responseDelay = 5;
int prevXreading = 0;
boolean mouseIsActive = false;
int prevSwitchState = LOW;
void setup(){
// Start up our serial port, we configured our XBEE devices for 9600 bps.
Serial.begin(9600);
Serial1.begin(9600);
pinMode(switchPin, INPUT); // the switch pin
pinMode(ledpin,OUTPUT);
// while the serial stream is not open, do nothing:
while (!Serial) ;
Serial.println("Start");
// take control of the mouse:
Mouse.begin();
}
void loop(){
// read the switch:
int switchState = digitalRead(switchPin);
// if it's changed and it's high, toggle the mouse state:
if (switchState != prevSwitchState) {
if (switchState == HIGH) {
mouseIsActive = !mouseIsActive;
// turn on LED to indicate mouse state:
digitalWrite(ledpin, mouseIsActive);
}
}
// save switch state for next comparison:
prevSwitchState = switchState;
if (Serial1.available() >=20) { // Wait until we have a mouthful of data
//Serial.println("Got data?");
if (Serial1.read() == 126) { // Start delimiter of a frame
// Skip over the bytes in the API frame we don't care about
// Serial.println("Found Start Byte");
for (int i = 0; i < 10; i++) {
Serial1.read();
}
// The next two bytes are the high and low bytes of the sensor reading
int analogHigh = Serial1.read();
int analogLow = Serial1.read();
float analogValue = (analogLow + (analogHigh * 256))*3.3/1023;
mydata= Serial.println(analogValue);
int currXreading = mydata;
// Serial.println(myData);
//int currXreading = Serial.read();
int currXreading1 = map (currXreading, 0, 1023, 0, range);
// int currXreading1 = map (analogValue, 0, 1023, 0, range);
int xReading = prevXreading - currXreading1;
prevXreading = currXreading1;
// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, 0, 0);
}
}
}
}