Skip to main content
Re-indented OP's code
Source Link
JRobert
  • 15.3k
  • 3
  • 23
  • 51
unsignedXhunsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
   if (LEDstatus==1)
   {
      LEDcolor = LEDcolor +1;
      if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
      if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
      if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
      if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
      if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
      if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
      if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
      if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
      if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
}
   }
}

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
      LEDstatus=1;Serial.println("On");
   }
   if (LEDstatus==1 && changed==0)
   {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 255); 
      analogWrite(bluePin, 255);
      LEDcolor=0;
      changed=1;
      LEDstatus=0;Serial.println("Off");
   }
changed=0;
   changed=0;
}

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.println("key press");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
    Serial.println("key release");
    
    if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
   }
   else {
      shortKeyPress();
   }
}

void setup() 
  {
   Serial.begin(9600);
   pinMode(keyPin, INPUT);
   digitalWrite(keyPin, HIGH);
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT); 
   analogWrite(redPin, 255);
   analogWrite(greenPin, 255); 
   analogWrite(bluePin, 255);
  }

void loop() {
// key management section
   if (millis() - keyPrevMillis >= keySampleIntervalMs) {
      keyPrevMillis = millis();
   
  
     byte currKeyState = digitalRead(keyPin);
   
  
     if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
         keyPress();
      }
      else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
         keyRelease();
      }
      else if (currKeyState == LOW) {
         longKeyPressCount++;
    }
  }

  
     prevKeyState = currKeyState;
   }
}
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
if (LEDstatus==1)
{
LEDcolor = LEDcolor +1;
if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
}
  }

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
  LEDstatus=1;Serial.println("On");
}
if (LEDstatus==1 && changed==0)
{
  analogWrite(redPin, 255);
  analogWrite(greenPin, 255); 
  analogWrite(bluePin, 255);
  LEDcolor=0;
  changed=1;
  LEDstatus=0;Serial.println("Off");
}
changed=0;
  }

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.println("key press");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
    Serial.println("key release");
    
    if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
}
else {
    shortKeyPress();
}
}

void setup() 
  {
Serial.begin(9600);
pinMode(keyPin, INPUT);
digitalWrite(keyPin, HIGH);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT); 
analogWrite(redPin, 255);
analogWrite(greenPin, 255); 
analogWrite(bluePin, 255);
  }

void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
    keyPrevMillis = millis();
    
     byte currKeyState = digitalRead(keyPin);
    
     if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
        keyPress();
    }
    else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
        keyRelease();
    }
    else if (currKeyState == LOW) {
        longKeyPressCount++;
    }
    
     prevKeyState = currKeyState;
}
}
Xhunsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
{
   Serial.println("short");
   if (LEDstatus==1)
   {
      LEDcolor = LEDcolor +1;
      if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
      if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
      if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
      if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
      if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
      if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
      if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
      if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
      if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
   }
}

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
{
   Serial.println("long");
   if (LEDstatus==0 && changed==0)
   {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
      LEDstatus=1;Serial.println("On");
   }
   if (LEDstatus==1 && changed==0)
   {
      analogWrite(redPin, 255);
      analogWrite(greenPin, 255); 
      analogWrite(bluePin, 255);
      LEDcolor=0;
      changed=1;
      LEDstatus=0;Serial.println("Off");
   }
   changed=0;
}

// called when key goes from not pressed to pressed
void keyPress() {
   Serial.println("key press");
   longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
   Serial.println("key release");

   if (longKeyPressCount >= longKeyPressCountMax) {
      longKeyPress();
   }
   else {
      shortKeyPress();
   }
}

void setup() 
{
   Serial.begin(9600);
   pinMode(keyPin, INPUT);
   digitalWrite(keyPin, HIGH);
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT); 
   analogWrite(redPin, 255);
   analogWrite(greenPin, 255); 
   analogWrite(bluePin, 255);
}

