Arduino with I2C Relay, LEDs and LCD

I am using a 8 relay switches to light up 8 LEDs (one switch controls one LED). I am using an IR remote to switch each relay (with a different button for each switch). I am using a switch case for the remote.
I’d like to add an LCD screen showing a progress bar (i.e if four of the switches were on it would show a 50% complete progress bar) but i’m not sure how to do this and would like to avoid using lots/only if statements.

All my code is here https://www.reddit.com/r/learnprogramming/comments/mw4kde/creating_progress_bar_for_arduino_with_i2c_relay/.

Any help would be greatly appreciated!

All your code needs to be here - (in code tags, the </> icon).  Folks are not inclined to go offsite to view sketches.

Try a site search for ‘lcd bar graph’.

Hi, @ari_arduino
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.
OR

Then look down to “code problems” about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :grinning: :+1: :coffee: :australia:

Hi! Thank you for your reply. I tried to post my code but it said I couldn’t post more than two links. I don’t have any links in my code so I’m not sure why this happened. In future posts when I can upload my code file, I will, I just can’t just now as I’m a new user.

Hi Tom, thank you for replying. I did try paste in my code but it said I couldn’t post something with more than two links, I’m not sure why this happened as there’s no links in my code. Is there any other way to make it available for people to see?
Cheers!

Hi,
You need to copy and paste your code from that site into code tags on here. OPs code:

#include "Wire.h"
#include <IRremote.h>
#include <LiquidCrystal_I2C.h> //#include <LiquidCrystal.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

// -------- Relay ----------
#define ON 1
#define OFF 0
#define address 0x20 // i2c slave address from 0x20 to 0x27

unsigned char i;
unsigned char variable_LOW;
unsigned char variable_HIGH;
unsigned int mode_byte; // 16bit unsigned variable
unsigned int FAST;

// -------- Remote ----------
int receiver = 11; // the pin where you connect the output pin of sensor
//int all = 0;
int relay1 = 0;
int relay2 = 0;
int relay3 = 0;
int relay4 = 0;
int relay5 = 0;
int relay6 = 0;
int relay7 = 0;
int relay8 = 0;

int relay = relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8;
//int totalCount = 8;

#define code1 0xC // code received from button no. 1
#define code2 0x18 // code received from button no. 2
#define code3 0x5E // code received from button no. 3
#define code4 0x8 // code received from button no. 4
#define code5 0x1C // code received from button no. 5
#define code6 0x5A // code received from button no. 6
#define code7 0x42 // code received from button no. 7
#define code8 0x52 // code received from button no. 8
#define codeP 0x45 // code received from button no. 8

// -------- LCD ----------
/* NON I2C SET UP initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
//const int rs = 2, e = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
//LiquidCrystal lcd(rs, e, d4, d5, d6, d7); */

// LCD screen configuration function for the progress bar.
// Use custom characters 0 to 5 (6 and 7 remain available).
// These 5 arrays paint the bars that go across the screen.
byte zero[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte one[] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000
};

byte two[] = {
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000
};

byte three[] = {
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100
};

byte four[] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110
};

byte five[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};

// END LCD SET UP


void setup() {
// START Relay Setup
Wire.begin(); // wake up I2C bus
Wire.beginTransmission(0x20);
Wire.write(0x00); // A register
Wire.write(0x00); // set all of port A to outputs
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write((byte)0x01); // B register
Wire.write((byte)0x00); // set all of port B to outputs
Wire.endTransmission();

FAST = 500;
//END Relay Setup

// START IR Setup
Serial.begin(115200);
Serial.println("IR Receive Test");
IrReceiver.begin(receiver, ENABLE_LED_FEEDBACK);
// END IR Setup

// START LCD Setup
lcd.init(); //changing from lcd.begin(16,2) to the init worked but i dont know why
lcd.backlight();
lcd.setCursor(0,0); //Position the text will be displayed
lcd.print("Gyro Test");
lcd.setCursor(0,1);
lcd.print(" By Arianne ");
delay(3000);
lcd.clear();
// initialize the LCD and allocate the 5 arrays to a number (Save custom characters in LCD display memory)
lcd.createChar(0, zero);
lcd.createChar(1, one);
lcd.createChar(2, two);
lcd.createChar(3, three);
lcd.createChar(4, four);
lcd.createChar(5, five);
// END LCD Setup
}


