Hi there,
I'm brand new to the forum, but have been using Arduinos for a couple of years and typically can get what I need done wih enough trial and error/research. I was hoping those more experienced could have a look over my code and see how I could (no doubt) improve it to be more efficient.
It currently functions as I would like, but despite my reseach into understanding exactly how some of the functions operate (!, &&, ||, ==, and boolean), I have been unable to remove any of the else if statements ("Neutral") without breaking the functionality. It seems to me I should be able to write this code so that the else if is the ongoing state, and only varies from that when a magnet is present at one of the hall effect sensors - otherwise it seems bloated.
Aside from this, the serial monitor output for gears one-three gives me a single line/instance, however gears 4, 5 & reverse gives me endless lines of output until the magnet is removed. Doesn't affect the functionality as such, but certainly seems resource intensive.
Gracious for any help offered, thankyou.
// Arduino Nano-based manual gear shifter detector & display using the TM1637 7-Segment LED module.
// 3x A3144 Hall Effect Sensors are connected via pins 2, 3 & 12, and when a magnet/s is present,
// will display gear One (pin 2), Gear Two (pin 3), Gear Three (pin 12), Gear Four (Pins 2 & 12),
// Gear Five (Pins 3 & 12), and Reverse (pins 2 & 3) - otherwise Neutral is displayed.
#include Arduino.h
#include TM1637Display.h
const int CLK = 5; // Set the CLK pin connection to the display
const int DIO = 4; // Set the DIO pin connection to the display
const uint8_t blank[] = {0x00, 0x00, 0x00, 0x00}; // Declares the value of "blank"
const uint8_t data[] = {0xff, 0xff, 0xff, 0xff}; // Declares "data" as all segments on
const uint8_t n[] = { // Required segments to display the character "n"
SEG_C | SEG_E | SEG_G
};
const uint8_t r[] = {
SEG_E | SEG_G
}; // Required segments to display the character "r"
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
int TM1637Display(CLK, DIO);
int sensorPin2 = 2;
int sensorPin3 = 3;
int sensorPin12 = 12;
int ledPin7 = 7;
int ledPin8 = 8;
int ledPin9 = 9;
int counter = 0;
boolean sensorState2 = true;
boolean sensorState3 = true;
boolean sensorState12 = true;
void setup()
{
Serial.begin(9600); // Setup serial monitor
pinMode(sensorPin2, INPUT);
pinMode(sensorPin3, INPUT);
pinMode(sensorPin12, INPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT);
display.clear();
display.setBrightness(7); // Set the diplay to maximum brightness
display.setSegments(blank); // Clear display
}
void loop()
{
//display.setBrightness(7);
//display.setSegments(blank);
// Gear One
if (magnetPresent(sensorPin2) && !sensorState2)
{
sensorState2 = true;
printMessage("Gear One (Sensor Pin 2, Led Pin 7)");
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
display.showNumberDec(1, false, 4);
}
// Neutral
else if (!magnetPresent(sensorPin2) && sensorState2)
{
sensorState2 = false;
sensorState3 = false;
sensorState12 = false;
printMessage("Neutral");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
display.clear();
display.setSegments(n, 1, 3);
}
// Gear Two
if (magnetPresent(sensorPin3) && !sensorState3)
{
sensorState3 = true;
printMessage("Gear Two (Sensor Pin 3, Led Pin 8)");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin9, LOW);
display.showNumberDec(2, false, 4);
}
// Neutral
else if (!magnetPresent(sensorPin3) && sensorState3)
{
sensorState2 = false;
sensorState3 = false;
sensorState12 = false;
printMessage("Neutral");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
display.clear();
display.setSegments(n, 1, 3);
}
// Gear Three
if (magnetPresent(sensorPin12) && !sensorState12)
{
sensorState12 = true;
printMessage("Gear Three (Sensor Pin 12, Led Pin 9)");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, HIGH);
display.showNumberDec(3, false, 4);
}
// Neutral
else if (!magnetPresent(sensorPin12) && sensorState12)
{
sensorState2 = false;
sensorState3 = false;
sensorState12 = false;
printMessage("Neutral");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
display.clear();
display.setSegments(n, 1, 3);
}
// Gear Four
if ((magnetPresent(sensorPin2) && sensorState2) && (magnetPresent(sensorPin12) && sensorState12) || (magnetPresent(sensorPin12) && sensorState12) && (magnetPresent(sensorPin2) && sensorState2))
{
sensorState2 = true;
sensorState12 = true;
printMessage("Gear Four (Sensor Pin 2 & 12, Led Pin 7 & 9)");
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, HIGH);
display.showNumberDec(4, false, 4);
}
// Neutral
else if ((!magnetPresent(sensorPin2) && sensorState2) && (!magnetPresent(sensorPin12) && sensorState12))
{
sensorState2 = false;
sensorState3 = false;
sensorState12 = false;
printMessage("Neutral");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
display.clear();
display.setSegments(n, 1, 3);
}
// Gear Five
if ((magnetPresent(sensorPin3) && sensorState3) && (magnetPresent(sensorPin12) && sensorState12) || (magnetPresent(sensorPin12) && sensorState12) && (magnetPresent(sensorPin3) && sensorState3))
{
sensorState3 = true;
sensorState12 = true;
printMessage("Gear Five (Sensor Pin 3 & 12, Led Pin 8 & 9)");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin9, HIGH);
display.showNumberDec(5, false, 4);
}
// Neutral
else if ((!magnetPresent(sensorPin3) && sensorState3) && (!magnetPresent(sensorPin12) && sensorState12))
{
sensorState2 = false;
sensorState3 = false;
sensorState12 = false;
printMessage("Neutral");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
display.clear();
display.setSegments(n, 1, 3);
}
// Gear Reverse
if ((magnetPresent(sensorPin2) && sensorState2) && (magnetPresent(sensorPin3) && sensorState3) || (magnetPresent(sensorPin3) && sensorState3) && (magnetPresent(sensorPin2) && sensorState2))
{
sensorState2 = true;
sensorState3 = true;
printMessage("Gear Reverse (Sensor Pin 2 & 3, Led Pin 7 & 8)");
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin9, LOW);
display.setSegments(r, 1, 3);
}
// Neutral
else if ((!magnetPresent(sensorPin2) && sensorState2) && (!magnetPresent(sensorPin3) && sensorState3))
{
sensorState2 = false;
sensorState3 = false;
sensorState12 = false;
printMessage("Neutral");
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
display.clear();
display.setSegments(n, 1, 3);
}
}
void printMessage(String message) {
counter++;
Serial.print(counter);
Serial.print(" ");
Serial.println(message);
//delay(200);
}
boolean magnetPresent(int pin) {
return digitalRead(pin) == LOW;
}