#include #include #include #include RF24 radio(7, 8); // CE, CSN const byte address[6] = "00001"; // input pins const byte Xin = A0; // X input connected to A0 const byte Yin = A1; // Y input connected to A1 const byte Buttonin = 4; // pushbuttom input connected to Digital pin 7 // output pins (only if necessary) int ledPin = 13; // select the pin for the blue LED PB0 // state machine int state = 0; //threshold because of physical limitation 533 centre of joystick at x axis const int THRESHOLD_LOW_X = 523; const int THRESHOLD_HIGH_X = 543; //threshold because of physical limitation 522 centre of joystick at y axis const int THRESHOLD_LOW_Y = 512; const int THRESHOLD_HIGH_Y = 532; // joystick value int sensorValuex,sensorValuey, buttonState; void setup() { // put your setup code here, to run once: Serial.begin(9600); // initialize serial communication //Input pinMode (Buttonin, INPUT_PULLUP); // internal pull up resistor pinMode (Xin, INPUT); // initialize Xin as INPUT pinMode (Yin, INPUT); // initialize YZin as INPUT //Output pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) //radio setup NRF24L01 radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_MIN); radio.stopListening(); } void loop() { // put your main code here, to run repeatedly: readPotentiometer(); //moveAxis(); } void readPotentiometer(){ sensorValuex = analogRead(Xin); sensorValuey = analogRead(Yin); buttonState = digitalRead(Buttonin); // send the sensor Value of x through radio radio.write(&sensorValuex, sizeof(sensorValuex)); delay(5); /* Serial.print(" Axis XY"); Serial.print(" X:"); Serial.print(sensorValuex); Serial.print(" Y:"); Serial.println(sensorValuey); */ } void moveAxis(){ if(state == 0){ readPotentiometer(); if(sensorValuex > THRESHOLD_HIGH_X){ //move right state = 1; }else if(sensorValuex < THRESHOLD_LOW_X){ //move left state = 2; }else if(sensorValuey > THRESHOLD_HIGH_Y){ //move down state = 3; }else if(sensorValuey < THRESHOLD_LOW_Y){ //move up state = 4; } }else if(state ==1){ Serial.println("MOVE RIGHT"); readPotentiometer(); if((sensorValuex < THRESHOLD_HIGH_X)&& (sensorValuex > THRESHOLD_LOW_X)){ state = 0; // joy stick at the centre back to state 0 } }else if(state ==2){ Serial.println("MOVE LEFT"); readPotentiometer(); if((sensorValuex < THRESHOLD_HIGH_X)&& (sensorValuex > THRESHOLD_LOW_X)){ state = 0; // joy stick at the centre back to state 0 } }else if(state ==3){ Serial.println("MOVE DOWN"); readPotentiometer(); if((sensorValuey < THRESHOLD_HIGH_Y)&& (sensorValuey > THRESHOLD_LOW_Y)){ state = 0; // joy stick at the centre back to state 0 } }else if(state ==4){ Serial.println("MOVE UP"); readPotentiometer(); if((sensorValuey < THRESHOLD_HIGH_Y)&& (sensorValuey > THRESHOLD_LOW_Y)){ state = 0; // joy stick at the centre back to state 0 } } }