Hi,
I tried typing out a very long and detailed post, but for some reason it didn't post and I lost the whole thing.
My friend and I are in a class for High School where we get to experiment and learn with Arduinos (Mechatronics) and we wanted to build something very similar to NES Controller Interface for Etch A Sketch - Make: but before we got the motors we wanted to make a "diagnostic display" where it will illuminate an LED based off of which button is pressed. It's a fairly simple concept, but having to write digitalWrite() 72 times seemed a bit excessive and unnecessary. I figured there must be a way to simplify the code, and I thought that Arrays would be the way to go. Creating an array that is modified from within the cases and at the end of the loop, a way to digitalWrite( maxval, HIGH) and digitalWrite(minval, LOW) or something along those lines. Am I on the right track? Are there other methods for shortening up my code? I'm not looking for Shift Registers, I want the solution to come from the code only if possible. I also included a poorly done Fritzing diagram to show what the project will kind of look like, and to help visualize the goal.
int left = 2;
int up = 3;
int right = 4;
int down = 5;
int pinSetupArray [9]={6,7,8,9,10,11,12,13};
int seq = 0;
int switchLeft;
int switchUp;
int switchRight;
int switchDown;
void setup(){
for (seq = 0; seq <9; seq ++){
pinMode(pinSetupArray[seq],OUTPUT);
}
//sets all pins listed in pinSetupArray to OUTPUT
pinMode(left, INPUT);
pinMode(up, INPUT);
pinMode(right, INPUT);
pinMode(down, INPUT);
}
void loop(){
switchLeft = digitalRead(left);
switchUp = digitalRead(up);
switchRight = digitalRead(right);
switchDown = digitalRead(down);
if (switchLeft == 1){
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
else if(switchLeft ==1 && switchUp ==1);{
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
else if(switchUp == 1);{
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
// ... and so on for all the eight possible combinations
else {
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
}
