diff --git a/06-joystick/06-joystick.ino b/06-joystick/06-joystick.ino index 6aa5087..7e09a94 100644 --- a/06-joystick/06-joystick.ino +++ b/06-joystick/06-joystick.ino @@ -187,41 +187,39 @@ int calibrateJoystick(Joystick *js) ERROR(ERRNO_NULLPOINTER); } curmillis = millis(); - if ( js->state == JOYSTICK_STATE_CALIBRATING ) { - if ( js->x.calibration.start_time == 0 || js->y.calibration.start_time == 0 ) { - // Starting a new calibration cycle - Serial.printf("Starting a new calibration cycle\n"); - js->x.calibration.low = 65535; - js->x.calibration.high = 0; - js->y.calibration.low = 65535; - js->y.calibration.high = 0; - js->x.calibration.start_time = curmillis; - js->y.calibration.start_time = curmillis; - return ERRNO_SUCCESS; - } - if ( (curmillis - js->x.calibration.start_time) >= js->x.calibration.stable_time ) { - js->state = JOYSTICK_STATE_CALIBRATED; - js->x.calibration.start_time = 0; - js->y.calibration.start_time = 0; - Serial.printf("Calibrated joystick to x: <%d, %d> y <%d, %d>\n", - js->x.calibration.low, - js->x.calibration.high, - js->y.calibration.low, - js->y.calibration.high); - return ERRNO_SUCCESS; - } - x = analogRead(js->x.pin); - if ( x < js->x.calibration.low ) { - js->x.calibration.low = x; - } else if ( x > js->x.calibration.high ) { - js->x.calibration.high = x; - } - y = analogRead(js->y.pin); - if ( y < js->y.calibration.low ) { - js->y.calibration.low = y; - } else if ( y > js->y.calibration.high ) { - js->y.calibration.high = y; - } + if ( js->x.calibration.start_time == 0 || js->y.calibration.start_time == 0 ) { + // Starting a new calibration cycle + Serial.printf("Starting a new calibration cycle\n"); + js->x.calibration.low = 65535; + js->x.calibration.high = 0; + js->y.calibration.low = 65535; + js->y.calibration.high = 0; + js->x.calibration.start_time = curmillis; + js->y.calibration.start_time = curmillis; + return ERRNO_SUCCESS; + } + if ( (curmillis - js->x.calibration.start_time) >= js->x.calibration.stable_time ) { + js->state = JOYSTICK_STATE_CALIBRATED; + js->x.calibration.start_time = 0; + js->y.calibration.start_time = 0; + Serial.printf("Calibrated joystick to x: <%d, %d> y <%d, %d>\n", + js->x.calibration.low, + js->x.calibration.high, + js->y.calibration.low, + js->y.calibration.high); + return ERRNO_SUCCESS; + } + x = analogRead(js->x.pin); + if ( x < js->x.calibration.low ) { + js->x.calibration.low = x; + } else if ( x > js->x.calibration.high ) { + js->x.calibration.high = x; + } + y = analogRead(js->y.pin); + if ( y < js->y.calibration.low ) { + js->y.calibration.low = y; + } else if ( y > js->y.calibration.high ) { + js->y.calibration.high = y; } return ERRNO_SUCCESS; } diff --git a/06-joystick/README.md b/06-joystick/README.md index 9aa4710..5de5128 100644 --- a/06-joystick/README.md +++ b/06-joystick/README.md @@ -1,5 +1,7 @@ # Joystick demo +![breadboard](breadboard.png) + This demo shows using a joystick peripheral to control the position of an LED light on a WS2812 light strip, as well as how to calibrate a joystick, while using PWM driven LEDs to show the relative position of a joystick on its axis and the status of the momentary joystick pushbutton. I won't cover the entirety of the code here, but you are welcome to peruse it for yourself: [06-joystick.ino](06-joystick.ino) @@ -18,6 +20,10 @@ I've been using a breadboard software named [Fritzing](https://fritzing.org/) to I also haven't gotten the hang of Fritzing's schematic functionality. It can generate schematics from breadboards, which is pretty cool, but I'll be honest the schematics are soup. You have to do a ton of cleanup to make them readable, and I haven't devoted the time yet to figuring out the best way to manage that workflow. +![schematic](schematic.png) + +That, uh ... That needs some work. + But if you're getting into this hobby, Fritzing is definitely worth the (small) money. ## I still don't trust C++ @@ -217,41 +223,39 @@ int calibrateJoystick(Joystick *js) ERROR(ERRNO_NULLPOINTER); } curmillis = millis(); - if ( js->state == JOYSTICK_STATE_CALIBRATING ) { - if ( js->x.calibration.start_time == 0 || js->y.calibration.start_time == 0 ) { - // Starting a new calibration cycle - Serial.printf("Starting a new calibration cycle\n"); - js->x.calibration.low = 65535; - js->x.calibration.high = 0; - js->y.calibration.low = 65535; - js->y.calibration.high = 0; - js->x.calibration.start_time = curmillis; - js->y.calibration.start_time = curmillis; - return ERRNO_SUCCESS; - } - if ( (curmillis - js->x.calibration.start_time) >= js->x.calibration.stable_time ) { - js->state = JOYSTICK_STATE_CALIBRATED; - js->x.calibration.start_time = 0; - js->y.calibration.start_time = 0; - Serial.printf("Calibrated joystick to x: <%d, %d> y <%d, %d>\n", - js->x.calibration.low, - js->x.calibration.high, - js->y.calibration.low, - js->y.calibration.high); - return ERRNO_SUCCESS; - } - x = analogRead(js->x.pin); - if ( x < js->x.calibration.low ) { - js->x.calibration.low = x; - } else if ( x > js->x.calibration.high ) { - js->x.calibration.high = x; - } - y = analogRead(js->y.pin); - if ( y < js->y.calibration.low ) { - js->y.calibration.low = y; - } else if ( y > js->y.calibration.high ) { - js->y.calibration.high = y; - } + if ( js->x.calibration.start_time == 0 || js->y.calibration.start_time == 0 ) { + // Starting a new calibration cycle + Serial.printf("Starting a new calibration cycle\n"); + js->x.calibration.low = 65535; + js->x.calibration.high = 0; + js->y.calibration.low = 65535; + js->y.calibration.high = 0; + js->x.calibration.start_time = curmillis; + js->y.calibration.start_time = curmillis; + return ERRNO_SUCCESS; + } + if ( (curmillis - js->x.calibration.start_time) >= js->x.calibration.stable_time ) { + js->state = JOYSTICK_STATE_CALIBRATED; + js->x.calibration.start_time = 0; + js->y.calibration.start_time = 0; + Serial.printf("Calibrated joystick to x: <%d, %d> y <%d, %d>\n", + js->x.calibration.low, + js->x.calibration.high, + js->y.calibration.low, + js->y.calibration.high); + return ERRNO_SUCCESS; + } + x = analogRead(js->x.pin); + if ( x < js->x.calibration.low ) { + js->x.calibration.low = x; + } else if ( x > js->x.calibration.high ) { + js->x.calibration.high = x; + } + y = analogRead(js->y.pin); + if ( y < js->y.calibration.low ) { + js->y.calibration.low = y; + } else if ( y > js->y.calibration.high ) { + js->y.calibration.high = y; } return ERRNO_SUCCESS; } diff --git a/06-joystick/breadboard.png b/06-joystick/breadboard.png new file mode 100644 index 0000000..83d5ce2 Binary files /dev/null and b/06-joystick/breadboard.png differ diff --git a/06-joystick/schematic.png b/06-joystick/schematic.png new file mode 100644 index 0000000..35736ca Binary files /dev/null and b/06-joystick/schematic.png differ