i have to follow the promt above and create a code that follows its guidelines.
ID: 3754740 • Letter: I
Question
i have to follow the promt above and create a code that follows its guidelines. it must by in c and work with the arduino.
2. Whac-An-LED Name your program file lab6p2 Write a program that randomly turns on one of the four red LEDs above the pushbutton switches and waits until you press the corresponding switch. a. b. c. The four red LEDs are on pins,9, 6, and 3, corresponding to LEDO, LEDI d. Use function read switches) from a previous lab. The function reads in all e. Get a random number from 0 and 3 (explained below), telling you which red LED LED2, and LED3, respectively. Be sure to set the switch to RED LEDs four pushbutton switch values, inverts them, and encodes them into binary to turn on. Turn off the other 3 LEDs. Keep that LED on until the corresponding Page l EE112- Embedded Systems Lab 6 Fall 2018 switch (and only that switch) is pressed. Once it is pressed, get another random number and continue the game In order to generate a random number in Arduino C, call the function random (min,max), which returns a pseudo-random integer in the range of min to max (but not including max). f. g. Before you can call the function random ) you have to initialize it (in setup with what a "seed" value. You do this by calling the function randomSeed (seed), where seed also needs to be a little random. One idea is to read an analog input on a pin that is not used, e.g randomSeed (analogRead (0). However, on the Edushield, all of the analog pins are used. So, to make it more random, we will use the sum of readings from all of the analog pins (see below). Below is a code fragment that may be useful to you: h. int i,j; randomSeed analogRead (0) +analogRead (1) + + analogRead (5) 1 seed random number // generatorDO ONCIE i-random (0,100) // get random int from 0 to 99 jrandom (10,20) // get random int from 10 to 19Explanation / Answer
a. This can be done easily by available Arduino software.
b.
void randomLEDON()
{
digitalWrite(3, LOW);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
while(1){
int LedNum= random(0,3); // Get LED number to be turn ON
if(LedNum==0) digitalWrite(3, HIGH);
else if(LedNum==1) digitalWrite(6, HIGH);
if(LedNum==2) digitalWrite(9, HIGH);
if(LedNum==3) digitalWrite(11, HIGH);
// wait until any keypress happens on Push buttons
while(1){
if(s1||s2||s3||s4 == False) break;
}
}
}
c.
void setup() {
// initialize all LED pins as an output.
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(3, OUTPUT);
// Let us assume that Pin 2, 4, 7, 10 are the input pins according to LED0, LED1, LED2, LED3. so
// make them as input pins
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(7, INPUT);
pinMode(10, INPUT);
}
d.
void read_switches()
{
// Make sure s1, s2, s3 ,s4 variables are global variables as boolean variables, use this values further for your programming
s1= digitalRead(2);
s2= digitalRead(4);
s3= digitalRead(7);
s4= digitalRead(10);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.