CPP 216
Arduino LED Guest on 27th January 2024 06:53:02 AM
  1.  
  2. // Muhammadi
  3. #define button 2
  4. #define led 3
  5. void setup() {
  6.   Serial.begin(9600);
  7.   pinMode(button, INPUT_PULLUP);  
  8. }
  9. bool flag = 0;
  10. int act = 0;
  11. int brightness = 255;
  12. void loop() {
  13.   if(digitalRead(button) == 0){
  14.     if(flag == 1){
  15.       int time = millis();
  16.       act = time;
  17.       while(digitalRead(button) == 0){
  18.         if(millis() - time > 400){
  19.           Serial.print(brightness);
  20.           time = millis();
  21.           if(brightness > 40){
  22.             brightness -= 40;
  23.           }
  24.           analogWrite(led, brightness);
  25.         }
  26.       }
  27.       if(millis() - act < 400){
  28.         analogWrite(led, 0);
  29.         flag = 0;
  30.       }
  31.     }
  32.     else{
  33.       while(digitalRead(button) == 0){}
  34.       brightness = 255;
  35.       flag = 1;
  36.       analogWrite(led, 255);
  37.     }
  38.   }
  39. }

Hightechrobo bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.