u ll 112 10 Stay off for another second, we write another delay for this: delay
ID: 3750899 • Letter: U
Question
u ll 112 10 Stay off for another second, we write another delay for this: delay (1000) Putting everything together, the code looks like this: int LEDpin 13; void setup) pinMode (LEDpin, oUTpuT); void loop) digitalwrite (LEDpin, HIGH); delay (1000) digitalWrite (LEDpin, LOW) delay (1000) Exercisel: Write a sketch that makes an LED on digital pin9 behave like this: turns on and stays in that state for half a second, then off and stays in the state for 2 seconds. Exercise2: Write a sketch that makes 3 LEDs on digital pin5, pinó, and pin7 respectively blink in sequence, starting with the LED on pin5. Each blink should consist of a second in the "on" state, and another second in the "off" state.Explanation / Answer
Answer : From the question it is not very clear if the requirement is to blink the LEDs only in the forward direction ie 5,6 and then 7. If this is the only requirement then the 2nd loop in the piece of code ie the loop for cycling back from 7 to 6 to 5 is not required. If the requirement is to cycle back and forth then both the loops are needed. I have provide the solution which caters to all the requirement. You can consider the entire code or the code minus the 2nd loop if only blinking in forward direction is needed.
//Assume that the 3 LEDs are connected to pins 5,6 and 7
int duration=1000 ; // set up delay for 1000 ms ie 1 sec
void setup() {
//initialize for output LED legs to be connected to GND on one hand and pins 5,6 and 7 sequentially in a loop
for (int PinIndex =5;PinIndex<=7;PinIndex++)
{ pinMode(PinIndex,OUTPUT);
}
}
Void loop() {
// looping for LED blink starting from Pin 5 to pin 7
for (int PinIndex =5;PinIndex<=7;PinIndex++) {
digitalWrite(PinIndex,HIGH); // Apply High voltage to the leg of the LED connected to pin=PinIndex & turn LED on
delay(duration); // set delay for 1 sec
digitalWrite(PinIndex,LOW); // Apply low voltage to the leg of the LED connected to pin=PinIndex & turn LED off
delay(duration); // set delay for 1 sec
}
// Only If it is needed to cycle back and forth for LED blinking for Pins 5,6 and 7 then the following code can be added to achieve the objective. This loop is to blink LEDs from pins 7 to 5
For(int PinIndex =7;PinIndex>=5;PinIndex--) {
digitalWrite(PinIndex,HIGH);
delay(duration);
digitalWrite(PinIndex,LOW);
delay(duration);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.