I am trying to use the capacitive touch pad of the board to toggle between two modes. Each mode is indicated by a different color from the neopixel. In each mode I want to execute a USB keyboard command but only if the board's button switch is pressed. Basically, use the touch pad to toggle between two different USB keyboard outputs when the button is pressed.
I can get parts of the code to work but can't get it to work together. I can use the touch pad to toggle between modes. I can get USB keyboard output when the button is pressed. But when I put it together as shown in the attached code, toggling the mode works but getting output when the key is pressed does not.
One clue, if I quickly cycle between the modes with the touch pad and repeatedly push the button, occasionally I get the desired output but only the first case (Alt + Keypad 0176 = ° the degree symbol)
I have been coding in the Arduino IDE for a while but still have a lot to learn and getting the braces right is one of my weak points.
Any Ideas?
Code: Select all
#include <Adafruit_NeoPixel.h>
#include <Adafruit_FreeTouch.h>
#include <Keyboard.h>
#include <Bounce2.h>
#define BUTTON_PIN PIN_SWITCH
//Bounce debouncedSwitch = Bounce();
Bounce2::Button debouncedSwitch = Bounce2::Button();
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
// Create the touch pad
Adafruit_FreeTouch qt = Adafruit_FreeTouch(PIN_TOUCH, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
bool oldState = HIGH;
int mode = 0;
int switchClosed = LOW;
//bool switchClosedSetup;
void setup() {
Serial.begin(9600);
//while (!Serial);
strip.begin();
strip.setBrightness(neo_brightness);
strip.show(); // Initialize all pixels to 'off'
if (! qt.begin())
Serial.println("Failed to begin qt");
debouncedSwitch.attach(BUTTON_PIN, INPUT_PULLDOWN);
debouncedSwitch.interval(5);
debouncedSwitch.setPressedState(LOW);
Keyboard.begin();
}
uint8_t j=0;
void loop() {
bool newState = HIGH;
// measure the captouches
uint16_t touch = qt.measure();
// If the pad is touched, turn on neopix!
if (touch > 500) {newState = LOW;}
if((newState == LOW) && (oldState == HIGH)) {
// Short delay to debounce button.
delay(50);
uint16_t touch = qt.measure();
if (touch > 500) {newState = LOW;}
if(newState == LOW) { // Yes, still low
if(++mode > 1) mode = 0; // Advance to next mode, wrap around after #1
// Update the Bounce instance :
switch(mode){
{case 0:
Serial.println("Case 0");
// check mechswitch
strip.setPixelColor(0, strip.Color(255,0,0));
strip.show();
debouncedSwitch.update();
// Get the updated value :
if (debouncedSwitch.pressed() ) {
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_KP_0);
Keyboard.press(KEY_KP_1);
Keyboard.press(KEY_KP_7);
Keyboard.press(KEY_KP_6);
Keyboard.releaseAll();
}
break;
}
{case 1:
Serial.println("Case 1");
strip.setPixelColor(0, strip.Color(0,255,0));
strip.show();
debouncedSwitch.update();
// Get the updated value :
if (debouncedSwitch.pressed() ) {
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_KP_0);
Keyboard.press(KEY_KP_1);
Keyboard.press(KEY_KP_5);
Keyboard.press(KEY_KP_0);
Keyboard.releaseAll();
}
break;
}
}
}
}
oldState = newState;
}