I want to press hold button switch to change pattern of LED but I can,t coding. When I long press button the LED not loops.Please help!!

:woozy_face:
int buttonStatePrevious = LOW;
unsigned long minButtonLongPressDuration = 500;
unsigned long buttonLongPressMillis;
bool buttonStateLongPress = false;
const int intervalButton = 50;
unsigned long previousButtonMillis;
unsigned long buttonPressDuration;

int R = 11;
int G = 10;
int B = 9;
int RR = 6;
int GG = 5;
int BB = 3;
int buttonPin = 2;
unsigned long currentMillis;
int old = 0;
int old_1 = 0;
int state = 0;
int state_1 = 0;

void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(R, OUTPUT); // set arduino pin to output mode
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(RR, OUTPUT); // set arduino pin to output mode
pinMode(GG, OUTPUT);
pinMode(BB, OUTPUT);

}

void loop() {
currentMillis = millis();
if(currentMillis - previousButtonMillis > intervalButton) {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
buttonLongPressMillis = currentMillis;
buttonStatePrevious = HIGH;
}
buttonPressDuration = currentMillis - buttonLongPressMillis;
if(buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
buttonStateLongPress = true;
state_1 = old_1 + 1;
while(state_1){
analogWrite(RR, 255);
analogWrite(GG, 255);
analogWrite(BB, 0);
analogWrite(R, 0);
analogWrite(G, 255);
analogWrite(B, 255);
delay(200);
analogWrite(RR, 255);
analogWrite(GG, 255);
analogWrite(BB, 255);
analogWrite(R, 255);
analogWrite(G, 255);
analogWrite(B, 255);
delay(500);
analogWrite(RR, 0);
analogWrite(GG, 255);
analogWrite(BB, 255);
analogWrite(R, 255);
analogWrite(G, 255);
analogWrite(B, 0);
delay(200);
old_1 = state_1 ;
}
}
if (buttonState == LOW && buttonStatePrevious == HIGH) {
buttonStatePrevious = LOW;
buttonStateLongPress = false;
if (!buttonStateLongPress && buttonPressDuration < minButtonLongPressDuration) {
state = old + 1;
Serial.println(state);

        switch(state){
      case 1:
      analogWrite(RR, 0); 
      analogWrite(GG, 255);
      analogWrite(BB, 255);
      analogWrite(R, 0); 
      analogWrite(G, 255);
      analogWrite(B, 255);
      old = state;
      break;
        case 2:
      analogWrite(RR, 255); 
analogWrite(GG, 0);
analogWrite(BB, 255); 
analogWrite(R, 255); 
analogWrite(G, 0);
analogWrite(B, 255);
      old = state;
      break;        
        case 3:
       analogWrite(RR, 255); 
analogWrite(GG, 255);
analogWrite(BB, 0); 
analogWrite(R, 255); 
analogWrite(G, 255);
analogWrite(B, 0);
      old = state;
      break;
        default:
      analogWrite(GG, 255);
analogWrite(BB, 255);
analogWrite(R, 255); 
analogWrite(G, 255);
analogWrite(B, 255);                    
   old =0;
      break;
    }
  }      
}
previousButtonMillis = currentMillis;

}
}

1 Like

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

1 Like
int buttonStatePrevious = LOW;                   // สถานะก่อนกดปุ่ม
unsigned long minButtonLongPressDuration = 500;  // เวลาเป็นมิลลิวินาทีจนกว่าจะแสดงว่า "กดแบบยาว"
unsigned long buttonLongPressMillis;             // เวลาเป็น ms เมื่อกดปุ่ม
bool buttonStateLongPress = false;               // กดปุ่มเป็นเวลานาน หากใช่ จะเป็น true
const int intervalButton = 50;                   // เวลาเป็นมิลลิวินาทีระหว่างอ่านปุ่มกด
unsigned long previousButtonMillis;              // เวลาที่อ่านข้อมูลล่าสุดคือปุ่มกด
unsigned long buttonPressDuration;               // เวลากดปุ่มกดเป็นมิลลิวินาที

int R = 11;
int G = 10;
int B = 9;
int RR = 6;
int GG = 5;
int BB = 3;
int buttonPin = 2;
unsigned long currentMillis;  // วินาทีนับตั้งแต่ Arduino เริ่มต้น
int old = 0;
int old_1 = 0;
int state = 0;
int state_1 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(R, OUTPUT);  // set arduino pin to output mode
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(RR, OUTPUT);  // set arduino pin to output mode
  pinMode(GG, OUTPUT);
  pinMode(BB, OUTPUT);
}

void loop() {
  currentMillis = millis();
  if (currentMillis - previousButtonMillis > intervalButton) {
    int buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
      buttonLongPressMillis = currentMillis;
      buttonStatePrevious = HIGH;
    }
    buttonPressDuration = currentMillis - buttonLongPressMillis;
    if (buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
      buttonStateLongPress = true;
      state_1 = old_1 + 1;
      analogWrite(RR, 255);
      analogWrite(GG, 255);
      analogWrite(BB, 0);
      analogWrite(R, 0);
      analogWrite(G, 255);
      analogWrite(B, 255);
      delay(200);
      analogWrite(RR, 255);
      analogWrite(GG, 255);
      analogWrite(BB, 255);
      analogWrite(R, 255);
      analogWrite(G, 255);
      analogWrite(B, 255);
      delay(500);
      analogWrite(RR, 0);
      analogWrite(GG, 255);
      analogWrite(BB, 255);
      analogWrite(R, 255);
      analogWrite(G, 255);
      analogWrite(B, 0);
      delay(200);
    }
    old_1 = state_1;
    if (buttonState == LOW && buttonStatePrevious == HIGH) {
      buttonStatePrevious = LOW;
      buttonStateLongPress = false;
      if (!buttonStateLongPress && buttonPressDuration < minButtonLongPressDuration) {
        state = old + 1;
        Serial.println(state);

        switch (state) {
          case 1:
            analogWrite(RR, 0);
            analogWrite(GG, 255);
            analogWrite(BB, 255);
            analogWrite(R, 0);
            analogWrite(G, 255);
            analogWrite(B, 255);
            old = state;
            break;
          case 2:
            analogWrite(RR, 255);
            analogWrite(GG, 0);
            analogWrite(BB, 255);
            analogWrite(R, 255);
            analogWrite(G, 0);
            analogWrite(B, 255);
            old = state;
            break;
          case 3:
            analogWrite(RR, 255);
            analogWrite(GG, 255);
            analogWrite(BB, 0);
            analogWrite(R, 255);
            analogWrite(G, 255);
            analogWrite(B, 0);
            old = state;
            break;
          default:
            analogWrite(GG, 255);
            analogWrite(BB, 255);
            analogWrite(R, 255);
            analogWrite(G, 255);
            analogWrite(B, 255);
            old = 0;
            break;
        }
      }
    }
    previousButtonMillis = currentMillis;
  }
}

Thank you for properly posting the sketch. :slightly_smiling_face:


Following the forum rules is eventual so we can better help you.


What Arduino do you have ?

We also require you to post a schematic of your circuit and a good image of your wiring.

Links to the hardware is also needed.


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.