// This function control each channel separately ON/OFF
void ch_mode(unsigned char addr, unsigned int channel, unsigned char mode){

switch (channel) { case 16 : channel = 7; break;
case 15 : channel = 6; break;
case 14 : channel = 5; break;
case 13 : channel = 4; break;
case 12 : channel = 3; break;
case 11 : channel = 2; break;
case 10 : channel = 1; break;
case 9 : channel = 0; break;
case 8 : channel = 15; break;
case 7 : channel = 14; break;
case 6 : channel = 13; break;
case 5 : channel = 12; break;
case 4 : channel = 11; break;
case 3 : channel = 10; break;
case 2 : channel = 9; break;
case 1 : channel = 8; break; }

mode_byte &= ~(1<<(channel));
mode_byte |= mode<<channel;

variable_LOW = lowByte(mode_byte);
variable_HIGH = highByte(mode_byte);

Wire.beginTransmission(addr);
Wire.write(0x12); // address bank A
Wire.write(variable_LOW);
Wire.endTransmission();
Wire.beginTransmission(addr);
Wire.write(0x13); // address bank B
Wire.write(variable_HIGH);
Wire.endTransmission();
}

// This is the method that does all the work on the progress bar.
void updateProgressBar(unsigned long count, int totalCount, int lineToPrintOn) //total count is the total number to count to
{
double factor = totalCount/80.0; //Because I am using a 16 x 2 display, I have 16 characters. Each character has 5 sections. Therefore, I need to declare the number 80.0.
int percent = (count+1)/factor; // count is the current number in the count progress
int relay = percent/5;
int remainder = percent%5;
if(relay > 0)
{
lcd.setCursor(relay-1,lineToPrintOn);
lcd.write(5);
}
lcd.setCursor(relay,lineToPrintOn);
lcd.write(remainder);
}
/*void draw_progressbar (byte percent) {

// Display the new value in numeric form on the first line
lcd.setCursor (0,0);
lcd.print (percent);
lcd.print (F ( "%" ));
// NB The two spaces at the end of the line are used to erase the digits of the previous percentage
// when going from a value with two or three digits to a value with two or one digits.

lcd.setCursor (0,1);

// Map range (0 ~ 100) to range (0 ~ LCD_NB_COLUMNS * 5)
byte nb_columns = map (percent, 0 , 100 , 0 , LCD_NB_COLUMNS * 5 );

//Draw each character in the line
for (byte i = 0 ; i < LCD_NB_COLUMNS; ++ i) {

//Depending on the number of columns remaining to display
if (nb_columns == 0 ) { // Empty case
lcd.write ((byte) 0 );

} else if (nb_columns >= 5 ) { // Full case
lcd.write ( 5 );
nb_columns -= 5 ;

} else { // Last box not empty
lcd.write (nb_columns);
nb_columns = 0 ;
}
}
}*/

