Introduction: Fancy Blinking LEDs Using Arduino
This is fancy light generator which is built using Arduino . This project uses four LEDs and controlled using a microswitch(button switch) which trigger them to glow LEDs in different sequence and patterns....
Uno is one of the series of USB Arduino boards. The Arduino Uno is a microcontroller board based on the ATmega328. It has 14 digital input/output pins (of which six can be used as PWM outputs), six analogue inputs, a 16MHz crystal oscillator, a USB connection, a power jack, an ICSP (in-circuit serial programming) header and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer through a USB cable or power it with a 9V DC adaptor (or 9V battery) to get started.
Sequence....
On--All off
First--All on
Second--Flashing
Third--waving
Step 1: Components Required
*4 LEDs (Any colour)
*Micro switch(button switch)
*wires
*Power supply(for arduino)
*bread board (if needed)
Step 2: Rig Up the Ckt
Resistors of 220ohm can be used to limit the current through LEDs...
resistor is very much needed between the ground and pin no 2....(10k is enough).
Step 3: Coding
//By AP//
int switchPin = 2; // mode switch is connected to pin 2
int led1Pin = 12;
int led2Pin = 11;
int led3Pin = 10;
int led4Pin = 9;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the mode switch state
int lightMode = 0;
void setup() {
pinMode(switchPin, INPUT); // Set the mode switch pin as input
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10);
val2 = digitalRead(switchPin); // Key Bounce Prevention
if (val == val2) {
if (val != buttonState) {
if (val == HIGH) { // check if the mode button is pressed
if (lightMode == 0) {
lightMode = 1; // turn LEDs on
}
else {
if (lightMode == 1) { // if LEDs on
lightMode = 2; // make it flash
}
else {
if (lightMode == 2) { // if LEDs flashing
lightMode = 3; // make it wave
}
else {
if (lightMode == 3) { // if LEDs waving,
lightMode = 0; // turn LEDs off
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (lightMode == 0) { // LEDs-off
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
}
if (lightMode == 1) { // LEDs-on
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
}
if (lightMode == 2) { // LEDS-flashing
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
delay(100);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
delay(100);
}
if (lightMode == 3) { // LEDs-waving
digitalWrite(led4Pin, LOW);
digitalWrite(led1Pin, HIGH);
delay(100);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(100);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay(100);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, HIGH);
delay(100);
digitalWrite(led4Pin, LOW);
}
}