We have published several simple articles on Arduino, LED and pushbuttons. They are a great learning tool for the children, as well as great “templates” for the adults to start a new project. Here is a list of some of the old articles:
- Arduino 2 Push Button One LED: Switch On/Off
- Make Beep Sound in Arduino Project Upon Push Button Press
- Arduino Blink LED Rate Depending On Push Button Press Duration
- Arduino: Blink LED and Beep Every X Seconds Upon Push Button Press
- Arduino Flip-Flop Blinking LED With Push Button
- Arduino Blink LED With Pushbutton Control to Turn ON and Off
- One Push Button Multiple Functions (Single Press, Double Press, Long-Time Press)
- Arduino 3 LED and One Push Button
In this article’s code, the LED will start blinking on its own. When we will press the push button it will pause or start the blink. We have added a potentiometer which will set the delay()
for the blink.
I found that Zeynep Öztürk already created a similar project and the simulation is available here on Tinkercad.
---
I have forked it here. The forked version is slightly different. You can try it here by first clicking the “Simulate” button, then clicking the push button:
Here is the 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 | int led = 11; // led connected to 11th port int button = 7;// button connected to 7th port int pot =0;// potentiometer connected to A0 int potValue; int buttonStatus = 0; int ledStatus = 0; void setup() { pinMode(led, OUTPUT);// led set to output pinMode(button,INPUT);//button set to input Serial.begin(9600); digitalWrite(led, LOW);// led set to off mode } void loop() { buttonStatus = digitalRead(button); //reads the value from the button delay(100); // wait 10 milliseconds if(buttonStatus == 1) // if you pressed the button { if(ledStatus == 0) // if led status is in off mode { ledStatus = 1;// make led status is on mode } else { digitalWrite(led,LOW); ledStatus = 0; } } if(ledStatus == 1)//if led status is in on mode { potValue = analogRead(A0)*10;//The potentiometer value and analog readout were equaled. //multiplied by 10 to convert to seconds Serial.println(potValue/1000);//divided by 1000 so that the value given on the serial monitor is in seconds. digitalWrite(led, HIGH);// led turns on delay(potValue);//The led stays on for as many seconds as the pot value. digitalWrite(led, LOW);// led turns off delay(potValue);//The led stays off for as many seconds as the pot value. } } |