Hello! I have written a very long hand version of the knight rider code because I wish a play a tone as each LED blinks (the finished effect is a slow increase in blinking speed to give the appearance of a device charging up). However, I prefer the condensed version of the Knight Rider code and the smoother transition between LEDs. I was wondering if there was a way to include my tone with the Knight Rider code. I have posted my code with sound and then the sample Knight Rider code.
I am open to any and suggestions, very new to Arduino!
My code
/*
Laser gun charge
*/
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
delay(300);
tone(7, 2000, 80);
digitalWrite(3, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(3, LOW); // set the LED off
delay(80); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(80); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(80); // wait for a second
tone(7, 2000, 80);
digitalWrite(6, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(6, LOW); // set the LED off
delay(80); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(80); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(80); // wait for a secon
tone(7, 2000, 80);
digitalWrite(3, HIGH); // set the LED on
delay(80); // wait for a second
digitalWrite(3, LOW); // set the LED off
delay(70); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(70); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(70); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(70); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(70); // wait for a second
tone(7, 2000, 80);
digitalWrite(6, HIGH); // set the LED on
delay(70); // wait for a second
digitalWrite(6, LOW); // set the LED off
delay(60); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(60); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(60); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(60); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(60); // wait for a secon
tone(7, 2000, 80);
digitalWrite(3, HIGH); // set the LED on
delay(60); // wait for a second
digitalWrite(3, LOW); // set the LED off
delay(50); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(50); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(50); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(50); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(50); // wait for a second
tone(7, 2000, 80);
digitalWrite(6, HIGH); // set the LED on
delay(50); // wait for a second
digitalWrite(6, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(40); // wait for a secon
tone(7, 2000, 80);
digitalWrite(3, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(3, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(6, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(6, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(40); // wait for a secon
tone(7, 2000, 80);
digitalWrite(3, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(3, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(6, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(6, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(5, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(5, LOW); // set the LED off
delay(40); // wait for a second
tone(7, 2000, 80);
digitalWrite(4, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(4, LOW); // set the LED off
delay(40); // wait for a secon
tone(7, 2000, 80);
digitalWrite(3, HIGH); // set the LED on
delay(40); // wait for a second
digitalWrite(3, LOW); // set the LED off
delay(40); // wait for a second
}
else {
}
}
Knight Rider sample code
/* Knight Rider 3
* --------------
*
* This example concentrates on making the visuals fluid.
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/
int pinArray[] = {2, 3, 4, 5, 6, 7};
int count = 0;
int timer = 30;
void setup(){
for (count=0;count<6;count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
for (count=5;count>0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
}
Thanks!