For Assignment 3 we had four different tasks, each a combination of code and hardware. In this post I will begin with an explanation, show my code, and then link to the video.
Part 1:
We were tasked with adding a second LED and make them flash like a railroad crossing. I did this using a delay in between the on and off for the individual pins but no delay in between the two pins.
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13, HIGH); // turn led on (high is voltage level)
delay(1000); // wait for 1 second
digitalWrite(13, LOW); // turn led off by making voltage low
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
}
Part 2:
Next I added a third LED, and did an airport landing strip. I found this to be fun and interesting, I definitely am wondering what else I could do and looking forward to learning more.
void setup() {
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // turn led on (high is voltage level)
delay(100); // wait for .1 second
digitalWrite(13, LOW); // turn led off by making voltage low
digitalWrite(11, HIGH);
delay(100);
digitalWrite(11, LOW);
digitalWrite(9, HIGH);
delay(100);
digitalWrite(9, LOW);
delay(2000);
}
Part 3:
Next, I changed the numbers in the delay to be a variable, rather than a hard coded number. The var variable increments by 400 each time the loop runs, and once it reaches 2000 it will reset and go back to 0. The video shows how it gets slower between the lights blinking and then eventually gets faster again.
int var = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // turn led on (high is voltage level)
delay(var); // wait for var
digitalWrite(13, LOW); // turn led off by making voltage low
digitalWrite(11, HIGH);
delay(var);
digitalWrite(11, LOW);
digitalWrite(9, HIGH);
delay(var);
digitalWrite(9, LOW);
if(var<2000){
var += 400;
}
else{
var = 0;
}
}
Part 4:
Finally, we used a fritzing diagram provided to us to make sure we understood how to orient the LEDs and the wires to make a more simple circuit. The LEDs also blink back and forth, similarly to part 1, but with much fewer wires and much less complicated. It uses the wires to switch back and forth between being an output wire or being a grounding wire.
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(700);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(700);
digitalWrite(12, LOW);
}
Leave a comment