void loop() {
// key management section
   if (millis() - keyPrevMillis >= keySampleIntervalMs) {
      keyPrevMillis = millis();
 
      byte currKeyState = digitalRead(keyPin);
 
      if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
         keyPress();
      }
      else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
         keyRelease();
      }
      else if (currKeyState == LOW) {
         longKeyPressCount++;
      }

      prevKeyState = currKeyState;
   }
}
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
if (LEDstatus==1)
{
LEDcolor = LEDcolor +1;
if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
}
  }

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
  LEDstatus=1;Serial.println("On");
}
if (LEDstatus==1 && changed==0)
{
  analogWrite(redPin, 255);
  analogWrite(greenPin, 255); 
  analogWrite(bluePin, 255);
  LEDcolor=0;
  changed=1;
  LEDstatus=0;Serial.println("Off");
}
changed=0;
  }

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.println("key press");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
    Serial.println("key release");
    
    if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
}
else {
    shortKeyPress();
}
}

void setup() 
  {
Serial.begin(9600);
pinMode(keyPin, INPUT);
digitalWrite(keyPin, HIGH);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT); 
analogWrite(redPin, 255);
analogWrite(greenPin, 255); 
analogWrite(bluePin, 255);
  }

void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
    keyPrevMillis = millis();
    
    byte currKeyState = digitalRead(keyPin);
    
    if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
        keyPress();
    }
    else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
        keyRelease();
    }
    else if (currKeyState == LOW) {
        longKeyPressCount++;
    }
    
    prevKeyState = currKeyState;
}
}
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
if (LEDstatus==1)
{
LEDcolor = LEDcolor +1;
if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
}
  }

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
  LEDstatus=1;Serial.println("On");
}
if (LEDstatus==1 && changed==0)
{
  analogWrite(redPin, 255);
  analogWrite(greenPin, 255); 
  analogWrite(bluePin, 255);
  LEDcolor=0;
  changed=1;
  LEDstatus=0;Serial.println("Off");
}
changed=0;
  }

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.println("key press");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
    Serial.println("key release");
    
    if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
}
else {
    shortKeyPress();
}
}

void setup() 
  {
Serial.begin(9600);
pinMode(keyPin, INPUT);
digitalWrite(keyPin, HIGH);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT); 
analogWrite(redPin, 255);
analogWrite(greenPin, 255); 
analogWrite(bluePin, 255);
  }

void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
    keyPrevMillis = millis();
    
    byte currKeyState = digitalRead(keyPin);
    
    if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
        keyPress();
    }
    else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
        keyRelease();
    }
    else if (currKeyState == LOW) {
        longKeyPressCount++;
    }
    
    prevKeyState = currKeyState;
}
}
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
if (LEDstatus==1)
{
LEDcolor = LEDcolor +1;
if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
}
  }

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
  LEDstatus=1;Serial.println("On");
}
if (LEDstatus==1 && changed==0)
{
  analogWrite(redPin, 255);
  analogWrite(greenPin, 255); 
  analogWrite(bluePin, 255);
  LEDcolor=0;
  changed=1;
  LEDstatus=0;Serial.println("Off");
}
changed=0;
  }

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.println("key press");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
    Serial.println("key release");
    
    if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
}
else {
    shortKeyPress();
}
}

void setup() 
  {
Serial.begin(9600);
pinMode(keyPin, INPUT);
digitalWrite(keyPin, HIGH);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT); 
analogWrite(redPin, 255);
analogWrite(greenPin, 255); 
analogWrite(bluePin, 255);
  }

void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
    keyPrevMillis = millis();
    
    byte currKeyState = digitalRead(keyPin);
    
    if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
        keyPress();
    }
    else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
        keyRelease();
    }
    else if (currKeyState == LOW) {
        longKeyPressCount++;
    }
    
    prevKeyState = currKeyState;
}
}
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
if (LEDstatus==1)
{
LEDcolor = LEDcolor +1;
if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
}
  }

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
  LEDstatus=1;Serial.println("On");
}
if (LEDstatus==1 && changed==0)
{
  analogWrite(redPin, 255);
  analogWrite(greenPin, 255); 
  analogWrite(bluePin, 255);
  LEDcolor=0;
  changed=1;
  LEDstatus=0;Serial.println("Off");
}
changed=0;
  }

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.println("key press");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
    Serial.println("key release");
    
    if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
}
else {
    shortKeyPress();
}
}

