Hey guys...
As a little background I just got through trouble-shooting a hardware issue. The thread particular to that issue can be found here: https://forum.arduino.cc/index.php?action=profile;area=showposts;u=505335
I mention this in-case my current issue is driven by that hardware issue I had yesterday. Diagram also included there.
Today I ran the device for a full cycle. This is the device idle for about 20 minutes and about 8 minutes of solenoid open time. After checking on the device- the LCD was hung up. I am thinking I'm either stuck in an endless loop or I have a variable that was not declared correctly and errors out the device.
Posting program code below. It should be noted the hang-up occurs during "draining" state. In this state the progress bar is ticking down slowly. It draws 4 rectangles, then 3, then 2 etc...
All ideas welcomed!
// Import required libraries
#include <ArducamSSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file)
#include <Wire.h> // For I2C comm, but needed for not getting compile error
//#include <OneWire.h>
//#include <DallasTemperature.h>
#define OLED_RESET 16 // Pin 15 -RESET digital signal
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
ArducamSSD1306 display(OLED_RESET); // FOR I2C
//global vars
unsigned long sensor1Time, sensor2Time = millis(); //set time of sensor change
unsigned long currMillis = millis();
bool sensor1 = true; //state of sensor 1
bool sensor2 = true; //state of sensor 2
int prevMode = 0;
bool solenoidState = false; //state of water valve
int currMode = 4; //logic mode 1=empty 2=draining 3=filling 4=full
int barState = 4; //progress bar var
unsigned long barUpdate = millis(); //progress bar update
void setup(void)
{
// Start Serial
Serial.begin(115000);
// SSD1306 Init
display.begin(); // Switch OLED
display.setTextColor(WHITE);
// Clear the buffer.
display.clearDisplay();
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(10, OUTPUT);
analogWrite(10, 0);
}
void loop() {
int s1 = digitalRead(11); //check-sensors
int s2 = digitalRead(12);
currMillis = millis(); //set time for eval
if((currMillis-sensor1Time) >= 5000){ //evaluate if reading is consistant for 5 seconds
sensor1 = evaluate(1, s1);
}
if((currMillis-sensor2Time) >= 5000){
sensor2 = evaluate(2, s2);
}
currMode = process(sensor1, sensor2); //set mode based on sensors
changeState(currMode); //change solenoid state based on mode
barCount(); //set progress bar count
//print info to screen
display.clearDisplay();
printMenu();
printSystems();
printBar();
display.display();
}
bool evaluate(int sensor, int sVal){
if(sVal == 0){
if(sensor == 1){
sensor1Time = millis();
}
else{ //if(sensor == 2)
sensor2Time = millis();
}
return false;
}
else{ //if(sVal == 1)
if(sensor == 1){
sensor1Time = millis();
}
else{ //if(sensor == 2)
sensor2Time = millis();
}
return true;
}
}
void printMenu(){
display.setCursor(10,0);
display.setTextSize(2);
display.println("AUTO-FILL");
display.setTextSize(1);
display.setCursor(8,16);
display.println("SYSTEM");
display.setCursor(86,16);
display.println("STATUS");
display.drawRect(0, 15, 51, 10, WHITE);
display.drawRect(79, 15, 49, 10, WHITE);
display.drawLine(0, 15, 128, 15, WHITE);
display.drawPixel(0, 31, WHITE);
display.setCursor(3,28);
display.println("LVL SENSOR 1");
display.drawPixel(0, 41, WHITE);
display.setCursor(3,38);
display.println("LVL SENSOR 2");
display.drawPixel(0, 51, WHITE);
display.setCursor(3,48);
display.println("FEED VALVE 1");
}
int process(bool upperSens, bool lowerSens){
if((upperSens == false) && (lowerSens == false)){
return 1;
}
else if((upperSens == true) && (lowerSens == true)){
return 4;
}
else{ //((lowerSens == true) && (upperSens == false))
if(solenoidState == false){
return 2;
}
else{ //(solenoidState == true)
return 3;
}
}
}
void changeState(int state){
if(state == 1){
analogWrite(10, 255);
solenoidState = true;
currMode = 3;
}
if(state == 4){
analogWrite(10, 0);
solenoidState = false;
//currMode = 2;
}
}
void printSystems(){
display.setCursor(92,28);
if(sensor1 == false){
display.println("OFF");
}
else{
display.println("ON");
}
display.setCursor(92,38);
if(sensor2 == false){
display.println("OFF");
}
else{
display.println("ON");
}
display.setCursor(92,48);
if(solenoidState == true){
display.println("OPEN");
}
else{
display.println("CLOSED");
}
}
void barCount(){
switch(currMode){
case 1 :
barState = 0;
break;
case 2 :
if(barUpdate < millis()-1000){
if(barState == 0){
barState = 4;
}
else{
barState = barState - 1;
}
barUpdate = millis();
}
break;
case 3 :
if(barUpdate < millis()-1000){
if(barState == 4){
barState = 0;
}
else{
barState = barState + 1;
}
barUpdate = millis();
}
break;
case 4 :
barState = 4;
break;
}
}
void printBar(){
switch(barState){
case 0 :
break;
case 1 :
display.fillRect(2, 60, 29, 6, WHITE);
break;
case 2 :
display.fillRect(2, 60, 29, 6, WHITE);
display.fillRect(34, 60, 29, 6, WHITE);
break;
case 3 :
display.fillRect(2, 60, 29, 6, WHITE);
display.fillRect(34, 60, 29, 6, WHITE);
display.fillRect(66, 60, 29, 6, WHITE);
break;
case 4 :
display.fillRect(2, 60, 29, 6, WHITE);
display.fillRect(34, 60, 29, 6, WHITE);
display.fillRect(66, 60, 29, 6, WHITE);
display.fillRect(98, 60, 28, 6, WHITE);
break;
}
}
[color=#222222][/color]