// input button pins, these will all be set up with pulldown resistors
const int Input_Button = 2;
const int Zero_Button = 3;
const int Jump_Button = 4;
const int Stop_Button = 5;
//array for LED pins
int currentROW = 0;
int currentCOLUMN = 0;
int totalCOLUMNinput = 0; //used to limit total number of columns according to user input
const int LEDPins[6][4] = { //6 rows and 4 columns of pins
{22,23,24,25}, // this array will use arduino mega's digital pins 22-45
{26,27,28,29},
{30,31,32,33},
{34,35,36,37},
{38,39,40,41},
{42,43,44,45},
};
void waitForInput(){
while(Serial.available() <= 0){
//puts user in endless while loop when there is no input
}
}
void clearSerialBuffer(){
while(Serial.available() > 0){
Serial.read(); //reads any leftover /n's or spaces and doesn't feed them into other inputs
}
}
//move to next position function
//show current position function
void showROWandCOLUMN(){ //shows the current position after button presses
Serial.print("Current position: Row");
Serial.print(currentROW + 1); // this shows the rows as 1-6 instead of 0-5
Serial.print(", Column ");
Serial.print(currentCOLUMN + 1); // this shows the columns as 1-4 instead of 0-3
}
void incrementCOLUMNandROWS(){
if (currentCOLUMN < totalCOLUMNinput) {
currentCOLUMN++;
}
else if (currentCOLUMN >= totalCOLUMNinput){
currentCOLUMN = 0; //column is 0 because it goes to the next row
if (currentROW = 6){ //if row is greater than 5, reset back to zero
currentROW = 0;
}
else{
currentROW = currentROW + 1; //if it is still less than 5, increment by 1
}
}
showROWandCOLUMN(); //show column after adjustments
}
void setup(){
Serial.begin(9600);
bool validinput = false;// used to check if user's input is in the range of 1-4
// Sets up all button pins as inputs
pinMode(Input_Button, INPUT);
pinMode(Zero_Button, INPUT);
pinMode(Jump_Button, INPUT);
pinMode(Stop_Button, INPUT);
for (int row = 0; row < 6; row++) { //goes thru the entire array of LEDs and sets the pin connections as inputs
for (int col = 0; col < 4; col++) {
pinMode(LEDPins[row][col], OUTPUT);
}
}
while(validinput == false){ //asks user how many columns to use for the array
Serial.println("How many columns do you want to use? (1-4):");
waitForInput();
totalCOLUMNinput = Serial.parseInt(); //takes user integer input and stores in variable
clearSerialBuffer(); //reads extra characters
if ((totalCOLUMNinput >= 1) && (totalCOLUMNinput <= 4)){ //inputs in between 1-4
Serial.print("Using ");
Serial.print(totalCOLUMNinput);
Serial.println(" columns.");
validinput = true;
}else{
Serial.println("Invalid Input! Please enter a number between 1 to 4 and press <enter>");
}
}
void loop(){
if (digitalRead(Input_Button) == 1){ //turn the LED on if input button is pressed at iteration
digitalWrite(LEDPins[currentROW][currentCOLUMN], 1);
Serial.println("LED ON!");
incrementCOLUMNandROWS();
delay(200);
}
if (digitalRead(Zero_Button) == 1){ //turn the LED OFF if input button is pressed at iteration
digitalWrite(LEDPins[currentROW][currentCOLUMN], 0);
Serial.println("LED OFF!");
incrementCOLUMNandROWS();
delay(200);
}
if (digitalRead(Jump_Button) == 1){ //turn the LED OFF if input button is pressed at iteration
if (currentROW = 6){ //if row is greater than 5, reset back to zero
currentROW = 0;
currentCOLUMN = 0;
Serial.println("Jumped to next row!");
showROWandCOLUMN();
delay(200);
}
else {
currentROW = currentROW + 1; //if it is still less than 5, increment by 1
currentCOLUMN = 0;
Serial.println("Jumped to next row!");
showROWandCOLUMN();
delay(200);
}
}
if (digitalRead(Stop_Button) == 1){ //return to stop if stop button is pressed
Serial.println("Program Stopped!");
return;
}
}