void setup() 
  {
Serial.begin(9600);
pinMode(keyPin, INPUT);
digitalWrite(keyPin, HIGH);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT); 
analogWrite(redPin, 255);
analogWrite(greenPin, 255); 
analogWrite(bluePin, 255);
  }

void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
    keyPrevMillis = millis();
    
    byte currKeyState = digitalRead(keyPin);
    
    if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
        keyPress();
    }
    else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
        keyRelease();
    }
    else if (currKeyState == LOW) {
        longKeyPressCount++;
    }
    
    prevKeyState = currKeyState;
}
}
Source Link

Changing how a longpress is executed

I have an RGB LED common Anode attached to my Uno and this code detects a long press to turn the LED on and off. Short presses cycle through colors, then off.

The problem is that a longpress is 2+sec, but it doesn't register until I release the button. How would I set this up to execute the code at 2sec regardless of whether the button is released?

It's probably not worth my time if it requres too much modification. I have a working item, it's just not... ideal.

unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int LEDcolor=0; // 0=White, 1=Red, 2=Green, 3-Blue, 4=Yellow, 5=Purple, 6=Pink, 7=Orange, 8=Off 
int LEDstatus =0; //0=Off, 1=On
int changed =0; //has the LEDstatus just changed?
byte longKeyPressCountMax = 80;    // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;

byte prevKeyState = HIGH;         // button is active low
const byte keyPin = 2;            // button is connected to pin 2 and GND

// called when button is kept pressed for less than 2 seconds
void shortKeyPress() 
  {
    Serial.println("short");
if (LEDstatus==1)
{
LEDcolor = LEDcolor +1;
if (LEDcolor==0) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 0);Serial.println("White");}
if (LEDcolor==1) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Red");}
if (LEDcolor==2) {analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Green");}
if (LEDcolor==3) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 0);Serial.println("Blue");}
if (LEDcolor==4) {analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);Serial.println("Yellow");}
if (LEDcolor==5) {analogWrite(redPin, 175);analogWrite(greenPin, 255);analogWrite(bluePin, 175);Serial.println("Purple");}
if (LEDcolor==6) {analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 155);Serial.println("Pink");}
if (LEDcolor==7) {analogWrite(redPin, 5);analogWrite(greenPin, 215);analogWrite(bluePin, 255);Serial.println("Orange");}
if (LEDcolor==8) {analogWrite(redPin, 255);analogWrite(greenPin, 255);analogWrite(bluePin, 255);Serial.println("Off");LEDstatus=0;}
}
  }

// called when button is kept pressed for more than 2 seconds
void longKeyPress() 
  {
    Serial.println("long");
    if (LEDstatus==0 && changed==0)
    {
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0); 
      analogWrite(bluePin, 0);
      LEDcolor=0;
      changed=1;
  LEDstatus=1;Serial.println("On");
}
if (LEDstatus==1 && changed==0)
{
  analogWrite(redPin, 255);
  analogWrite(greenPin, 255); 
  analogWrite(bluePin, 255);
  LEDcolor=0;
  changed=1;
  LEDstatus=0;Serial.println("Off");
}
changed=0;
  }

// called when key goes from not pressed to pressed
void keyPress() {
    Serial.println("key press");
    longKeyPressCount = 0;
}


// called when key goes from pressed to not pressed
void keyRelease() {
    Serial.println("key release");
    
    if (longKeyPressCount >= longKeyPressCountMax) {
        longKeyPress();
}
else {
    shortKeyPress();
}
}

void setup() 
  {
Serial.begin(9600);
pinMode(keyPin, INPUT);
digitalWrite(keyPin, HIGH);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT); 
analogWrite(redPin, 255);
analogWrite(greenPin, 255); 
analogWrite(bluePin, 255);
  }

void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
    keyPrevMillis = millis();
    
    byte currKeyState = digitalRead(keyPin);
    
    if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
        keyPress();
    }
    else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
        keyRelease();
    }
    else if (currKeyState == LOW) {
        longKeyPressCount++;
    }
    
    prevKeyState = currKeyState;
}
}