indicators for relays on an oled-display

This is all new to me, so Im spending alot of time learning basics at the moment :slight_smile:

I have four relays connected and an 0,96" oled display and I wish to have some sort of an indication for each relay if they are active or not. At the moment I have a number "1" for Relay1 when its active and number "0" when its not. But instead of updating the display is showing both numbers at the same time, as if it dosnt update with the relay.
Not all that simple to explain, but here is my code so far:

#include <Sodaq_DS3231.h>

#include <dht.h>

#include "U8glib.h"

#include <Button.h>

U8GLIB_SSD1306_128X64 oled(U8G_I2C_OPT_NONE);

Sodaq_DS3231 RTC;

byte hours;
byte minutes;
byte seconds;

dht DHT;

#define DHT11_PIN 4

#define RELAY_ON 1
#define RELAY_OFF 0

#define Button_ON 1
#define Button_OFF 0

#define Relay_1 5
#define Relay_2 6
#define Relay_3 9
#define Relay_4 10

Button Purge(2);

void setup() {

Purge.begin();

//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);

//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
delay(4000); //Check that all relays are inactive at Reset

}
void loop() {
updateTime();

oled.firstPage();
DateTime now = rtc.now();

// Kanal #1
if (now.minute() == 00) {
digitalWrite(Relay_1, RELAY_ON);
}
if (now.minute() == 01) {
digitalWrite(Relay_1, RELAY_OFF);
}

// Kanal #2
if (now.minute() == 15) {
digitalWrite(Relay_2, RELAY_ON);
}
if (now.minute() == 16) {
digitalWrite(Relay_2, RELAY_OFF);
}

// Kanal #3
if (now.minute() == 30) {
digitalWrite(Relay_3, RELAY_ON);
}
if (now.minute() == 31) {
digitalWrite(Relay_3, RELAY_OFF);
}

// Kanal #4
if (now.minute() == 45) {
digitalWrite(Relay_4, RELAY_ON);
}
if (now.minute() == 46) {
digitalWrite(Relay_4, RELAY_OFF);
}

// Purga ut vattnet i 15 sekunder
else if(Purge.pressed()) {
digitalWrite(Relay_1, RELAY_ON);

digitalWrite(Relay_2, RELAY_ON);

digitalWrite(Relay_3, RELAY_ON);

digitalWrite(Relay_4, RELAY_ON);
delay(15000);
digitalWrite(Relay_1, RELAY_OFF);

digitalWrite(Relay_2, RELAY_OFF);

digitalWrite(Relay_3, RELAY_OFF);

digitalWrite(Relay_4, RELAY_OFF);
}

do {
draw();
}
while (oled.nextPage());
delay(50);

}

void updateTime() {
DateTime now = rtc.now();
hours = now.hour();
minutes = now.minute();
seconds = now.second();

}

void draw(void) {
oled.setFont(u8g_font_fub17);

char timeString[10];

sprintf(timeString, "%02u:%02u:%02u", hours, minutes, seconds);
oled.setPrintPos(20, 32);
oled.print(timeString);

oled.setFont(u8g_font_fixed_v0);
int chk = DHT.read11(DHT11_PIN);

oled.setPrintPos(0, 52);
oled.print("Temp: ");
oled.print(DHT.temperature);
oled.print(" c");

oled.setPrintPos(0, 64);
oled.print("Humid: ");
oled.print(DHT.humidity);
oled.print("%");
delay(130);

oled.drawLine(0, 10, 128, 10);
oled.drawLine(0, 37, 128, 37);
oled.drawLine(86, 37, 86, 64);
oled.setPrintPos(95, 58);
oled.print("08:00");

DateTime now = rtc.now();
oled.setFont(u8g_font_fixed_v0);

//Kanal #1 GUI
if (Relay_1, RELAY_ON) {
oled.setPrintPos(0, 8);
oled.print("1");
}
if (Relay_1, RELAY_OFF); {
oled.setPrintPos(0, 8);
oled.print("0");
}

//Kanal #2 GUI
if (Relay_2, RELAY_ON) {
oled.setPrintPos(42, 8);
oled.print("2");
}
if (Relay_2, RELAY_OFF); {
oled.setPrintPos(42, 8);
oled.print("0");
}

//Kanal #3 GUI
if (Relay_3, RELAY_ON) {
oled.setPrintPos(86, 8);
oled.print("3");
}
if (Relay_3, RELAY_OFF); {

oled.setPrintPos(86, 8);
oled.print("0");
}

//Kanal #4 GUI
if (Relay_4, RELAY_ON) {
oled.setPrintPos(122, 8);
oled.print("4");
}
if (Relay_4, RELAY_OFF); {
oled.setPrintPos(122, 8);
oled.print("0");
}

}

