Ks0436 keyestudio Ultimate Starter Kit For Little Inventor (Zero-based Arduino Learning Robot): Difference between revisions
Keyestudio (talk | contribs) |
Keyestudio (talk | contribs) |
||
Line 761: | Line 761: | ||
These switches have a nice, tactile, “clicky” feedback when you press them. <br> | These switches have a nice, tactile, “clicky” feedback when you press them. <br> | ||
Note that the different colors are just aesthetic. All of the buttons included behave the same, no matter their color. <br> | Note that the different colors are just aesthetic. All of the buttons included behave the same, no matter their color. <br> | ||
[[File:Ks0436-65.png| | [[File:Ks0436-65.png|500px]][[File:Ks0436-66.png|300px]] | ||
<br> | <br> | ||
Line 826: | Line 826: | ||
<br> | <br> | ||
<big>'''Think about:'''</big> <br> | <big>'''Think about:'''</big> <br> | ||
How to make responder effect? <br> | '''How to make responder effect?''' <br> | ||
Create your own exciting game. You can try to use three buttons to control three LEDs, one button used as reset button to make a responder. | Create your own exciting game. You can try to use three buttons to control three LEDs, one button used as reset button to make a responder. See what will happen?<br> | ||
See what will happen?<br> | [[File:Ks0436-71.png|500px]]<br> | ||
[[File:Ks0436-71.png| | |||
<br> | <br> | ||
Line 902: | Line 901: | ||
<br> | <br> | ||
[[File:Ks0436-72.png| | [[File:Ks0436-72.png|500px]]<br> | ||
Press the button to turn corresponding LED on and off. | Press the button to turn corresponding LED on and off. | ||
<br> | <br> | ||
<br> | <br> |
Revision as of 16:39, 12 July 2019
WELCOME TO THE KIT GUIDE
This is your map for navigating beginning embedded electronics.
This kit contains all the information you will need to build the circuit and robot projects for the keyestudio UNO Board.
When you’re done with this guide, you will have built five great projects and acquired the know-how to create countless more.
Now enough talk — let’s start something!
INTRODUCTION
The UNO Board Platform
The keyestudio UNO Board is your development platform.
The UNO Board is essentially a small, portable computer, also known as a microcontroller.
It is capable of taking inputs (such as the push of a button or a reading from a light sensor) and interpreting that information to control various outputs (like blinking an LED light or spinning an electric motor).
This board is capable of taking the world of electronics and relating it to the physical world in a real and tangible way.
The keyestudio UNO Board is a microcontroller board based on the ATmega328P microprocessor.
It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, 2 ICSP headers and a reset button.
It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it via an external DC power jack (DC 7-12V) or via female headers Vin/GND(DC 7-12V) to get started.
The Breadboard Platform
A breadboard is a circuit-building platform that allows you to connect multiple components without using a soldering iron.
This is a half-size transparent breadboard, good for small projects.
It has 2 power rails on both sides, a standard double-strip in the middle with 30 columns and 10 rows - a total of 400 tie in points.
This tiny breadboard also has a self-adhesive on the back, so you can stick it onto an Arduino protoshield or keyestudio chassis.
The Baseplate Assembly
Before you can build circuits, you’ll want to first assemble the breadboard baseplate.
This keyestudio baseplate makes circuit building easier by keeping the UNO Board microcontroller and the breadboard connected without the worry of disconnecting or damaging your circuit.
1)To begin with, collect your parts: the UNO Board, breadboard, screwdriver, keyestudio baseplate and four baseplate screws.
2)Align the UNO board with its spot on the baseplate.
The text on it should face the same direction as the text on the breadboard and the baseplate.
Using four screws, affix the UNO Board to the four stand-off holes found on the baseplate.
3)Peel the adhesive backing off the breadboard.
4)Carefully align the breadboard over its spot on the baseplate. The text on the breadboard should face the same direction as the text on the baseplate. Firmly press the breadboard to the baseplate to adhere it.
Your baseplate is now assembled!
The Arduino IDE
In order to get your UNO Board up and running.
you'll need to download the newest version of the Arduino software from www.arduino.cc (it's free!).
This software, known as the Arduino IDE (Integrated Development Environment), will allow you to program the UNO Board to do exactly what you want. It’s like a word processor for coding.
With an internet-capable computer, open up your favorite browser and type the following URL into the address bar to download the software of any versions:
https://www.arduino.cc/en/Main/OldSoftwareReleases#1.5.x
Open Arduino IDE
Downloaded the software package, unzip the folder package, double click the Arduino icon to open.
The functions of each button on the Toolbar are listed below:
Select Your Board And Serial Port
NOTE: select the Arduino UNO, but if you are not sure which ports should choose. Go to your computer panel, and check the Port out in the Device Manger.
The Parts List
The Kit contains extensive electronic components. Below shows you a part of kit components:
GET STARTED WITH CIRCUIT PROJECTS!
There are 20 circuit projects total. Each project will introduce new concepts and components, which will be described in more detail as you progress through the circuits.
As you work your way through each circuit, you will learn to use new and more complicated parts to accomplish increasingly complex tasks.
The project will set the foundation for the rest and will aid in helping you understand the fundamentals of circuit building and electricity!
Circuit 1: Blinking an LED
About this circuit:
Blinking an LED is the classic starting point for learning how to program embedded electronic components.
In this circuit, you’ll write code that makes an LED blink on and off.
What You Need:
- UNO Baseplate
- Red LED x 1
- 220Ω Resistor x 1
- Jumper wires x 2
- USB cable x 1
Component Introduction:
LIGHT-EMITTING DIODES (LEDS):
They come in different colors, brightnesses and sizes. LEDs have a positive (+) leg and a negative (-) leg, and they will only let electricity flow through them in one direction.
LEDs can also burn out if too much electricity flows through them, so you should always use a resistor to limit the current when you wire an LED into a circuit.
RESISTORS: resist the flow of electricity.
You can use them to protect sensitive components like LEDs. The strength of a resistor (measured in ohms) is marked on the body of the resistor using small colored bands.
Each color stands for a number, which you can look up using a resistor chart.
Wiring Diagram:
Check out the schematics and wiring diagram below to see how everything is connected.
With the 220Ω resistor in place, the LED should be quite bright.
If you swap out the 220Ω resistor for the 1kΩ resistor, then the LED will appear a little dimmer.
At the moment, you have Digital pin 10 going to one leg of the resistor, the other leg of the resistor going to the positive side of the LED and the other side of the LED going to GND.
Upload Code:
- 1)Connect the UNO Board to a USB port on your computer.
- 2)Open the program in the code folder- Circuit 1 LED, or directly copy and paste the code below on the Ardunio IDE.
- 3)Select UPLOAD to program the sketch on the UNO Board.
int ledPin = 10; // define digital pin 10. void setup() { pinMode(ledPin, OUTPUT);// define pin with LED connected as output. } void loop() { digitalWrite(ledPin, HIGH); // set the LED on. delay(1000); // wait for a second. digitalWrite(ledPin, LOW); // set the LED off. delay(1000); // wait for a second }
Code Explanation:
- Single line comments start with // and everything up until the end of that line is considered a comment.
Comments are a great way to leave notes in your code explaining why you wrote it the way you did.
- The first line of code is:
int ledPin = 10; // define digital pin 10
As the comment above it explains, this is giving a name to the pin that the LED is connected to. You can change the connection pin here.
- Next, we have the “setup” and “loop” function
void setup()
Every Arduino sketch must have a 'setup' function, and the place where you might want to add instructions of your own is between the { and the }.
In this case, there is just one command there, which, as the comment states tells the Arduino board that we are going to use the LED pin as an output.
void loop()
It is also mandatory for a sketch to have a “loop” function. Unlike the 'setup' function that only runs once, after a reset, the 'loop' function will, after it has finished running its commands, immediately start again.
- Input Or Output ?:
pinMode(ledPin, OUTPUT)
Before you can use one of the digital pins, you need to tell the UNO Board whether it is an INPUT or OUTPUT.
We use a built-in “function” called pinMode() to make pin 10 a digital output.
Digital Output:
digitalWrite(ledPin, HIGH)
digitalWrite(ledPin, LOW)
When you’re using a pin as an OUTPUT, you can command it to be HIGH (output 5 volts) or LOW (output 0 volts).
- What happens when you change the number in one or both of the delay(1000)
This delay period is in milliseconds, so if you want the LED to blink twice as fast, change the value, try 500 or 2000.
Upload the sketch again and you should see the LED start to blink more quickly or slowly.
After downloading this program, in the experiment, you will see the LED flashing on for one second, then off for one second.
The blinking LED experiment is now completed. Thank you!
Troubleshooting:
- Upload failed?
Make sure you have assembled the circuit correctly and verified and uploaded the code to your board. Check out the Board and Serial Port.
Circuit 2: Light Extension
About this circuit:
After mastering LED Blinking, let’s move on to how to control lots of LEDs. The principle is almost the same.
In this circuit, you’ll write code that makes six LEDs blink on and off one by one.
Furthermore, you can also use three LEDs to simulate the traffic light. Now let’s start something different!
What You Need:
- UNO Baseplate
- Red LED x 6
- 220Ω Resistor x 6
- Jumper wires x 7
- USB cable x 1
Wiring Diagram:
Check out the wiring below to see how everything is connected.
Upload Code:
- 1)Connect the UNO Board to a USB port on your computer.
- 2)Open the program in the code folder- Circuit 2 Light, or directly copy and paste the code below on the Ardunio IDE.
- 3)Select UPLOAD to program the sketch on the UNO Board.
int BASE = 2 ; // the I/O pin for the first LED int NUM = 6; // the number of LEDs void setup() { for (int i = BASE; i < BASE + NUM; i ++) { pinMode(i, OUTPUT); // set I/O pins as output } } void loop() { for (int i = BASE; i < BASE + NUM; i ++) { digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one by one. delay(200); // delay } for (int i = BASE; i < BASE + NUM; i ++) { digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one delay(200); // delay } }
Code Explanation:
- Single line comments start with // and everything up until the end of that line is considered a comment.
Comments are a great way to leave notes in your code explaining why you wrote it the way you did.
- int BASE = 2 ; // set the I/O pin for the first LED
- int NUM = 6; // the number of LEDs
You can see the six LEDs blink on and off one by one. It seems like light chasing.
If it doesn’t, make sure you have assembled the circuit correctly and verified and uploaded the code to your board.
Check out the Board and Serial Port.
Troubleshooting:
- Upload failed?
Make sure you have assembled the circuit correctly and verified and uploaded the code to your board. Check out the Board and Serial Port.
- LED Not Lighting Up?
Make sure you connect the LED correctly or try other LEDs.
How to make a traffic light?
What You Need:
- UNO Baseplate
- Red LED x 1
- Yellow LED x 1
- Green LED x 1
- 220Ω Resistor x 3
- Jumper wires x 4
- USB cable x 1
Hookup Guide:
Check out the hookup table below to see how everything is connected.
Schematics:
Source Code:
int redled =10; // initialize digital pin 8. int yellowled =7; // initialize digital pin 7. int greenled =4; // initialize digital pin 4. void setup() { pinMode(redled, OUTPUT);// set the pin with red LED as “output” pinMode(yellowled, OUTPUT); // set the pin with yellow LED as “output” pinMode(greenled, OUTPUT); // set the pin with green LED as “output” } void loop() { digitalWrite(greenled, HIGH);//// turn on green LED delay(5000);// wait 5 seconds digitalWrite(greenled, LOW); // turn off green LED for(int i=0;i<3;i++)// blinks for 3 times { delay(500);// wait 0.5 second digitalWrite(yellowled, HIGH);// turn on yellow LED delay(500);// wait 0.5 second digitalWrite(yellowled, LOW);// turn off yellow LED } delay(500);// wait 0.5 second digitalWrite(redled, HIGH);// turn on red LED delay(5000);// wait 5 second digitalWrite(redled, LOW);// turn off red LED }
<br<
Done uploading the code, you can see your own designed traffic light.
This circuit design is very similar with the one in LED chase effect.
The green light will turn on for 5 seconds, and then off, followed by the yellow light blinking for 3 times, and then red light turns on for 5 seconds, circularly and repeatedly.
Circuit 3: Potentiometer
About this circuit:
In the previous project, you have learned how to turn on or off an LED.
In this circuit, you’ll use a potentiometer as an input device to control the speed at which your LED blinks.
Here may involves a new concept PWM to control the brightness of an LED. Check more details below.
What You Need:
- UNO Baseplate
- Potentiometer x 1
- Red LED x 1
- 220Ω Resistor x 1
- Jumper wires x 6
- USB cable x 1
Component Introduction:
POTENTIOMETER:
Potentiometers (also known as“trimpots”or“knobs”) are one of the basic inputs for electronic devices.
A potentiometer is a 3-pin variable resistor.
When powered with 5V, the middle pin outputs a voltage between 0V and 5V, depending on the position of the knob on the potentiometer.
By tracking the position of the knob with your UNO Board, you can make volume controls, speed controls, angle sensors and a ton of other useful inputs for your projects.
PWM Control:
PWM is short for Pulse Width Modulation.
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave of different duty cycle, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off.
In the graphic, the green lines represent a regular time period.
This duration or period is the inverse of the PWM frequency.
In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each.
A call to analogWrite() is on a scale of 0-255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
To be simply, we all know that the voltage output of Arduino Digital port only has two states, LOW and HIGH, corresponding to the voltage output of 0V and 5V.
If merely make use of LOW and HIGH state, it cannot control the brightness of an LED light. However, if convert the voltage output of 0 Volts and 5 Volts into the value within 0-255, this way you can change the value within 0-255 to control the brightness of light.
The Arduino controller has totally 6 PWM outputs, which are Digital 3, 5, 6, 9, 10 and 11.
These pins can be used as Digital output or Analog output. If used as Analog output, it needs to call the analogWrite() function of ARDUINO, and this analogWrite() function can be controlled in the range of 0-255.
Hookup Guide:
The input of potentiometer is analog, so we connect it to analog port, and LED to PWM pin.
Different PWM signals can regulate the LED brightness.
Upload Code:
- 1)Connect the UNO Board to a USB port on your computer.
- 2)Open the program in the code folder- Circuit 3 Potentiometer, or directly copy and paste the code below on the Ardunio IDE.
- 3)Select UPLOAD to program the sketch on the UNO Board.
int potpin=0;// initialize analog pin 0 int ledpin=11;//initialize digital pin 11(PWM output) int val=0;// Temporarily store variables' value from the sensor void setup() { pinMode(ledpin,OUTPUT);// define digital pin 11 as “output” Serial.begin(9600);// set baud rate at 9600 // attention: for analog ports, they are automatically set up as “input” } void loop() { val=analogRead(potpin);// read the analog value from the sensor and assign it to val Serial.println(val);// display value of val analogWrite(ledpin,val/4);// turn on LED and set up brightness(maximum output of PWM is 255) delay(10);// wait for 0.01 second }
Code Explanation:
- Single line comments start with // and everything up until the end of that line is considered a comment.
Comments are a great way to leave notes in your code explaining why you wrote it the way you did.
- The first line of code is:
int potpin=0;// initialize analog pin 0
A variable is a placeholder for values that may change in your code. You must introduce, or“declare,”variables before you use them.
Here we’re declaring a variable called potPosition of type int (integer).
- Serial.begin(9600)
Serial commands can be used to send and receive data from your computer. Notice that the baud rate, 9600, is the same as the one we selected in the monitor. This is the speed at which the two devices communicate, and it must match on both sides.
- val=analogRead(potpin)
We use the analogRead() function to read the value on an analog pin.
- Serial.println(val);
This is the line that actually prints the trimpot value to the monitor.
What You Will See:
You should see the LED brighter or dim in accordance with your potentiometer.
You should see numeric values print out in the monitor. Turning the potentiometer changes the value.
Troubleshooting:
- Upload failed?
Make sure you have assembled the circuit correctly and verified and uploaded the code to your board. Ensure that you are on the correct Board and Serial Port.
- No values in Serial Monitor?
Make sure that you have selected the correct baud rate, 9600.
Circuit 4: Photo Resistor
About this circuit:
In the previous circuit, you have learned how to use a potentiometer as an input device to control the LED’s brightness.
In this circuit, you’ll be using a photoresistor, which changes resistance based on how much light the sensor receives.
Using this sensor you can make a simple night-light that turns on when the room gets dark and turns off when it is bright.
What You Need:
- UNO Baseplate
- Potentiometer x 1
- Red LED x 1
- 220Ω Resistor x 1
- 10KΩ Resistor x 1
- Jumper wires x 5
- USB cable x 1
Component Introduction:
Photo Resistor:
Photo resistor (Photovaristor) is a resistor whose resistance varies according to different incident light strength.
It's made based on the photoelectric effect of semiconductor.
If the incident light is intense, its resistance reduces; if the incident light is weak, the resistance increases.
Photo resistor is widely applied to various light control circuit, such as light control and adjustment, optical switches, etc.
Photovaristor is an element that changes its resistance as light strength changes. So we will need to read the analog values.
We can refer to the PWM experiment, replacing the potentiometer with photovaristor. When there is change in light strength, there will be corresponding change on the LED.
Hookup Guide:
The photoresistors, like regular resistors, are not polarized and can be installed in either direction.
Upload Code:
- 1)Connect the UNO Board to a USB port on your computer.
- 2)Open the program in the code folder- Circuit 4 Photoresistor, or directly copy and paste the code below on the Ardunio IDE.
- 3)Select UPLOAD to program the sketch on the UNO Board.
After the connection, let's begin the program compiling. The program is similar to the one of PWM.
For change details, please refer to the Sample Code below.
int potpin=0;// initialize analog pin 0, connected with photovaristor int ledpin=11;// initialize digital pin 11, output regulating the brightness of LED int val=0;// initialize variable val void setup() { pinMode(ledpin,OUTPUT);// set digital pin 11 as“output” Serial.begin(9600);// set baud rate at“9600” } void loop() { val= map(analogRead(potpin),0,1023,0,255); Serial.println(val);// display the value of val analogWrite(ledpin,val);// turn on the LED and set up brightness(maximum output value 255) delay(10);// wait for 0.01 }
What You Will See:
You can change the light intensity around the photovaristor and see corresponding brightness change of the LED.
See the light intensity on the serial monitor.
Circuit 5: Buzzer
About this circuit:
In this project, you will learn how to make tones with a buzzer.
What You Need:
- UNO Baseplate
- Passive Buzzer x 1
- Jumper wires x 2
- USB cable x 1
Component Introduction:
Passive Buzzer:
Buzzers can be categorized as active and passive ones.
The difference between the two is that an active buzzer has a built-in oscillating source, so it will generate a sound when electrified.
A passive buzzer does not have such a source, so DC signal cannot drive it beep. Different frequencies produce different sounds. By the buzzer, you can even play a song.
Hookup Guide:
Wiring the buzzer connected to the UNO board, the red (positive) to the pin8, black wire (negative) to the GND.
Upload Code:
- 1)Connect the UNO Board to a USB port on your computer.
- 2)Open the program in the code folder- Circuit 5 Buzzer, or directly copy and paste the code below on the Ardunio IDE.
- 3)Select UPLOAD to program the sketch on the UNO Board.
After the connection, let's begin the program compiling.
int buzzer=8;// select digital IO pin for the buzzer void setup() { pinMode(buzzer,OUTPUT);// set digital IO pin pattern, OUTPUT to be output } void loop() { unsigned char i,j;//define variable while(1) { for(i=0;i<80;i++)// output a frequency sound { digitalWrite(buzzer,HIGH);// sound delay(1);//delay1ms digitalWrite(buzzer,LOW);//not sound delay(1);//ms delay } for(i=0;i<100;i++)// output a frequency sound { digitalWrite(buzzer,HIGH);// sound digitalWrite(buzzer,LOW);//not sound delay(2);//2ms delay } } }
Test Reult:
Done uploading the code to the board, the passive buzzer will make a tone.
Troubleshooting:
- No sound is playing?
Check your wiring of the buzzer. It’s easy to misalign a pin with a jumper wire or reverse the buzzer.
The use method is almost the same.
Think about it and try to make an audible beep from active buzzer.
Test code is showed below:
int buzzer=2;// initialize digital IO pin that controls the buzzer void setup() { pinMode(buzzer,OUTPUT);// set pin mode as “output” } void loop() { digitalWrite(buzzer, HIGH); // produce sound }
From the test code, we can know the buzzer’s positive lead (long lead) is connected to Digital pin 2 of UNO R3. The short lead is connected to ground.
Troubleshooting:
- No sound playing?
Check your wiring of the buzzer. It’s easy to misalign a pin with a jumper wire or reverse the buzzer.
Circuit 6: Button
About this circuit:
Buttons are all around us, from the keys on your keyboard to the buttons on your remote control.
In this circuit, you’ll learn how to use the button to control the LED on and off using digital input.
What You Need:
- UNO Baseplate
- Button x 1
- Red LED x 1
- 220Ω Resistor x 1
- 10KΩ Resistor x 1
- Jumper wires x 5
- USB cable x 1
Component Introduction:
Push Button:
Also known as momentary switches, buttons only remain in their ON state as long as they’re being actuated, or pressed.
Most often momentary switches are best used for intermittent user-input cases: reset button and keypad buttons.
These switches have a nice, tactile, “clicky” feedback when you press them.
Note that the different colors are just aesthetic. All of the buttons included behave the same, no matter their color.
Hookup Guide:
See the wiring diagram below to connect everything.
Schematics:
Upload Code:
- 1)Connect the UNO Board to a USB port on your computer.
- 2)Open the program in the code folder- Circuit 6 Button, or directly copy and paste the code below on the Ardunio IDE.
- 3)Select UPLOAD to program the sketch on the UNO Board.
Now, let's begin the compiling. When the button is pressed, the LED will be on. After the previous study, the coding should be easy for you. In this program, we add a statement of judgment. Here, we use an if () statement.
Arduino IDE is based on C language, so statements of C language such as while, switch, can certainly be used for Arduino program.
When we press the button, pin 7 will output high level. We can program pin 11 to output high level and turn on the LED. When pin 7 outputs low level, pin 11 also outputs low level and the LED remains off.
int ledpin=11;// initialize pin 11 int inpin=7;// initialize pin 7 int val;// define val void setup() { pinMode(ledpin,OUTPUT);// set LED pin as “output” pinMode(inpin,INPUT);// set button pin as “input” } void loop() { val=digitalRead(inpin);// read the level value of pin 7 and assign if to val if(val==LOW)// check if the button is pressed, if yes, turn on the LED { digitalWrite(ledpin,LOW);} else { digitalWrite(ledpin,HIGH);} }
Code Explanation:
- Single line comments start with // and everything up until the end of that line is considered a comment.
Comments are a great way to leave notes in your code explaining why you wrote it the way you did.
- To declare a standard input, use the line pinMode(pin, INPUT);
digitalRead(pin);
Check to see if an input pin is reading HIGH (5V) or LOW (0V). Returns TRUE (1) or FALSE (0) depending on the reading.
What You Will See:
When the button is pressed, LED is on; otherwise, LED remains off.
After the above process, the button controlled LED experiment is completed.
The simple principle of this experiment is widely used in a variety of circuit and electric appliances.
Troubleshooting:
- The buttons are not working
First, make sure that the wiring is correct. It is easy to misalign a wire with a button leg. Second, make sure that you have declared your buttons as inputs.
Think about:
How to make responder effect?
Create your own exciting game. You can try to use three buttons to control three LEDs, one button used as reset button to make a responder. See what will happen?
Upload Code:
int redled=8; // set red LED as “output” int yellowled=7; // set yellow LED as “output” int greenled=6; // set green LED as “output” int redpin=5; // initialize pin for red button int yellowpin=4; // initialize pin for yellow button int greenpin=3; // initialize pin for green button int restpin=2; // initialize pin for reset button int red; int yellow; int green; void setup() { pinMode(redled,OUTPUT); pinMode(yellowled,OUTPUT); pinMode(greenled,OUTPUT); pinMode(redpin,INPUT); pinMode(yellowpin,INPUT); pinMode(greenpin,INPUT); } void loop() // repeatedly read pins for buttons { red=digitalRead(redpin); yellow=digitalRead(yellowpin); green=digitalRead(greenpin); if(red==LOW)RED_YES(); if(yellow==LOW)YELLOW_YES(); if(green==LOW)GREEN_YES(); } void RED_YES()// execute the code until red light is on; end cycle when reset button is pressed { while(digitalRead(restpin)==1) { digitalWrite(redled,HIGH); digitalWrite(greenled,LOW); digitalWrite(yellowled,LOW); } clear_led(); } void YELLOW_YES()// execute the code until yellow light is on; end cycle when reset button is pressed { while(digitalRead(restpin)==1) { digitalWrite(redled,LOW); digitalWrite(greenled,LOW); digitalWrite(yellowled,HIGH); } clear_led(); } void GREEN_YES()// execute the code until green light is on; end cycle when reset button is pressed { while(digitalRead(restpin)==1) { digitalWrite(redled,LOW); digitalWrite(greenled,HIGH); digitalWrite(yellowled,LOW); } clear_led(); } void clear_led()// all LED off { digitalWrite(redled,LOW); digitalWrite(greenled,LOW); digitalWrite(yellowled,LOW); }
Press the button to turn corresponding LED on and off.