How to build and control a series of LEDs on an Arduino with a switch button:
by J.B. Wylzan
Project 11: The Switch
LEDs
resistors
switch button
wires
breadboard
computer cable
Arduino UNO
Block Diagram:
image designed by Fritzing |
Actual Assembly:
Procedure:
1. Use the same prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #11 below
5. Paste Code #11
6. Click File > Save
7. Click Verify
8. Click Upload
9. Activate Project Breaker by pressing a switch
Code # 11:
/*
iHackLab The Switch
powered by Arduino
sketched by J.B. Wylzan
modified by Lawsinium
Executes Project Breaker when a switch is pressed
Turns On Led (#8) when switch is released.
Initializes values on certain variables.
This example code is public domain. */
//declare variables/constants
int y = 1000; // y is a variable with a certain value = 1000
int z; // z is a variable with uncertain value
void setup() {
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
z = digitalRead(3); // input action: Press or Release ?
if (z == 0) // if button is in release mode; z == 0
{
digitalWrite(8, HIGH); // led#8 turns ON
}
else // if button is pressed; z == 1
{
digitalWrite(8, LOW); // then led#8 turns OFF
// **project Breaker starts**
digitalWrite(7, HIGH); // turn the LED on
digitalWrite(9, HIGH); // turn the LED on
delay(y);
digitalWrite(7, LOW); // turn the LED off
digitalWrite(9, LOW); // turn the LED off
digitalWrite(6, HIGH); // turn the LED on
digitalWrite(10, HIGH); // turn the LED on
delay(y);
digitalWrite(6, LOW); // turn the LED off
digitalWrite(10, LOW); // turn the LED off
digitalWrite(5, HIGH); // turn the LED on
digitalWrite(11, HIGH); // turn the LED on
delay(y);
digitalWrite(5, LOW); // turn the LED off
digitalWrite(11, LOW); // turn the LED off
digitalWrite(4, HIGH); // turn the LED on
digitalWrite(12, HIGH); // turn the LED on
delay(y);
digitalWrite(4, LOW); // turn the LED off
digitalWrite(12, LOW); // turn the LED off
// **procedure Breaker ends**
}
}
iHackLab The Switch
powered by Arduino
sketched by J.B. Wylzan
modified by Lawsinium
Executes Project Breaker when a switch is pressed
Turns On Led (#8) when switch is released.
Initializes values on certain variables.
This example code is public domain. */
//declare variables/constants
int y = 1000; // y is a variable with a certain value = 1000
int z; // z is a variable with uncertain value
void setup() {
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
z = digitalRead(3); // input action: Press or Release ?
if (z == 0) // if button is in release mode; z == 0
{
digitalWrite(8, HIGH); // led#8 turns ON
}
else // if button is pressed; z == 1
{
digitalWrite(8, LOW); // then led#8 turns OFF
// **project Breaker starts**
digitalWrite(7, HIGH); // turn the LED on
digitalWrite(9, HIGH); // turn the LED on
delay(y);
digitalWrite(7, LOW); // turn the LED off
digitalWrite(9, LOW); // turn the LED off
digitalWrite(6, HIGH); // turn the LED on
digitalWrite(10, HIGH); // turn the LED on
delay(y);
digitalWrite(6, LOW); // turn the LED off
digitalWrite(10, LOW); // turn the LED off
digitalWrite(5, HIGH); // turn the LED on
digitalWrite(11, HIGH); // turn the LED on
delay(y);
digitalWrite(5, LOW); // turn the LED off
digitalWrite(11, LOW); // turn the LED off
digitalWrite(4, HIGH); // turn the LED on
digitalWrite(12, HIGH); // turn the LED on
delay(y);
digitalWrite(4, LOW); // turn the LED off
digitalWrite(12, LOW); // turn the LED off
// **procedure Breaker ends**
}
}
/* End of Program */
Serial Monitor Test:
//replace the button with any sensor e.g. motion sensor
byte sensorPin = 3;
byte ledPin= 13;
void setup()
{
pinMode(sensorPin,INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
byte state = digitalRead(sensorPin);
digitalWrite(ledPin,state);
if(state == 1)Serial.println("Warning! Intruder!!!!");
else if(state == 0)Serial.println("No one!");
delay(500);
}
Project # 12: The Game of Reflex
Disclaimer: We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email.
Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.
==================================================================
Patent Pending. 2000 © ®
A L.A.W.S.I.N. Educational Production
No comments:
Post a Comment