Hey all! I've been working on a complicated project involving multiple Arduinos integrated into some on my vehicle's functions. This particular function I'm augmenting is the Air Conditioner indication. The OEM indication is the use of LEDs to show a certain function is in use (auto, A/C, Front + Rear control, etc).
My first iteration was to simply replicate the indication on an OLED screen which worked pretty well. The next iteration I'm working on involves communicating status over the serial bus. I'm running into a problem with the MonitorInputs function I created (Analog.read) interfering with a simple response function (Serial1Rply). When I comment out the MonitorInputs function the Serial1Rply function works (I receive the reply code when prompted from the IDE Serial Monitor).
I'd greatly appreciate anyone taking a peek at my code below for any obvious errors. Also, not sure if this is a known issue but I've also used a function to print the analog inputs over the Serial bus to the IDE Serial Monitor.
// NOTE: Pro Micro board appears as Leonardo
// NOTE: Ensure SEPU has common ground with system
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, & Wire, OLED_RESET);
int FDEF_Pin = A0; //FDEF Input
int RDEF_Pin = A1; //RDEF Input
int AUTO_Pin = A2; //AUTO Input
int AC_Pin = A3; //A/C Input
int CIRC_Pin = A4; //CIRC Input
int FR_Pin = A5; //F+R Input
int FDEF_State = 0;
int RDEF_State = 0;
int AUTO_State = 0;
int AC_State = 0;
int CIRC_State = 0;
int FR_State = 0;
//int CMD_CODE;
//int RPLY_CODE;
int TestMode = 1; //Default to display test on startup
void indicatorTest0() //indicatorTest(void)
{
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner (x, y)
display.print("FDEF");
display.setCursor(80, 0);
display.print("RDEF");
display.setCursor(0, 24);
display.print("AUTO");
display.setCursor(92, 24);
display.print("A/C");
display.setCursor(0, 49);
display.print("CIRC");
display.setCursor(92, 49);
display.print("F+R");
display.drawLine(0, 17, 127, 17, WHITE);
display.drawLine(0, 43, 127, 43, WHITE);
display.drawLine(64, 0, 64, 63, WHITE);
display.display();
}
void indicatorOff() //indicatorTest(void)
{
//display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(0,0); // Start at top-left corner (x, y)
display.print("FDEF");
display.setCursor(80, 0);
display.print("RDEF");
display.setCursor(0, 24);
display.print("AUTO");
display.setCursor(92, 24);
display.print("A/C");
display.setCursor(0, 49);
display.print("CIRC");
display.setCursor(92, 49);
display.print("F+R");
display.setTextColor(SSD1306_WHITE);
display.drawLine(0, 17, 127, 17, WHITE);
display.drawLine(0, 43, 127, 43, WHITE);
display.drawLine(64, 0, 64, 63, WHITE);
display.display();
}
void PrintFDEF_On()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner (x, y)
display.print("FDEF");
display.display();
}
void PrintFDEF_Off()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(0,0); // Start at top-left corner (x, y)
display.print("FDEF");
display.display();
}
void PrintRDEF_On()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(80, 0); // Start at top-left corner (x, y)
display.print("RDEF");
display.display();
}
void PrintRDEF_Off()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(80, 0); // Start at top-left corner (x, y)
display.print("RDEF");
display.display();
}
void PrintAUTO_On()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 24); // Start at top-left corner (x, y)
display.print("AUTO");
display.display();
}
void PrintAUTO_Off()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(0, 24); // Start at top-left corner (x, y)
display.print("AUTO");
display.display();
}
void PrintAC_On()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(92, 24); // Start at top-left corner (x, y)
display.print("A/C");
display.display();
}
void PrintAC_Off()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(92, 24); // Start at top-left corner (x, y)
display.print("A/C");
display.display();
}
void PrintCIRC_On()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 49); // Start at top-left corner (x, y)
display.print("CIRC");
display.display();
}
void PrintCIRC_Off()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(0, 49); // Start at top-left corner (x, y)
display.print("CIRC");
display.display();
}
void PrintFR_On()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(92, 49); // Start at top-left corner (x, y)
display.print("F+R");
display.display();
}
void PrintFR_Off()
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(92, 49); // Start at top-left corner (x, y)
display.print("F+R");
display.display();
}
void MonitorFDEF()
{
FDEF_State = analogRead(A0);
delay(120);
if (FDEF_State>=350)
{
PrintFDEF_Off();
}
else if (FDEF_State<=330)
{
PrintFDEF_On();
}
else
{
}
}
void MonitorRDEF()
{
RDEF_State = analogRead(A1);
delay(120);
if (RDEF_State>=350)
{
PrintRDEF_Off();
}
else if (RDEF_State<=330)
{
PrintRDEF_On();
}
else
{
}
}
void MonitorAUTO()
{
AUTO_State = analogRead(A2);
delay(120);
if (AUTO_State>=350)
{
PrintAUTO_Off();
}
else if (AUTO_State<=330)
{
PrintAUTO_On();
}
else
{
}
}
void MonitorAC()
{
AC_State = analogRead(A3);
delay(120);
if (AC_State>=350)
{
PrintAC_Off();
}
else if (AC_State<=330)
{
PrintAC_On();
}
else
{
}
}
void MonitorCIRC()
{
CIRC_State = analogRead(A4);
delay(120);
if (CIRC_State>=350)
{
PrintCIRC_Off();
}
else if (CIRC_State<=330)
{
PrintCIRC_On();
}
else
{
}
}
void MonitorFR()
{
FR_State = analogRead(A5);
delay(120);
if (FR_State>=350)
{
PrintFR_Off();
}
else if (FR_State<=330)
{
PrintFR_On();
}
else
{
}
}
void MonitorInputs()
{
MonitorFDEF();
MonitorRDEF();
MonitorAUTO();
MonitorAC();
MonitorCIRC();
MonitorFR();
}
void PrintInputs()
{
Serial.print("FDEF_State ");
Serial.println(FDEF_State);
Serial.print("RDEF_State ");
Serial.println(RDEF_State);
Serial.print("AUTO_State ");
Serial.println(AUTO_State);
Serial.print("AC_State ");
Serial.println(AC_State);
Serial.print("CIRC_State ");
Serial.println(CIRC_State);
Serial.print("FR_State ");
Serial.println(FR_State);
}
void Serial1Rply()
{
if (Serial.available()) {
int CMD_CODE = Serial.parseInt();
int RPLY_CODE = (CMD_CODE + 1);
if (CMD_CODE==9002)
{
Serial.print("Response: ");
Serial.println(RPLY_CODE);
}
else
{
}
}
}
void setup()
{
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(1000);
display.clearDisplay();
pinMode(FDEF_Pin, INPUT);
pinMode(RDEF_Pin, INPUT);
pinMode(AUTO_Pin, INPUT);
pinMode(AC_Pin, INPUT);
pinMode(CIRC_Pin, INPUT);
pinMode(FR_Pin, INPUT);
}
void loop()
{
while (true)
{
//MonitorInputs();
PrintInputs();
Serial1Rply();
/*if (TestMode == 1)
{
indicatorTest0();
TestMode = (TestMode - 1);
delay(3000);
indicatorOff();
}
else
{
}*/
}
}