I do understand, that this is for sure not going to happen my way. But how would you do this in the best and simplest way so that there is some sort of a indication for the 4 relays on the oled?

Did you place all those 8) there on purpose? Don't think so. Please edit your post to include code tags. See How to use this forum.

An already two tips

Once you start numbering variables, arrays, always arrays.

And instead of all those macros (#define) a const byte is preferred in C++. Macros can give you quite some headaches and C++ has some fine and more save alternatives :slight_smile:

instead of:

if (Relay_1, RELAY_ON) {
    oled.setPrintPos(0, 8);
    oled.print("1");
}

try:

if (Relay_1 == RELAY_ON) {
    oled.setPrintPos(0, 8);
    oled.print("1");
}

Sorry for that!

#include <Sodaq_DS3231.h>

#include <dht.h>

#include "U8glib.h"

#include <Button.h>

U8GLIB_SSD1306_128X64 oled(U8G_I2C_OPT_NONE);

Sodaq_DS3231 RTC;



byte hours;
byte minutes;
byte seconds;

dht DHT;

#define DHT11_PIN 4

#define RELAY_ON 1
#define RELAY_OFF 0

#define Button_ON 1
#define Button_OFF 0

#define Relay_1 5
#define Relay_2 6
#define Relay_3 9
#define Relay_4 10

Button Purge(2);


void setup() {

  Purge.begin();
  
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);

  //---( THEN set pins as outputs )----
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  delay(4000); //Check that all relays are inactive at Reset

  
}
void loop() {
  updateTime();
 
  oled.firstPage();
 DateTime now = rtc.now();


 
  // Kanal #1
  if (now.minute() == 00) {
    digitalWrite(Relay_1, RELAY_ON);   
  }
  if (now.minute() == 01) {
    digitalWrite(Relay_1, RELAY_OFF);   
  }


  // Kanal #2
  if (now.minute() == 15) {
    digitalWrite(Relay_2, RELAY_ON);
  }
  if (now.minute() == 16) {
    digitalWrite(Relay_2, RELAY_OFF);
  }


  // Kanal #3
  if (now.minute() == 30) {
    digitalWrite(Relay_3, RELAY_ON);
  }
  if (now.minute() == 31) {
    digitalWrite(Relay_3, RELAY_OFF);
  }
  

  // Kanal #4
  if (now.minute() == 45) {
    digitalWrite(Relay_4, RELAY_ON);
  }
  if (now.minute() == 46) {
    digitalWrite(Relay_4, RELAY_OFF);
  }

  

  
  // Purga ut vattnet i 15 sekunder
  else if(Purge.pressed()) {
    digitalWrite(Relay_1, RELAY_ON);
    
    digitalWrite(Relay_2, RELAY_ON);
    
    digitalWrite(Relay_3, RELAY_ON);
    
    digitalWrite(Relay_4, RELAY_ON);
    delay(15000);
    digitalWrite(Relay_1, RELAY_OFF);
    
    digitalWrite(Relay_2, RELAY_OFF);
    
    digitalWrite(Relay_3, RELAY_OFF);
    
    digitalWrite(Relay_4, RELAY_OFF);
}

  do {
    draw();
  }
  while (oled.nextPage());
  delay(50);


}

void updateTime() {
  DateTime now = rtc.now();
  hours = now.hour();
  minutes = now.minute();
  seconds = now.second();

}

