Hey everyone!
So for my Intro to Embedded Systems programming course I have to write a program that creates a Knight Rider sequence, but it has to have 5 different speeds that are controlled by the number of button presses. I have it almost all working, except once it hits the top 3 speeds the actual speed of the LED's does not change, rather they start to flash as they move back and forth. I have racked my brain and I can't figure out why. Any help would be much appreciated!
Thank you!
/*
EE1910_Lab4_ADV.ino
Counts down from 20 to 0 in 1 second intervals,
with a pushbutton that can reset the count to 20,
and leds that display the status of the count.
Author: Austin R. Bartz
Date: 1/11/13
Hardware connections;
LEDs to pins 4-13
Pushbuttons to pin 2
*/
char LED[]={
4,5,6,7,8,9,10,11,12,13}; //"maps" the LED pins from 0 thru 9
char button = 2; //variable for button pin
char lastState = 1; //last button state
char i; //variable for speed index
unsigned char t; //variable for delay
void setup()
{
char pin = 4; //starting pin and variable
while(pin <= 13) //sets LED pins as outputs
{
pinMode(pin, OUTPUT);
++pin; //increment pin #
}
pinMode(2, INPUT); //sets button pin as input
Serial.begin(9600); //starts serial
}
void loop()
{
char x = 1; //variable for increment
for (char n = 0; n >= 0; n = n + x) { //loop to increment and decrement the LEDs
char state = digitalRead(button); //reads button state to variable
if (state != lastState) {
if (state == 0) i++;
}
lastState = state;
if (i == 5) i = 0;
switch (i) { //sets delay and prints message based on speed index
case 0:
t = 200;
Serial.println("Captain Picard says ENGAGE!");
Serial.println("Warp 1 (SLOW)");
break;
case 1:
t = 100;
Serial.println("Captain Picard says ENGAGE!");
Serial.println("Warp 2 (MEDIUM)");
break;
case 2:
t = 50;
Serial.println("Captain Picard says ENGAGE!");
Serial.println("Warp 3 (FAST)");
break;
case 3:
t = 25;
Serial.println("Captain Picard says ENGAGE!");
Serial.println("Warp 4 (VERY FAST)");
break;
case 4:
t = 10;
Serial.println("Captain Picard says ENGAGE!");
Serial.println("Warp 5 (ULTRA FAST)");
break;
}
if (n == 0 && x == -1) t = 0; //prevents the first LED for being ON for 2*delay
digitalWrite(LED[n], 1); //turns LED n ON
if (n == 9) x = -1; //reverses count
delay(t); //delay time
digitalWrite(LED[n], 0); //turns LED n OFF
}
}