02 / HW : Knight Rider
- Feb 1, 2016
- 4 min read
/ Write the program to blink the LEDs in a simple Cylon/Larson/Knight-rider pattern.
/ Make the potentiometer control the speed of the pattern
/ Make the switch change to a different pattern (any pattern).
A / Connect 5 LEDs to the circuit
I used pinMode number (3-7) to run all LEDS and all was working
then used Blink example to test.
*To learn more about differences between analog and digital, I used this youtube link

"BLINK EXAMPLE"
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
digitalWrite(4, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(4, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
}

B / Apply a potentiometer to control LEDS's speed
I used pinMode number (3,4) to test with potentiometer through "AnalogReadSerial" example and
assumed that I could control the first and second light.
To achieve that..
1) I added pinMode 3 and 4 to void setup
2) I added digitalWrite both of pin3 and 4
Yes it was success!

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if (sensorValue > 250) {
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}
if (sensorValue > 250) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
}

C / Set the blinks for all lights as a loop so they get back and forth
To create a “Knight Rider” blinking style, I applied "Fade" example structure to control back-and-forth loop.
Orginal code from "Fade"
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
to "Knight Rider"
if (pin == 3 || pin == 7) {
next = -next;
}
void setup() {
// initialize serial communication at 9600 bits per second:
pinMode(3, OUTPUT);
Serial.begin(9600);
}
int pin = 3;
int next = 1;
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
digitalWrite(pin, HIGH);
delay(sensorValue/15);
digitalWrite(pin, LOW);
pin = pin + next;
if (pin == 3 || pin == 7) {
next = -next;
}
}
}
D / Final “Knight Rider” blinking with the SWITCH
For the last assignment, I applied a switch to control different actions with my lights. To test that it was connected with circuit correctly, I opened DigitalReadSerial and check it through serial monitor. Whenever I pressed the button, the program detected as "1" so my switch was connected already.
I was confusing when I tried to connect the botton with the circuit board so I follow the instruction on this youtube.


1st attempt : Highlight my Middle Son :
I used digitalRead to turn on only middle light ( pin no. 5)
when the button is pressed by add this part..
int middlepin = 5;
else { (digitalRead(10) == 1 ); digitalWrite(middlepin, HIGH); delay(20); digitalWrite(pin, LOW); delay(20); }
Here is my overall coding
void setup() { // initialize serial communication at 9600 bits per second: pinMode(3, OUTPUT); Serial.begin(9600); }
int pin = 3; int next = 1; int middlepin = 5;
// the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: if (digitalRead(10) == 0 ) { int sensorValue = analogRead(A0); // print out the value you read: //Serial.println(sensorValue); //delay(1); // delay in between reads for stability digitalWrite(pin, HIGH); delay(sensorValue/10); digitalWrite(pin, LOW); pin = pin + next; if (pin == 3 || pin == 7) { next = -next; } } else { (digitalRead(10) == 1 ); digitalWrite(middlepin, HIGH); delay(20); digitalWrite(pin, LOW); delay(20); } }
// Debug //
Problem My middle light was blinking stronger than others although I didn't press anything.
Hypothesis My digitalWrite codes are overlapped.
Result Still working on it
2nd attempt : Shut it all
I used digitalRead to turn off all lights when the button is pressed by add this part..
(digitalRead(10) == 1 ); digitalWrite(pin, LOW); delay(20);
Whenever the button switch is pressed, all the lights would be off!!
void setup() { // initialize serial communication at 9600 bits per second: pinMode(3, OUTPUT); Serial.begin(9600); }
int pin = 3; int next = 1;
// the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: if (digitalRead(10) == 0 ) { int sensorValue = analogRead(A0); // print out the value you read: //Serial.println(sensorValue); //delay(1); // delay in between reads for stability digitalWrite(pin, HIGH); delay(sensorValue/10); digitalWrite(pin, LOW); pin = pin + next; if (pin == 3 || pin == 7) { next = -next; } } else { (digitalRead(10) == 1 ); digitalWrite(pin, LOW); delay(20); } }















Comments