void draw(void) {
  oled.setFont(u8g_font_fub17);

  char timeString[10];


  sprintf(timeString, "%02u:%02u:%02u", hours, minutes, seconds);
  oled.setPrintPos(20, 32);
  oled.print(timeString);

  oled.setFont(u8g_font_fixed_v0);
  int chk = DHT.read11(DHT11_PIN);

  oled.setPrintPos(0, 52);
  oled.print("Temp: ");
  oled.print(DHT.temperature);
  oled.print(" c");

  oled.setPrintPos(0, 64);
  oled.print("Humid: ");
  oled.print(DHT.humidity);
  oled.print("%");
  delay(130);

  oled.drawLine(0, 10, 128, 10);
  oled.drawLine(0, 37, 128, 37);
  oled.drawLine(86, 37, 86, 64);
  oled.setPrintPos(95, 58);
  oled.print("08:00");


  
  DateTime now = rtc.now();
  oled.setFont(u8g_font_fixed_v0);


  //Kanal #1 GUI
  if (Relay_1, RELAY_ON) {
    oled.setPrintPos(0, 8);
    oled.print("1");
  }
  if (Relay_1, RELAY_OFF); {
    oled.setPrintPos(0, 8);
    oled.print("0");
  }
  

  //Kanal #2 GUI
  if (Relay_2, RELAY_ON) {
    oled.setPrintPos(42, 8);
    oled.print("2");
  }
  if (Relay_2, RELAY_OFF); {
    oled.setPrintPos(42, 8);
    oled.print("0");
  }  


  //Kanal #3 GUI
  if (Relay_3, RELAY_ON) {
    oled.setPrintPos(86, 8);
    oled.print("3");
  }
  if (Relay_3, RELAY_OFF); {
    
    oled.setPrintPos(86, 8);
    oled.print("0");
  }

  
  //Kanal #4 GUI
  if (Relay_4, RELAY_ON) {
    oled.setPrintPos(122, 8);
    oled.print("4");
  }
  if (Relay_4, RELAY_OFF); {
    oled.setPrintPos(122, 8);
    oled.print("0");
  }


  }

6v6gt:
instead of:

if (Relay_1, RELAY_ON) {

oled.setPrintPos(0, 8);
   oled.print("1");
}




try:


if (Relay_1 == RELAY_ON) {
   oled.setPrintPos(0, 8);
   oled.print("1");
}

Nah, Then the relay indicates to be off the whole time. Thanks for the respons though! :smiley:

OK. I'll try again.
You did change all the if statements which contained the comma operator ? The one I gave was just an example. Temperature and Humidity display correctly ?
Anyway, your construct:

//Kanal #1 GUI
  if (Relay_1, RELAY_ON) {
    oled.setPrintPos(0, 8);
    oled.print("1");
  }
  if (Relay_1, RELAY_OFF); {
    oled.setPrintPos(0, 8);
    oled.print("0");
  }

Would more usually be written so:

//Kanal #1 GUI
  if (Relay_1 == RELAY_ON) {
    oled.setPrintPos(0, 8);
    oled.print("1");
  }
  else {
    oled.setPrintPos(0, 8);
    oled.print("0");
  }

etc.
This also gets rid of the stray ";" you have at the end of every second if statement as in:

if (Relay_1, RELAY_OFF); {

I'm actually surprised that the "if" statement compiled and I wonder how the compiler would have interpreted them.

Same Result like before, all the Relays are showing "0" for indication.
And the temperature and humidity seem to be written in a different way. I dont have any ifs there.

#include <Sodaq_DS3231.h>

#include <dht.h>

#include "U8glib.h"

#include <Button.h>

U8GLIB_SSD1306_128X64 oled(U8G_I2C_OPT_NONE);

Sodaq_DS3231 RTC;



byte hours;
byte minutes;
byte seconds;

dht DHT;

#define DHT11_PIN 4

#define RELAY_ON 1
#define RELAY_OFF 0

#define Button_ON 1
#define Button_OFF 0

#define Relay_1 5
#define Relay_2 6
#define Relay_3 9
#define Relay_4 10

Button Purge(2);


void setup() {

  Purge.begin();
  
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);

  //---( THEN set pins as outputs )----
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  delay(4000); //Check that all relays are inactive at Reset

  
}
void loop() {
  updateTime();
 
  oled.firstPage();
 DateTime now = rtc.now();


 
  // Kanal #1
  if (now.minute() == 00) {
    digitalWrite(Relay_1, RELAY_ON);   
  }
  if (now.minute() == 01) {
    digitalWrite(Relay_1, RELAY_OFF);   
  }


  // Kanal #2
  if (now.minute() == 15) {
    digitalWrite(Relay_2, RELAY_ON);
  }
  if (now.minute() == 16) {
    digitalWrite(Relay_2, RELAY_OFF);
  }


  // Kanal #3
  if (now.minute() == 30) {
    digitalWrite(Relay_3, RELAY_ON);
  }
  if (now.minute() == 31) {
    digitalWrite(Relay_3, RELAY_OFF);
  }
  

  // Kanal #4
  if (now.minute() == 45) {
    digitalWrite(Relay_4, RELAY_ON);
  }
  if (now.minute() == 46) {
    digitalWrite(Relay_4, RELAY_OFF);
  }

  

  
  // Purga ut vattnet i 15 sekunder
  if  (Purge.pressed()) {
    digitalWrite(Relay_1, RELAY_ON);
    
    digitalWrite(Relay_2, RELAY_ON);
    
    digitalWrite(Relay_3, RELAY_ON);
    
    digitalWrite(Relay_4, RELAY_ON);
    delay(15000);
    digitalWrite(Relay_1, RELAY_OFF);
    
    digitalWrite(Relay_2, RELAY_OFF);
    
    digitalWrite(Relay_3, RELAY_OFF);
    
    digitalWrite(Relay_4, RELAY_OFF);
}

  do {
    draw();
  }
  while (oled.nextPage());
  delay(50);


}

