Previously we published some Arduino guides which use the millis
function. Arduino 2 LED was a basic project, Arduino Flip-Flop Blinking LED With Push Button is somewhat advanced project. Others were One Push Button Multiple Functions supporting Single Press, Double Press, Long-Time Press, Arduino Blink LED Rate Depending On Push Button Press Duration etc. In this guide we will use 3 LEDs and one push button. For this project, we will need :
- One Arduino UNO or simillar board
- 3 LEDs
- 1 Pushbutton
- 3 x 220 Ohm value resistors
- 1 x 1K Ohm value resistor
- 1 Breadboard
- Few jumper wires
220 Ohm value resistors are for the 3 LEDS, 1K Ohm value resistor is for the push button. We will add the pushbutton on pin 7, first LED1 on pin 8, second LED on pin 9 and third LED on pin 10 of Arduino board. This will be the connection :
Following oficial sources of Arduino are used in this sample example code :
---
1 2 3 4 5 | # https://playground.arduino.cc/Code/Enum https://playground.arduino.cc/French/SwitchCase https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/ # |
Here is the complete code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | // Pin assignement #define btnPin A0 #define led1Pin 8 #define led2Pin 9 #define led3Pin 10 enum fcnMode { OFF, LED1, LED2, LED3, FADE1, ALL, BLINK, NBSTATE }; // OFF = 0 and NBSTATE=7 int ledState1 = LOW,ledState2 = LOW,ledState3 = LOW; // ledState used to set the LED unsigned long buttonState = 0; int funcState=0; unsigned long currentMillis1,currentMillis2,currentMillis3; // will store current time unsigned long previousMillis1,previousMillis2,previousMillis3; // will store last time LED was updated const long interval1 = 100; // interval at which to blink (milliseconds) const long interval2 = 300; const long interval3 = 500; /******************************************************************\ * PRIVATE FUNCTION: setup * * PARAMETERS: * ~ void * * RETURN: * ~ void * * DESCRIPTIONS: * Initiate inputs/outputs * \******************************************************************/ void setup(){ Serial.begin(9600); // initialize serial port pinMode(btnPin,INPUT_PULLUP); pinMode(led1Pin,OUTPUT); pinMode(led2Pin,OUTPUT); pinMode(led3Pin,OUTPUT); } /******************************************************************\ * PRIVATE FUNCTION: loop * * PARAMETERS: * ~ void * * RETURN: * ~ void * * DESCRIPTIONS: * Main Function of the code \******************************************************************/ void loop(){ buttonPressed(); setMode(); } /****************************************************************** * SUBFUNCTIONS \******************************************************************/ void buttonPressed() { buttonState = pulseIn(btnPin,HIGH,1000000); if (buttonState > 50){ funcState += 1; Serial.print("Button state n: "); Serial.println(funcState); } funcState=funcState%NBSTATE; } void setMode() { // All Off digitalWrite(led1Pin,LOW); digitalWrite(led2Pin,LOW); digitalWrite(led3Pin,LOW); Serial.print("Function : "); Serial.println(funcState); switch(funcState){ case OFF: break; case LED1: digitalWrite(led1Pin,HIGH); break; case LED2: digitalWrite(led2Pin,HIGH); break; case LED3: digitalWrite(led3Pin,HIGH); break; case FADE1: fade1(); break; case ALL: digitalWrite(led1Pin,HIGH); digitalWrite(led2Pin,HIGH); digitalWrite(led3Pin,HIGH); break; case BLINK: blinkLed1(); blinkLed2(); blinkLed3(); break; } } void fade1(){ int brightness = 0; int fadeAmount = 5; for (brightness=0;brightness<=255;brightness+=fadeAmount){ analogWrite(led1Pin, brightness); delay(30); } for (brightness=255;brightness>=0;brightness-=fadeAmount){ analogWrite(led1Pin, brightness); delay(30); } } void blinkLed1(){ currentMillis1 = millis(); if (currentMillis1 - previousMillis1 >= interval1) { // save the last time you blinked the LED previousMillis1 = currentMillis1; // if the LED is off turn it on and vice-versa: if (ledState1 == LOW) { ledState1 = HIGH; } else { ledState1 = LOW; } // set the LED with the ledState of the variable: digitalWrite(led1Pin, ledState1); } } void blinkLed2(){ currentMillis2 = millis(); if (currentMillis2 - previousMillis2 >= interval2) { // save the last time you blinked the LED previousMillis2 = currentMillis2; // if the LED is off turn it on and vice-versa: if (ledState2 == LOW) { ledState2 = HIGH; } else { ledState2 = LOW; } // set the LED with the ledState of the variable: digitalWrite(led2Pin, ledState2); } } void blinkLed3(){ currentMillis3 = millis(); if (currentMillis3 - previousMillis3 >= interval3) { // save the last time you blinked the LED previousMillis3 = currentMillis3; // if the LED is off turn it on and vice-versa: if (ledState3 == LOW) { ledState3 = HIGH; } else { ledState3 = LOW; } // set the LED with the ledState of the variable: digitalWrite(led3Pin, ledState3); } } |
This project is shared by aranacorp.com/en/arana-shop/
. We lack copyright on diagram or code.