Problem l: Using serial monitor to control how LEDs flash. Build a circuit and w
ID: 3727906 • Letter: P
Question
Problem l: Using serial monitor to control how LEDs flash. Build a circuit and write an Arduino program to do the following: 1. The system requires a green LED, a yellow LED, a red LED, a push button and other components 2. When the push button is pressed the first time, the system will be turned ON. The serial monitor is 3. Display a prompt on the serial monitor for a random number with two digits in the form of 1*, 2* or 4. The first number (1, 2 or 3) control which LED is flashing, while the second number) control the ready to receive numerical input. Display this status on serial monitor. number of flashing. If the number is 1* (for example 14), the green LED flash times. If the number is 2* (for example 23), the yellow LED flash times. If the number is 3* (for example 35), the red LED flash * times. The flashing frequency is 2Hz. If the number is not 1*, 2* or 3*, display invalid status and prompt for new number. At any time during the operation, if the push button is pressed the second time, the system will be turned OFF (to step 2). 5. Sketch the circuit.Explanation / Answer
int pushPin=7; // input pin for push button
int pinVal=0; // variable to read pin status
long rnum; // random number
int count=0; // variable for flashing count
void setup() {
// declare pin 11,12,13 to be outputs:
pinMode(11, OUTPUT); // for green led at pin 11
pinMode(12, OUTPUT); // for yellow led at pin 12
pinMode(13, OUTPUT); // for red led at pin 13
pinMode(pushPin, INPUT); // declare pushbutton as input
Serial.begin(9600); // starts serial communication
}
void loop(){
pinVal = digitalRead(pushPin); // read input value
if (pinVal == LOW) { // check if the input is LOW (button pressed)
rnum = random(10, 40); // generates random numbers from 10 to 39
Serial.println(rnum); // displays random number
if(rnum>=10 && rnum<20)
{
count = rnum % 10;
for(int i=0;i<count;i++)
{
digitalWrite(11, HIGH);
delay(500); // time = 1/ frequency , so for frequnecy 2Hz we need delay of 500 ms
digitalWrite(11,LOW);
delay(500);
}
}
else if(rnum>=20 && rnum<30)
{
count = rnum % 10;
for(int i=0;i<count;i++)
{
digitalWrite(12, HIGH);
delay(500); // time = 1/ frequency , so for frequnecy 2Hz we need delay of 500 ms
digitalWrite(12,LOW);
delay(500);
}
}
else if(rnum >=30 && rnum<40)
{
count = rnum % 10;
for(int i=0;i<count;i++)
{
digitalWrite(13, HIGH);
delay(500); // time = 1/ frequency , so for frequnecy 2Hz we need delay of 500 ms
digitalWrite(13,LOW);
delay(500);
}
}
else
{
Serial.println("Invalid status");
}
}
}
void setup() {
// declare pin 11,12,13 to be outputs:
pinMode(11, OUTPUT); // for green led at pin 11
pinMode(12, OUTPUT); // for yellow led at pin 12
pinMode(13, OUTPUT); // for red led at pin 13
pinMode(07, INPUT); // declare pushbutton as input at pin 07
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.