const int LEDs[] = { 7 , 6 , 5 , 4 , 3 , 2, 8 };
const int ledStripSize = sizeof(LEDs) / sizeof(LEDs[0]);
const int startingPosition = 0;
//intialising the current position
int currentPosition;
typedef void (*callbackFunction)();
void setup() {
Serial.begin(115200);
// set all of the led pin modes to HIGH
for(int i = 0 ; i < ledStripSize ; i++){
pinMode(LEDs[i] , OUTPUT);
}
//check if the starting postion is out of bounds
if(startingPosition <= ledStripSize - 1){
currentPosition = startingPosition;
}
else{
Serial.println("Starting Position is out of bounds , Setting The Default Current Postion to 0");
currentPosition = 0;
}
//turning the light on starting position
blinkLED();
}
void loop() {
// moveRight();
// moveLeft();
// moveRight();
repeat(moveRight , 6);
repeat(moveLeft , 6);
}
bool moveRight(){
//handling out of range errors
if(currentPosition < ledStripSize - 1 ){
currentPosition++;
blinkLED();
return true;
}
else{
Serial.println("Right is out of bounds");
blinkLED();
return false;
};
}
bool moveLeft(){
if(currentPosition > 0 ){
currentPosition--;
blinkLED();
return true;
}
else{
Serial.println("Left is out of bounds");
blinkLED();
return false;
};
}
void blinkLED(){
digitalWrite(LEDs[currentPosition] , HIGH);
Serial.println(currentPosition);
delay(500);
digitalWrite(LEDs[currentPosition] , LOW);
delay(500);
}
void repeat(callbackFunction callback , int times){
for (int i = 0 ; i < times ; i++){
callback();
}
}