const int PIN_VERT = A0;
const int PIN_HORIZ = A1;
const int PIN_SEL = A2;
const int PIN_LED_SIZE = 6;
const int PIN_LED[] {3, 5, 6, 9, 10, 11};
class neighbor{
public:
int left;
int right;
int up;
int down;
neighbor(int l, int r, int u, int d);
};
neighbor::neighbor(int l, int r, int u, int d){
left = l;
right = r;
up = u;
down = d;
}
float vertV;
float horizV;
float selV;
int prevLED = 0;
int currentLED = 0;
int delayTime = 200;
neighbor ledNeighbor[] {
neighbor(-1, 1, -1, 3),
neighbor(0, -1, -1, 2),
neighbor(3, -1, 1, 5),
neighbor(-1, 2, 0, 4),
neighbor(-1, 5, 3, -1),
neighbor(4, -1, 2, -1)};
void setup() {
Serial.begin(9600);
pinMode(PIN_VERT, INPUT);
pinMode(PIN_HORIZ, INPUT);
pinMode(PIN_SEL, INPUT);
for (int i = 0; i < PIN_LED_SIZE; i++){
pinMode(PIN_LED[i], OUTPUT);
}
}
void loop() {
vertV = convert(analogRead(PIN_VERT));
horizV = convert(analogRead(PIN_HORIZ));
selV = convert(analogRead(PIN_SEL));
prevLED = currentLED;
if(vertV == 5.0){
currentLED = validNeighbor(prevLED, ledNeighbor[prevLED].up);
}else if(vertV == 0){
currentLED = validNeighbor(prevLED, ledNeighbor[prevLED].down);
}else if(horizV == 5.0){
currentLED = validNeighbor(prevLED, ledNeighbor[prevLED].left);
}else if(horizV == 0.0){
currentLED = validNeighbor(prevLED, ledNeighbor[prevLED].right);
}
toggleLED(currentLED);
delay(delayTime);
}
void toggleLED(int index){
for(int i = 0; i < PIN_LED_SIZE; i++){
if(i == index){
digitalWrite(PIN_LED[i], HIGH);
}else{
digitalWrite(PIN_LED[i], LOW);
}
}
}
float convert(float v){
return (5.0 / 1023.0) * v;
}
int validNeighbor(int current, int next){
if(next < 0){
return current;
}else{
return next;
}
}