I am still a beginner in arduino. I am trying to send the analog values of the x position and y position from the joystick which is connected to xbee coordinator to the xbee router each connected to uno arduino. While the joystick send serial print value, the reciever displays number on serial monitor. My code is to make led on when joystick is not in center position. But it makes the led turn on and of immediately. please reply i really need help
Here is my code
const byte PIN_ANALOG_X = A0;
const byte PIN_ANALOG_Y = A1;//to calibrate the joystick
const int X_THRESHOLD_LOW = 331;
const int X_THRESHOLD_HIGH = 336;const int Y_THRESHOLD_LOW = 324;
const int Y_THRESHOLD_HIGH = 330;int x_position;
int y_position;int x_direction;
int y_direction;void setup() {
Serial.begin(9600);
pinMode(PIN_ANALOG_X, INPUT);
pinMode(PIN_ANALOG_Y, INPUT);
}
void loop() {x_direction = 0;
y_direction = 0;x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);if (x_position > X_THRESHOLD_HIGH) {
x_direction = 1;
} else if (x_position < X_THRESHOLD_LOW) {
x_direction = -1;
}if (y_position > Y_THRESHOLD_HIGH) {
y_direction = 1;
} else if (y_position < Y_THRESHOLD_LOW) {
y_direction = -1;
}if (x_direction == -1) {
if (y_direction == -1) {
Serial.println("q"); // joystick in left down position
} else if (y_direction == 0) {
Serial.println("w"); // joystick in left position
} else {
// y_direction == 1
Serial.println("e"); // left down
}
} else if (x_direction == 0) {
if (y_direction == -1) {
Serial.println("a"); // down
} else if (y_direction == 0) {
Serial.println("s"); // center
} else {
// y_direction == 1
Serial.println("d"); // up
}
} else {
// x_direction == 1
if (y_direction == -1) {
Serial.println("z"); // right down
} else if (y_direction == 0) {
Serial.println("x"); // right
} else {
// y_direction == 1
Serial.println("c"); // right up
}
}}
For receiver :
int ledpin = 13;
char receivedCommand;/* Declares an array holding various armature positions. If your servo starts grinding when you try 0 or 4, change this array to hold values in the range 1000 - 2000. */
void setup()
{
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, LOW);}
void loop()
{
if(Serial.available())
{
receivedCommand = Serial.read();
/* If any data is available in the serial interface buffer, it's read into receivedCommand. Reading the buffer automatically empties it of the read character. */switch(receivedCommand)
{
/* This switch-case construct will evaluate the received character and write the corresponding position to the servo. */case 's' :
{
/* If the received character is '0', this block will be executed */
digitalWrite(ledpin, LOW);
Serial.println(receivedCommand);break;
}
case 'a' :
{
/* If the received character is '0', this block will be executed */
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}
case 'd' :
{
/* If the received character is '0', this block will be executed */
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}
/* Exits the switch-case block. */
case 'q' :
{
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}
case 'w' :
{
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}
case 'e' :
{
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}case 'z' :
{
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}
case 'x' :
{
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}
case 'c' :
{
digitalWrite(ledpin, HIGH);
Serial.println(receivedCommand);break;
}
}}
}