Hello, I have written code that uses a digispark, a neopixel LED and a switch. It works by if the button is held the LED lights up. If the button is pressed twice within quick succession then the way the LED lights up changes. ( I hope this makes sense ). I am also trying to make it work so that if the switch is held down for more than 2 seconds it sends a message to the computer as a keypress ( using the digikeyboard.h ) but this doesnt work. If i remove the delay and make it send the message on a keypress then it works.
#include <Adafruit_NeoPixel.h> //Controls the LED
#include "DigiKeyboard.h"
const int buttonPin = 2; // the pin that the pushbutton is attached to
// LED VARIABLES
#define PIXEL_PIN 0 //Connect LED to Pin 0
#define PIXEL_COUNT 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
unsigned long lastUpdate = 0;
int LightMode;
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int timerEnable = 0;
int duration;
int startTime;
int endTime;
int buttonClicks;
// Times for long press
int endTime2;
int duration2;
int startTime2;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
strip.begin(); // Initialise the LED
strip.show();
strip.setBrightness(10);
}
void LightModeLoop() {
switch (LightMode) {
case 0:
rainbowCycle();
break;
case 1:
strip.setPixelColor(0, 255, 0, 0);
strip.show(); // Red
break;
case 2:
strip.setPixelColor(0, 0, 255, 0);
strip.show(); // Green
break;
case 3:
strip.setPixelColor(0, 0, 0, 255);
strip.show(); // Blue
break;
}
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
//if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) { //on
// if the current state is HIGH then the button went from off to on:
startTime2 = millis();
if (timerEnable == 0) {
startTime = millis();
timerEnable = 1;
buttonClicks = 0;
}
if ((timerEnable == 1) && (buttonClicks == 1)) {
endTime = millis();
timerEnable = 0;
duration = endTime - startTime;
if (duration <= 500) {
LightMode = LightMode + 1;
if ( LightMode > 3 ) {
LightMode = 0;
}
}
}
LightModeLoop();
endTime2 = millis();
duration2 = startTime2 - endTime2;
if (duration2 >= 100) {
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.println("Hello");
}
} else { //off
// if the current state is LOW then the button went from on to off:
endTime2 = millis();
duration2 = startTime2 - endTime2;
if (duration2 >= 2000) {
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.println("Hello");
}
strip.setPixelColor(0, 0, 0, 0);
strip.show();
buttonClicks = 1;
}
// Delay a little bit to avoid bouncing
delay(50);
//}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
//////////RAINBOW CYCLE CODE
void rainbowCycle() { // modified from Adafruit example to make it a state machine
static uint16_t j = 0;
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
j++;
if (j >= 256 * 5) j = 0;
lastUpdate = millis(); // time for next change to the display
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
Im not sure how to make it send the message after being held for 2 seconds or more and any help is gratly appreciated.
Thank you