void updateTime() {
  DateTime now = rtc.now();
  hours = now.hour();
  minutes = now.minute();
  seconds = now.second();

}

void draw(void) {
  oled.setFont(u8g_font_fub17);

  char timeString[10];


  sprintf(timeString, "%02u:%02u:%02u", hours, minutes, seconds);
  oled.setPrintPos(20, 32);
  oled.print(timeString);

  oled.setFont(u8g_font_fixed_v0);
  int chk = DHT.read11(DHT11_PIN);

  oled.setPrintPos(0, 52);
  oled.print("Temp: ");
  oled.print(DHT.temperature);
  oled.print(" c");

  oled.setPrintPos(0, 64);
  oled.print("Humid: ");
  oled.print(DHT.humidity);
  oled.print("%");
  delay(130);

  oled.drawLine(0, 10, 128, 10);
  oled.drawLine(0, 37, 128, 37);
  oled.drawLine(86, 37, 86, 64);
  oled.setPrintPos(95, 58);
  oled.print("08:00");


  
  DateTime now = rtc.now();
  oled.setFont(u8g_font_fixed_v0);


  //Kanal #1 GUI
  if (Relay_1 == RELAY_ON) {
    oled.setPrintPos(0, 8);
    oled.print("1");
  }
  else {
    oled.setPrintPos(0, 8);
    oled.print("0");
  }
  

  //Kanal #2 GUI
  if (Relay_2 == RELAY_ON) {
    oled.setPrintPos(42, 8);
    oled.print("2");
  }
  else {
    oled.setPrintPos(42, 8);
    oled.print("0");
  }  


  //Kanal #3 GUI
  if (Relay_3 == RELAY_ON) {
    oled.setPrintPos(86, 8);
    oled.print("3");
  }
  else {
    
    oled.setPrintPos(86, 8);
    oled.print("0");
  }

  
  //Kanal #4 GUI
  if (Relay_4 == RELAY_ON) {
    oled.setPrintPos(122, 8);
    oled.print("4");
  }
  else {
    oled.setPrintPos(122, 8);
    oled.print("0");
  }


  }
  delay(4000); //Check that all relays are inactive at Reset

Do you REALLY believe that that is what that code does?

#define RELAY_ON 1
#define Relay_1 5

  if (Relay_1 == RELAY_ON) {

What are the odds that 5 will equal 1? Not very good, I'd say. Don't you think you should read the STATE of the Relay_1 pin and compare that STATE to RELAY_ON?

Well it would be pretty cool if it did :stuck_out_tongue:

I copied and paste some of the stuff in this code, so there might be some bits and pieces which I could manage without.

And I changed the "if" statements, so it now says Relay 1 equals 5. But unfortunately it dosnt change much other then that Im getting the opposite outcome. The display are now showing that the relays are active at all times even though they are not.

And I changed the "if" statements, so it now says Relay 1 equals 5. But unfortunately it dosnt change much other then that Im getting the opposite outcome. The display are now showing that the relays are active at all times even though they are not.

So, you changed the code incorrectly, and it still doesn't work. Bummer.

I have this crazy idea. Perhaps you could post the modified code.

TheSweeed:
Well it would be pretty cool if it did :stuck_out_tongue:

I copied and paste some of the stuff in this code, so there might be some bits and pieces which I could manage without.

And I changed the "if" statements, so it now says Relay 1 equals 5. But unfortunately it dosnt change much other then that Im getting the opposite outcome. The display are now showing that the relays are active at all times even though they are not.

you are missing out on a lot of convenience by not using sequential pins or arrays...

uint8_t pins[] = {8, 9, 10, 11};
// blah blah

void setup() 
{
  // blah blah
}

void loop() 
{
  // blah blah
  for(uint8_t i = 0; i < sizeof(pins)/sizeof(pins[0]); i++)
  {
    oled.setPrintPos(42 * i, 8);
    oled.print(1 << i & PORTB ? 1 + i: 0); 
  }
}

even without polling the port directly and using your pin numbers, using arrays makes it a heck of a lot easier!