// input pins const byte Xin = A0; // X input connected to A0 const byte Yin = A1; // Y input connected to A1 const byte Buttonin = 11; // pushbuttom input connected to Digital pin 7 // output pins (only if necessary) int ledPinBlue = 0; // select the pin for the blue LED PB0 int ledPinRed = 1; // select the pin for the red LED PB1 // 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(ledPinBlue, OUTPUT); // declare the ledPin as an OUTPUT pinMode(ledPinRed, OUTPUT); // declare the ledPin as an OUTPUT } void loop() { // put your main code here, to run repeatedly: //readPotentiometer(); moveAxis(); } void readPotentiometer(){ sensorValuex = analogRead(Xin); sensorValuey = analogRead(Yin); buttonState = digitalRead(Buttonin); /* 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 } } }