EDIT: Thanks to everyone here I've successfully re-factored the code to be far more flexible and friendly! See the latest version here if you are interested:
Hi there!
Long time lurker, first time poster here - thanks for all the resources, they've been incredibly helpful already.
I have some code (below) that along with some Hall Effect Sensors, a magnet, and an Arduino Uno (rev3?) serves as a Gear Shift Indicator for my dad's car - which is displayed on an 8x8 dot matrix LED.
Thanks to everyone here it works great as-is, but I'm looking to make the code more efficient and readable before I add new features - I have an absolute mess of IF/ELSE statements that was a nightmare to wrap my head around and I'd like to fix that before moving the section to its own function.
I'm aware of arrays, and that multiple variables ending in numbers often indicates that an array combined with a loop could likely make it simpler but I'm having trouble conceptualising it and translating what I currently have.
I was hoping someone might be able to assist or point me towards a relevant resource (I have searched, but I'm unable to find someone doing what I'm trying to achieve).
Any help would be greatly appreciated!
Code is below, and attached is a simple wiring diagram minus the LED.
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define MAX_DEVICES 1
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
// constants won't change. They're used here to set pin numbers:
const int hallOne = A0; // the number of the hall effect sensor pin
const int hallTwo = A1;
const int hallThree = A2;
const int hallFour = A3;
const int hallFive = A4;
const int hallSix = A5;
// variables will change:
int hallOneState = 0; // variables for reading the hall sensors status
int hallTwoState = 0;
int hallThreeState = 0;
int hallFourState = 0;
int hallFiveState = 0;
int hallSixState = 0;
char previousDisplay = "";
MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);
void setup() {
// initialize the hall effect sensor pins as an input:
pinMode(hallOne, INPUT);
pinMode(hallTwo, INPUT);
pinMode(hallThree, INPUT);
pinMode(hallFour, INPUT);
pinMode(hallFive, INPUT);
pinMode(hallSix, INPUT);
// start LCD display
P.begin();
P.setIntensity(4);
}
void loop() {
// read the state of the hall effect sensors:
hallOneState = digitalRead(hallOne);
hallTwoState = digitalRead(hallTwo);
hallThreeState = digitalRead(hallThree);
hallFourState = digitalRead(hallFour);
hallFiveState = digitalRead(hallFive);
hallSixState = digitalRead(hallSix);
if (hallOneState == LOW) {
if (previousDisplay == 'P') {
previousDisplay = 'R';
P.displayText("R", PA_CENTER, 50, 1, PA_SCROLL_UP, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else if (previousDisplay == 'R') {
P.displayText("R", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
}
else if (previousDisplay == 'N') {
previousDisplay = 'R';
P.displayText("R", PA_CENTER, 50, 1, PA_SCROLL_DOWN, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else {
P.displayText("R", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
previousDisplay = 'R';
}
}
else if (hallTwoState == LOW) {
if (previousDisplay == 'R') {
previousDisplay = 'N';
P.displayText("N", PA_CENTER, 50, 1, PA_SCROLL_UP, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else if (previousDisplay == 'N') {
P.displayText("N", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
}
else if (previousDisplay == 'D') {
previousDisplay = 'N';
P.displayText("N", PA_CENTER, 50, 1, PA_SCROLL_DOWN, PA_NO_EFFECT);
while (!P.displayAnimate())
;
}
else {
P.displayText("N", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
previousDisplay = 'N';
}
}
else if (hallThreeState == LOW) {
if (previousDisplay == 'N') {
previousDisplay = 'D';
P.displayText("D", PA_CENTER, 50, 1, PA_SCROLL_UP, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else if (previousDisplay == 'D') {
P.displayText("D", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
}
else if (previousDisplay == '3') {
previousDisplay = 'D';
P.displayText("D", PA_CENTER, 50, 1, PA_SCROLL_DOWN, PA_NO_EFFECT);
while (!P.displayAnimate())
;
}
else {
P.displayText("D", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
previousDisplay = 'D';
}
}
else if (hallFourState == LOW) {
if (previousDisplay == 'D') {
previousDisplay = '3';
P.displayText("3", PA_CENTER, 50, 1, PA_SCROLL_UP, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else if (previousDisplay == '3') {
P.displayText("3", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
}
else if (previousDisplay == '2') {
previousDisplay = '3';
P.displayText("3", PA_CENTER, 50, 1, PA_SCROLL_DOWN, PA_NO_EFFECT);
while (!P.displayAnimate())
;
}
else {
P.displayText("3", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
previousDisplay = '3';
}
}
else if (hallFiveState == LOW) {
if (previousDisplay == '3') {
previousDisplay = '2';
P.displayText("2", PA_CENTER, 50, 1, PA_SCROLL_UP, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else if (previousDisplay == '2') {
P.displayText("2", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
}
else if (previousDisplay == '1') {
previousDisplay = '2';
P.displayText("2", PA_CENTER, 50, 1, PA_SCROLL_DOWN, PA_NO_EFFECT);
while (!P.displayAnimate())
;
}
else {
P.displayText("2", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
previousDisplay = '2';
}
}
else if (hallSixState == LOW) {
if (previousDisplay == '2') {
previousDisplay = '1';
P.displayText("1", PA_CENTER, 50, 1, PA_SCROLL_UP, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else if (previousDisplay == '1') {
P.displayText("1", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
}
else {
P.displayText("1", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
previousDisplay = '1';
}
}
else {
if (previousDisplay == 'R') {
previousDisplay = 'P';
P.displayText("P", PA_CENTER, 50, 1, PA_SCROLL_DOWN, PA_NO_EFFECT);
while (!P.displayAnimate())
;
} else {
previousDisplay = 'P';
P.displayText("P", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
P.displayAnimate();
previousDisplay = 'P';
}
}
}