void loop()
{
if (IrReceiver.decode()) // have we received an IR signal?
{
switch (IrReceiver.decodedIRData.command)
{
case code1: /* == RELAY 1 RED ==*/
ch_mode(address,1,!relay1);
//relay1 != relay1; for some reason this only turned off but not back on
relay1 = 1- relay1;
break;
case code2: /* == RELAY 2 BLUE ==*/
ch_mode(address,2,!relay2);
//relay2 != relay2;
relay2 = 1-relay2;
break;
case code3: /* == RELAY 3 YELLOW ==*/
ch_mode(address,3,!relay3);
//relay3 != relay3;
relay3 = 1-relay3;
break;
case code4: /* == RELAY 4 GREEN ==*/
ch_mode(address,4,!relay4);
//relay4 != relay4;
relay4 = 1-relay4;
break;
case code5: /* == RELAY 5 RED ==*/
ch_mode(address,5,!relay5);
//relay5 != relay5;
relay5 = 1-relay5;
break;
case code6: /* == RELAY 6 BLUE ==*/
ch_mode(address,6,!relay6);
//relay6 != relay6;
relay6 = 1-relay6;
break;
case code7: /* == RELAY 7 YELLOW ==*/
ch_mode(address,7,!relay7);
//relay7 != relay7;
relay7 = 1-relay7;
break;
case code8: /* == RELAY 8 GREEN ==*/
ch_mode(address,8,!relay8);
//relay8 != relay8;
relay8 = 1-relay8;
break;
case codeP: // All off or all on

//Other attempts in notepad file
if (relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 >= 1)
{
ch_mode(address,8,OFF);
ch_mode(address,7,OFF);
ch_mode(address,6,OFF);
ch_mode(address,5,OFF);
ch_mode(address,4,OFF);
ch_mode(address,3,OFF);
ch_mode(address,2,OFF);
ch_mode(address,1,OFF);

relay8 = 0;
relay7 = 0;
relay6 = 0;
relay5 = 0;
relay4 = 0;
relay3 = 0;
relay2 = 0;
relay1 = 0;
}
else if (relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 0)
{
ch_mode(address,8,ON);
ch_mode(address,7,ON);
ch_mode(address,6,ON);
ch_mode(address,5,ON);
ch_mode(address,4,ON);
ch_mode(address,3,ON);
ch_mode(address,2,ON);
ch_mode(address,1,ON);

relay8 = 1;
relay7 = 1;
relay6 = 1;
relay5 = 1;
relay4 = 1;
relay3 = 1;
relay2 = 1;
relay1 = 1;
}
break;
default:
break;
}
delay(FAST);
IrReceiver.resume();
outputRemote();
}
/* ============ LCD START ============= */
if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 1)
{
lcd.setCursor(1,0);
//lcd.print("One LED");
//lcd.print(" ");
updateProgressBar(10, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 2)
{
lcd.setCursor(2,0);
//lcd.print("Two LED");
//lcd.print(" ");
updateProgressBar(20, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 3)
{
lcd.setCursor(3,0);
//lcd.print("3 LED");
//lcd.print(" ");
updateProgressBar(30, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 4)
{
lcd.setCursor(4,0);
//lcd.print("4 LED");
//lcd.print(" ");
updateProgressBar(40, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 5)
{
lcd.setCursor(5,0);
//lcd.print("Two LED");
//lcd.print(" ");
updateProgressBar(50, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 6)
{
lcd.setCursor(6,0);
//lcd.print("Two LED");
//lcd.print(" ");
updateProgressBar(60, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 7)
{
lcd.setCursor(7,0);
//lcd.print("Two LED");
//lcd.print(" ");
updateProgressBar(70, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
if (relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == 8)
{
lcd.setCursor(8,0);
updateProgressBar(80, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(200);
}
delay(1000);
/* ============ LCD END ============= */
}
void outputRemote() // describing Remote IR codes
{
switch(IrReceiver.decodedIRData.command)
{
case 0x45: Serial.println("POWER"); break;
case 0x47: Serial.println("FUNC/STOP"); break;
case 0x46: Serial.println("VOL+"); break;
case 0x44: Serial.println("REWIND"); break;
case 0x40: Serial.println("PAUSE"); break;
case 0x43: Serial.println("FAST FORWARD"); break;
case 0x7: Serial.println("DOWN"); break;
case 0x15: Serial.println("VOL-"); break;
case 0x9: Serial.println("UP"); break;
case 0x19: Serial.println("EQ"); break;
case 0xD: Serial.println("ST/REPT"); break;
case 0x16: Serial.println("0"); break;
case 0xC: Serial.println("1"); break;
case 0x18: Serial.println("2"); break;
case 0x5E: Serial.println("3"); break;
case 0x8: Serial.println("4"); break;
case 0x1C: Serial.println("5"); break;
case 0x5A: Serial.println("6"); break;
case 0x42: Serial.println("7"); break;
case 0x52: Serial.println("8"); break;
case 0x4A: Serial.println("9"); break;

default:
Serial.println(" other button ");
} //END Switch

delay(FAST); // Do not get immediate repeat

Serial.print("Relay One: ");
Serial.println(digitalRead (relay1));
Serial.print("Relay Two: ");
Serial.println(digitalRead (relay2));
Serial.print("Relay Three: ");
Serial.println(digitalRead (relay3));
Serial.print("Relay Four: ");
Serial.println(digitalRead (relay4));
Serial.print("Relay Five: ");
Serial.println(digitalRead (relay5));
Serial.print("Relay Six: ");
Serial.println(digitalRead (relay6));
Serial.print("Relay Seven: ");
Serial.println(digitalRead (relay7));
Serial.print("Relay Eight: ");
Serial.println(digitalRead (relay8));
}

Hi, @ari_arduino
Here is your code with auto format.

#include "Wire.h"
#include <IRremote.h>
#include <LiquidCrystal_I2C.h> //#include <LiquidCrystal.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

// -------- Relay ----------
#define ON 1
#define OFF 0
#define address 0x20 // i2c slave address from 0x20 to 0x27

unsigned char i;
unsigned char variable_LOW;
unsigned char variable_HIGH;
unsigned int mode_byte; // 16bit unsigned variable
unsigned int FAST;

// -------- Remote ----------
int receiver = 11; // the pin where you connect the output pin of sensor
//int all = 0;
int relay1 = 0;
int relay2 = 0;
int relay3 = 0;
int relay4 = 0;
int relay5 = 0;
int relay6 = 0;
int relay7 = 0;
int relay8 = 0;

int relay = relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8;
//int totalCount = 8;

#define code1 0xC // code received from button no. 1
#define code2 0x18 // code received from button no. 2
#define code3 0x5E // code received from button no. 3
#define code4 0x8 // code received from button no. 4
#define code5 0x1C // code received from button no. 5
#define code6 0x5A // code received from button no. 6
#define code7 0x42 // code received from button no. 7
#define code8 0x52 // code received from button no. 8
#define codeP 0x45 // code received from button no. 8

// -------- LCD ----------
/* NON I2C SET UP initialize the library by associating any needed LCD interface pin
  // with the arduino pin number it is connected to
  //const int rs = 2, e = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
  //LiquidCrystal lcd(rs, e, d4, d5, d6, d7); */

// LCD screen configuration function for the progress bar.
// Use custom characters 0 to 5 (6 and 7 remain available).
// These 5 arrays paint the bars that go across the screen.
byte zero[] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};
byte one[] = {
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000
};

byte two[] = {
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000
};

byte three[] = {
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100
};

byte four[] = {
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110
};

byte five[] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};

// END LCD SET UP


void setup() {
  // START Relay Setup
  Wire.begin(); // wake up I2C bus
  Wire.beginTransmission(0x20);
  Wire.write(0x00); // A register
  Wire.write(0x00); // set all of port A to outputs
  Wire.endTransmission();
  Wire.beginTransmission(0x20);
  Wire.write((byte)0x01); // B register
  Wire.write((byte)0x00); // set all of port B to outputs
  Wire.endTransmission();

  FAST = 500;
  //END Relay Setup

  // START IR Setup
  Serial.begin(115200);
  Serial.println("IR Receive Test");
  IrReceiver.begin(receiver, ENABLE_LED_FEEDBACK);
  // END IR Setup

  // START LCD Setup
  lcd.init(); //changing from lcd.begin(16,2) to the init worked but i dont know why
  lcd.backlight();
  lcd.setCursor(0, 0); //Position the text will be displayed
  lcd.print("Gyro Test");
  lcd.setCursor(0, 1);
  lcd.print(" By Arianne ");
  delay(3000);
  lcd.clear();
  // initialize the LCD and allocate the 5 arrays to a number (Save custom characters in LCD display memory)
  lcd.createChar(0, zero);
  lcd.createChar(1, one);
  lcd.createChar(2, two);
  lcd.createChar(3, three);
  lcd.createChar(4, four);
  lcd.createChar(5, five);
  // END LCD Setup
}


// This function control each channel separately ON/OFF
void ch_mode(unsigned char addr, unsigned int channel, unsigned char mode) {

  switch (channel) {
    case 16 : channel = 7; break;
    case 15 : channel = 6; break;
    case 14 : channel = 5; break;
    case 13 : channel = 4; break;
    case 12 : channel = 3; break;
    case 11 : channel = 2; break;
    case 10 : channel = 1; break;
    case 9 : channel = 0; break;
    case 8 : channel = 15; break;
    case 7 : channel = 14; break;
    case 6 : channel = 13; break;
    case 5 : channel = 12; break;
    case 4 : channel = 11; break;
    case 3 : channel = 10; break;
    case 2 : channel = 9; break;
    case 1 : channel = 8; break;
  }

  mode_byte &= ~(1 << (channel));
  mode_byte |= mode << channel;

  variable_LOW = lowByte(mode_byte);
  variable_HIGH = highByte(mode_byte);

  Wire.beginTransmission(addr);
  Wire.write(0x12); // address bank A
  Wire.write(variable_LOW);
  Wire.endTransmission();
  Wire.beginTransmission(addr);
  Wire.write(0x13); // address bank B
  Wire.write(variable_HIGH);
  Wire.endTransmission();
}

// This is the method that does all the work on the progress bar.
void updateProgressBar(unsigned long count, int totalCount, int lineToPrintOn) //total count is the total number to count to
{
  double factor = totalCount / 80.0; //Because I am using a 16 x 2 display, I have 16 characters. Each character has 5 sections. Therefore, I need to declare the number 80.0.
  int percent = (count + 1) / factor; // count is the current number in the count progress
  int relay = percent / 5;
  int remainder = percent % 5;
  if (relay > 0)
  {
    lcd.setCursor(relay - 1, lineToPrintOn);
    lcd.write(5);
  }
  lcd.setCursor(relay, lineToPrintOn);
  lcd.write(remainder);
}
/*void draw_progressbar (byte percent) {

  // Display the new value in numeric form on the first line
  lcd.setCursor (0,0);
  lcd.print (percent);
  lcd.print (F ( "%" ));
  // NB The two spaces at the end of the line are used to erase the digits of the previous percentage
  // when going from a value with two or three digits to a value with two or one digits.

  lcd.setCursor (0,1);

  // Map range (0 ~ 100) to range (0 ~ LCD_NB_COLUMNS * 5)
  byte nb_columns = map (percent, 0 , 100 , 0 , LCD_NB_COLUMNS * 5 );

  //Draw each character in the line
  for (byte i = 0 ; i < LCD_NB_COLUMNS; ++ i) {

  //Depending on the number of columns remaining to display
  if (nb_columns == 0 ) { // Empty case
  lcd.write ((byte) 0 );

  } else if (nb_columns >= 5 ) { // Full case
  lcd.write ( 5 );
  nb_columns -= 5 ;

  } else { // Last box not empty
  lcd.write (nb_columns);
  nb_columns = 0 ;
  }
  }
  }*/

void loop()
{
  if (IrReceiver.decode()) // have we received an IR signal?
  {
    switch (IrReceiver.decodedIRData.command)
    {
      case code1: /* == RELAY 1 RED ==*/
        ch_mode(address, 1, !relay1);
        //relay1 != relay1; for some reason this only turned off but not back on
        relay1 = 1 - relay1;
        break;
      case code2: /* == RELAY 2 BLUE ==*/
        ch_mode(address, 2, !relay2);
        //relay2 != relay2;
        relay2 = 1 - relay2;
        break;
      case code3: /* == RELAY 3 YELLOW ==*/
        ch_mode(address, 3, !relay3);
        //relay3 != relay3;
        relay3 = 1 - relay3;
        break;
      case code4: /* == RELAY 4 GREEN ==*/
        ch_mode(address, 4, !relay4);
        //relay4 != relay4;
        relay4 = 1 - relay4;
        break;
      case code5: /* == RELAY 5 RED ==*/
        ch_mode(address, 5, !relay5);
        //relay5 != relay5;
        relay5 = 1 - relay5;
        break;
      case code6: /* == RELAY 6 BLUE ==*/
        ch_mode(address, 6, !relay6);
        //relay6 != relay6;
        relay6 = 1 - relay6;
        break;
      case code7: /* == RELAY 7 YELLOW ==*/
        ch_mode(address, 7, !relay7);
        //relay7 != relay7;
        relay7 = 1 - relay7;
        break;
      case code8: /* == RELAY 8 GREEN ==*/
        ch_mode(address, 8, !relay8);
        //relay8 != relay8;
        relay8 = 1 - relay8;
        break;
      case codeP: // All off or all on

        //Other attempts in notepad file
        if (relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 >= 1)
        {
          ch_mode(address, 8, OFF);
          ch_mode(address, 7, OFF);
          ch_mode(address, 6, OFF);
          ch_mode(address, 5, OFF);
          ch_mode(address, 4, OFF);
          ch_mode(address, 3, OFF);
          ch_mode(address, 2, OFF);
          ch_mode(address, 1, OFF);

          relay8 = 0;
          relay7 = 0;
          relay6 = 0;
          relay5 = 0;
          relay4 = 0;
          relay3 = 0;
          relay2 = 0;
          relay1 = 0;
        }
        else if (relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 0)
        {
          ch_mode(address, 8, ON);
          ch_mode(address, 7, ON);
          ch_mode(address, 6, ON);
          ch_mode(address, 5, ON);
          ch_mode(address, 4, ON);
          ch_mode(address, 3, ON);
          ch_mode(address, 2, ON);
          ch_mode(address, 1, ON);

          relay8 = 1;
          relay7 = 1;
          relay6 = 1;
          relay5 = 1;
          relay4 = 1;
          relay3 = 1;
          relay2 = 1;
          relay1 = 1;
        }
        break;
      default:
        break;
    }
    delay(FAST);
    IrReceiver.resume();
    outputRemote();
  }
  /* ============ LCD START ============= */
  if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 1)
  {
    lcd.setCursor(1, 0);
    //lcd.print("One LED");
    //lcd.print(" ");
    updateProgressBar(10, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 2)
  {
    lcd.setCursor(2, 0);
    //lcd.print("Two LED");
    //lcd.print(" ");
    updateProgressBar(20, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 3)
  {
    lcd.setCursor(3, 0);
    //lcd.print("3 LED");
    //lcd.print(" ");
    updateProgressBar(30, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 4)
  {
    lcd.setCursor(4, 0);
    //lcd.print("4 LED");
    //lcd.print(" ");
    updateProgressBar(40, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 5)
  {
    lcd.setCursor(5, 0);
    //lcd.print("Two LED");
    //lcd.print(" ");
    updateProgressBar(50, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 6)
  {
    lcd.setCursor(6, 0);
    //lcd.print("Two LED");
    //lcd.print(" ");
    updateProgressBar(60, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  if (/*relay1 + relay2 + relay3 + relay4 + relay5 +relay6 + relay7 + relay8 == i &&*/ relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 7)
  {
    lcd.setCursor(7, 0);
    //lcd.print("Two LED");
    //lcd.print(" ");
    updateProgressBar(70, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  if (relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8 == 8)
  {
    lcd.setCursor(8, 0);
    updateProgressBar(80, 100, 0); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
    delay(200);
  }
  delay(1000);
  /* ============ LCD END ============= */
}
void outputRemote() // describing Remote IR codes
{
  switch (IrReceiver.decodedIRData.command)
  {
    case 0x45: Serial.println("POWER"); break;
    case 0x47: Serial.println("FUNC/STOP"); break;
    case 0x46: Serial.println("VOL+"); break;
    case 0x44: Serial.println("REWIND"); break;
    case 0x40: Serial.println("PAUSE"); break;
    case 0x43: Serial.println("FAST FORWARD"); break;
    case 0x7: Serial.println("DOWN"); break;
    case 0x15: Serial.println("VOL-"); break;
    case 0x9: Serial.println("UP"); break;
    case 0x19: Serial.println("EQ"); break;
    case 0xD: Serial.println("ST/REPT"); break;
    case 0x16: Serial.println("0"); break;
    case 0xC: Serial.println("1"); break;
    case 0x18: Serial.println("2"); break;
    case 0x5E: Serial.println("3"); break;
    case 0x8: Serial.println("4"); break;
    case 0x1C: Serial.println("5"); break;
    case 0x5A: Serial.println("6"); break;
    case 0x42: Serial.println("7"); break;
    case 0x52: Serial.println("8"); break;
    case 0x4A: Serial.println("9"); break;

    default:
      Serial.println(" other button ");
  } //END Switch

  delay(FAST); // Do not get immediate repeat

  Serial.print("Relay One: ");
  Serial.println(digitalRead (relay1));
  Serial.print("Relay Two: ");
  Serial.println(digitalRead (relay2));
  Serial.print("Relay Three: ");
  Serial.println(digitalRead (relay3));
  Serial.print("Relay Four: ");
  Serial.println(digitalRead (relay4));
  Serial.print("Relay Five: ");
  Serial.println(digitalRead (relay5));
  Serial.print("Relay Six: ");
  Serial.println(digitalRead (relay6));
  Serial.print("Relay Seven: ");
  Serial.println(digitalRead (relay7));
  Serial.print("Relay Eight: ");
  Serial.println(digitalRead (relay8));
}

Thanks @kgray9
Tom… :grinning: :+1: :coffee: :australia:

@TomGeorge @kgray9
Thank you! I really appreciate that!

Hi,
I think you used this tutorial. I would try this one on Arduino Project Hub. It uses a library and is very easy to add to your code. You probably should first remove all of the LCD code you have right now, then add that code from the tutorial.

So I tried to use this one as a jumping off point. That tutorial only plays a increasing/ decreasing progress bar animation not as a result of an event. I’m not sure how to edit it so that when I switch on a relay about 10% of the progress bar populates and stays there either until that switch is turned off or another turns on so it then reads 20% etc

Hi,
I checked it out and his code looks a little complicated/messy. I would just use the library.

The <LiquidCrystal_I2C.h> library? I can print words to the LCD, I just don’t know how to create the progress bar

Hi,
Google :goggles:

arduino progress bar lcd

There are even YouTube videos on how to do it.

Tom… :grinning: :+1: :coffee: :australia:

Hi,
I meant this library: LcdProgressBar - Arduino Libraries

I’ve been googling for three days :cry: :cry:

Most of the tutorials are just animations and not progress bars representing something actually happening. I found one were the progress bar was controlled using a potentiometer and it displayed voltage.
I tried to convert this to suit my code and downloaded the extra library it required but I had 0 luck. And only the words displayed - no progress bar.
My I2C LCD is connected through the relay which is then connected to the I2C pins on the Arduino uno (daisy chained) and because of this I thought using an array or a counter would be the best way to measure change for each relay but I don’t know how to do this when the outputs (LEDs) aren’t plugged directly into the arduino (each LED is connected to an individual switch on the relay board )

Hi,

Can you post a circuit diagram please?
Why have you got an I2C connection through a relay?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

(sorry I didn’t have the relay board or I2C LCD so I had to improvise with the drawing hahah)

I have the LCD going through the relay as the Arduino only has one VCC, one GND, one SDA pin and one SCL pin. The relay has these connections: one set to connect to the arduino and two more so you can ‘daisy chain’.

This is the relay board I’m using: https://www.tindie.com/products/bugrovs2012/16-channel-i2c-electromagnetic-relay-module-iot/

Hi,
Draw your circuit with pen(cil) and paper and post a picture of it please.

@ari_arduino relay board.

Have you been able to print anything in the LCD display?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

What would you like to see in more detail? Because a hand drawing would be the same to the one above

And yes! I can print text to the LCD screen. And i’ve managed to make squares light up in attempts to create a progress bar but they are all over the place (and uses a lot of if statements)