Arduino – Joystick-LINK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#define joystickX A4 #define joystickY A5 #define motorA 6 #define motorB 9 int speeds=0; int states=0; void setup() { } void loop() { speeds=map(analogRead(joystickY), 0, 1023, 0, 255); if (analogRead(joystickX) > 1024/2+30) { states=1; } else if (analogRead(joystickX) < 1024/2-30) { states=-1; } else { states=0; } if (states==1) { analogWrite(motorB, 0); analogWrite(motorA, speeds); } else if (states==-1) { analogWrite(motorA, 0); analogWrite(motorB, speeds); } else { analogWrite(motorA, 0); analogWrite(motorB, 0); } } |