diff --git a/Code/Joystick_ohne_float/Joystick_ohne_float.ino b/Code/Joystick_ohne_float/Joystick_ohne_float.ino index 2bcf183..c197786 100644 --- a/Code/Joystick_ohne_float/Joystick_ohne_float.ino +++ b/Code/Joystick_ohne_float/Joystick_ohne_float.ino @@ -2,18 +2,26 @@ #define yPin A6 int16_t xValue = 0; int16_t yValue = 0; +int16_t leftPWM = 0; +int16_t rightPWM = 0; + +const int16_t deadZone = 10; void setup() { - Serial.begin(9600); + Serial.begin(115200); } void loop() { koordinaten(analogRead(xPin), analogRead(yPin)); - - Serial.println("X: "); + motorPWM(); + Serial.print("X: "); Serial.println(xValue); - Serial.println("Y: "); + Serial.print("Y: "); Serial.println(yValue); + Serial.print("links: "); + Serial.println(leftPWM); + Serial.print("rechts: "); + Serial.println(rightPWM); delay(200); @@ -26,3 +34,52 @@ void koordinaten(uint16_t x, uint16_t y) { xValue = map(x, 0, 511, -255, 255); yValue = map(y, 0, 511, 255, -255); } + +void motorPWM() { + if((abs(xValue) > deadZone) || (abs(yValue) > deadZone)) { + if(yValue >= 0) { + if(xValue >= 0) { + //+y , +x + leftPWM = yValue; + rightPWM = yValue - xValue; + if(xValue >= yValue) { + leftPWM = 255; + rightPWM = -255; + } + } else { + //+y , -x + leftPWM = yValue; + rightPWM = yValue + xValue; + if(abs(xValue) >= yValue) { + leftPWM = -255; + rightPWM = 255; + } + } + } else { + if(xValue >= 0) { + //-y , +x + leftPWM = yValue; + rightPWM = yValue + xValue; + if(xValue >= abs(yValue)) { + leftPWM = 255; + rightPWM = -255; + } + } else { + //-y , -x + leftPWM = yValue; + rightPWM = yValue - xValue; + if(abs(xValue) >= abs(yValue)) { + leftPWM = -255; + rightPWM = 255; + } + } + } + if(abs(xValue) < deadZone) { + leftPWM = yValue; + rightPWM = yValue; + } + } else { + leftPWM = 0; + rightPWM = 0; + } +}