• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Arduino 3 LED and One Push Button

By Abhishek Ghosh December 27, 2018 10:50 am Updated on December 27, 2018

Arduino 3 LED and One Push Button

Advertisement

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 :

  1. One Arduino UNO or simillar board
  2. 3 LEDs
  3. 1 Pushbutton
  4. 3 x 220 Ohm value resistors
  5. 1 x 1K Ohm value resistor
  6. 1 Breadboard
  7. 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 :

Arduino 3 LED and One Push Button

Following oficial sources of Arduino are used in this sample example code :

Advertisement

---

Vim
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 :

Vim
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.

Tagged With arduino 3 led , Arduino 1 button 12 leds , turn on 3 led arduino with push button , btnpin = 1; , arduino with 2 push button AND 3 LED code , 3 action push button arduino , push buttons with an led on arduino , arduino 2 led with one pushbutton , 3 leds with arduino and one pus button , 3 led 1 button arduino

This Article Has Been Shared 438 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Arduino 3 LED and One Push Button

  • Arduino UNO Prototype Shield Tutorial

    In Arduino UNO Prototype Shield Tutorial We Have Talked How We Can Use to Convert a Project Semi-Permanent With Shield, Components & Solder.

  • How to Increase the Number of Digital Pins in Arduino (Port Extender)

    13+6 Pins Often a Limitation of Arduino in Projects Where We Need Many Pins. Here is How to Increase the Number of Digital Pins in Arduino.

  • Connecting Arduino With M029 JoyStick : Getting Started

    Here is a Getting Started Guide Around Connecting Arduino With M029 JoyStick and Testing With Basic Code. It is a useful thing for robotics.

  • Create an Arduino LDR Module With Transistor

    We Can Create an Arduino LDR Module With Transistor in Easy Way to Print Response as Either Zero or One.

  • Arduino 14 Segment LED Display : Driver IC & Library

    Here is Information About Arduino 14 Segment LED Display Driver IC & Library. As it is less used component for higher price, possibly you have build module yourself.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (22.1K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Safe Chargers for Samsung Galaxy S22 Ultra June 27, 2022
  • How Telecoms Can Use The Cloud To Power Their 5G Network June 24, 2022
  • A Beginner Guide to Cloud Computing for Development June 22, 2022
  • 5 Benefits of Using a Virtual Data Room Today June 19, 2022
  • Top System Administration Courses 2022 June 18, 2022

About This Article

Cite this article as: Abhishek Ghosh, "Arduino 3 LED and One Push Button," in The Customize Windows, December 27, 2018, June 28, 2022, https://thecustomizewindows.com/2018/12/arduino-3-led-and-one-push-button/.

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2022 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy