Help with a 3 Timer Relay w/ OLED - Arduino Uno

Hello,

I am in need of some guidance with a project that I am trying to work on involving a Arduino Uno and a OLED.

Project description:

3 timers (different time lengths each)

Timers 1, 2, 3 triggered to start with a momentary switch

When the countdown is complete, trigger a relay that corresponds to the timer that was set (relay 1,2 or 3).

Display the time of the countdown on the OLED display as well as the serial monitor.

When the time is complete, display DONE until another timer is started.

Here is my start which works for the timers, bot does not display properly:

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

#define OLED_ADDR 0x3C

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

char
szString[20];
byte
mins, secs;

unsigned long
timeTemp,
timeNow,
timeStart,
timeElapsed,
timeLeft;

Adafruit_SSD1306 display(-1);

const int SwitchPin1 = 2;
const int SwitchPin2 = 4;
const int SwitchPin3 = 7;
const int RelayPin1 = 13;
const int RelayPin2 = 12;
const int RelayPin3 = 8;

unsigned long startTime1 = 5000;
unsigned long startTime2 = 10000;
unsigned long startTime3 = 15000;
const long interval = 1000;

int SwitchState1 = 0;
int SwitchState2 = 0;
int SwitchState3 = 0;

void setup() {

// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);

Serial.begin(9600);
timeStart = millis();
mins = 1;
secs = 1;

pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);

pinMode(SwitchPin1, INPUT);
pinMode(SwitchPin2, INPUT);
pinMode(SwitchPin3, INPUT);

}
void DoCountdown()

{
static unsigned long
lastTimeNow = 0;
static byte
lastsecs = 1;

timeNow = millis();
timeElapsed = timeNow - timeStart;

if( mins == 0 && secs == 0 )
return;

timeLeft = startTime1 - timeElapsed;

mins = (byte)(timeLeft / 60000ul);
timeTemp = timeLeft - (mins * 60000);
secs = (byte)(timeTemp / 1000ul);
timeTemp = timeTemp - (secs * 1000ul);

if( mins == 0 && secs == 0 )
{
display.clearDisplay();
sprintf( szString, “DONE” );
Serial.println( szString );
display.setCursor( 10, 10 );
display.print( szString );
display.display();

}//if
else if( secs != lastsecs )
{
lastsecs = secs;
display.clearDisplay();
sprintf( szString, “%02d:%02d”, mins, secs );
Serial.println( szString );
display.setCursor( 30, 10 );
display.print( szString );
display.display();

}//if
}
void loop() {

//Relay 1 :

SwitchState1 = digitalRead(SwitchPin1);

if (SwitchState1 == HIGH)
{
startTime1 = millis();
DoCountdown();
digitalWrite(RelayPin1, HIGH);
}
if (millis() - startTime1 >= interval)
{

digitalWrite(RelayPin1, LOW);
}

//Relay 2 :

SwitchState2 = digitalRead(SwitchPin2);

if (SwitchState2 == HIGH)
{
startTime2 = millis();
DoCountdown();
digitalWrite(RelayPin2, HIGH);
}
if (millis() - startTime2 >= interval)
{

digitalWrite(RelayPin2, LOW);
}

//Relay 3 :

SwitchState3 = digitalRead(SwitchPin3);

if (SwitchState3 == HIGH)
{
startTime3 = millis();
DoCountdown();
digitalWrite(RelayPin3, HIGH);
}
if (millis() - startTime3 >= interval)
{

digitalWrite(RelayPin3, LOW);
}
}

bot does not display properly:

Can you describe what that means or post a photo?
How to post images so we don't have to download them and everyone can see them.
How to post an image.
Another page on posting images.

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Why won't you post the image so we don't have to download it? You will get more help if you don't make it a pain to help you.

So what is wrong? Do you want a decimal point instead of a colon?

Thank you for the help and links as I adjust to posting on this forum. After reading the document on attaching images within posts , hopefully this works.

What you see on this display is a static number. Another words, it does not count down or change. If I trigger a timer, This number may change once, but I do not get the start time to display and count down until it displays "DONE".

Thank you,

Steve

Do serial prints show the countdown properly? What do the serial prints show?

A bit of advice on program development, if you will accept it.

Begin your program by working ONLY on the display until you understand it and can get it to display whatever you want.

Then you can use the display to help debug the rest of your timer code.

Good luck

Paul

Paul_KD7HB:
A bit of advice on program development, if you will accept it.

Begin your program by working ONLY on the display until you understand it and can get it to display whatever you want.

Then you can use the display to help debug the rest of your timer code.

Good luck

Paul

Karma++

@ groundFungus:

The Serial Monitor only displays: "DONE"
I cannot get it to show the time of the countdown when i trigger a timer. If I connect a relay to the timer output as labeled in the code, the relay will trigger and remain energized for the required time.

@ Paul_KD7HB:

Thank you for the advice and yes I am open to the feedback. I appreciate the reply and perhaps I will start over and begin with the display.

Thank you,

Steve

image0.png

image0.png

Please take a minute to read the How To Use This Forum post. it is a sticky post on every forum.
come back and more > modify (bottom right of your post) and add the 'code tags' described in #7 of how to use this forum

as for your simple mistake

  //Relay 1 :
  if (SwitchState1 == HIGH)
  {
    startTime1 = millis();
    DoCountdown();
    digitalWrite(RelayPin1, HIGH);
  }

if SwitchState1 == HIGH, it would mean the switch ( not button) is in a constant state ?
or, are you using a momentary button ?

assuming a switch, then it will force startTime1 to equal millis() for the duration it is HIGH

if SwitchState1 == HIGH
if oldSwitchState1 == LOW
startTime1 = millis();
DoCountdown();
digitalWrite(RelayPin1, HIGH);

now, switchFlag1 will be set HIGH only if the conditions are met.

think about when you turn on the switch, it was low, now high
your turn off the switch, it was high, now low
what would happen if you toggled it wildly a few times ?

do the rest of void loop()
and .....

....at the very end.....

oldSwitchState1 = SwitchState1;
oldSwitchState2 = SwitchState2;
oldSwitchState3 = SwitchState3;
} // end of loop