Trouble With Compiler:

Hello :slight_smile:
I’m new at C but I tried to modify this Code :

Code

to Control the Relays 1-4 with the Buttons. When I compile the following Error messages are shown:

Arduino: 1.6.1 (Windows 7), Platine: “Arduino Uno”

Relay_Test.ino: In function ‘void loop()’:

Relay_Test.ino:57:17: error: statement cannot resolve address of overloaded function

Relay_Test.ino:64:17: error: statement cannot resolve address of overloaded function

Relay_Test.ino:71:17: error: statement cannot resolve address of overloaded function

Relay_Test.ino:78:17: error: statement cannot resolve address of overloaded function

Relay_Test.ino:85:17: error: statement cannot resolve address of overloaded function

Relay_Test.ino:91:17: error: statement cannot resolve address of overloaded function

What is wrong ? You are my last opinion- I searched everywhere, compairing to other Codes

Luca :slight_smile:

#include <LiquidCrystal.h>             //Sample using LiquidCrystal library

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);   // select the pins used on the LCD panel

// define some values used by the panel and buttons
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
int lcd_key     = 0;
int adc_key_in  = 0;





int read_LCD_buttons()                    //function for detection of pressed keypad button
{
 adc_key_in = analogRead(0);              // read the analog value from the sensor 
 if (adc_key_in > 1000) return btnNONE;   // We make this the 1st option for speed reasons since it will be the most likely result
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
 return btnNONE;  // when all others fail, return this...
}
int R1  =    0 ;
int R2  =    1 ;
int R3  =    2 ;
int R4  =    3 ;
void setup()

{ 
 lcd.begin(16, 2);                // start the LCD library
 lcd.setCursor(0,0);              // set cursor position at start
 pinMode( 0 , OUTPUT);
 pinMode( 1 , OUTPUT);
 pinMode( 2 , OUTPUT);
 pinMode( 3 , OUTPUT);
}

void loop(){

 
 
 lcd.setCursor(0,0);         
 lcd.print(analogRead(0));
        
 lcd.setCursor(7,0);            
 lcd_key = read_LCD_buttons();  // read the buttons function
 switch (lcd_key)               // depending on which button was pushed, we perform an action
 { case btnRIGHT:
     { 
       lcd.clear;
       lcd.print("Relay 1");
       digitalWrite( 0, HIGH);
       break;
     }
   case btnLEFT:
     { 
       lcd.clear;
       lcd.print("Relay 2");
       digitalWrite( 1, HIGH);
       break;
     }
   case btnUP:
     { 
       lcd.clear;
       lcd.print("Relay 3");
       digitalWrite( 2, HIGH);
       break;
     }
   case btnDOWN:
     { 
       lcd.clear;
       lcd.print("Relay 4");
       digitalWrite( 3, HIGH);
       break;
     }
   case btnSELECT:
     { 
       lcd.clear;
       lcd.print("All low ");
       break;
     }
     case btnNONE:
     { 
       lcd.clear;
       lcd.print("NONE  ");
       break;
     }
 }
 delay(40);  //wait 40ms
}

Line 57:

       lcd.clear;

You are missing something, like some of these: ()

Oh sh*** thank you :slight_smile:

OK fixed it, but the relays wont wortk …

   case btnSELECT:
     { 
       lcd.clear;
       lcd.print("All low ");
       break;
     }

The Arduino can not read what you wrote to the LCD. You need to actually do that!

Are the relays really activated by HIGH? Most relays on shields are activated by LOW.

Hi,

Also read this.

I would change it to

int Relay1 = 2 ; // a name for everyone 
int Relay2  =  3 ;
int Relay3  =  4 ;
int Relay4  =  5 ;

Also you use r1 - r 4

you had better use these names throughout the code

 pinMode(Relay1 , OUTPUT);
 pinMode(Relay2 , OUTPUT);
 pinMode(Relay3, OUTPUT);
 pinMode(Relay4 , OUTPUT);

Correct the rest yourself then.

OK I finally did this and it works :slight_smile: Thank you

#include <LiquidCrystal.h>             //Sample using LiquidCrystal library

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);   // select the pins used on the LCD panel

// define some values used by the panel and buttons
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
int lcd_key     = 0;
int adc_key_in  = 0;





int read_LCD_buttons()                    //function for detection of pressed keypad button
{
 adc_key_in = analogRead(0);              // read the analog value from the sensor 
 if (adc_key_in > 1000) return btnNONE;   // We make this the 1st option for speed reasons since it will be the most likely result
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
 return btnNONE;  // when all others fail, return this...
}
int R1  =    0 ;
int R2  =    1 ;
int R3  =    2 ;
int R4  =    3 ;
void setup()

{ 
 lcd.begin(16, 2);                // start the LCD library
 lcd.setCursor(0,0);              // set cursor position at start
 pinMode( 0 , OUTPUT);
 pinMode( 1 , OUTPUT);
 pinMode( 2 , OUTPUT);
 pinMode( 3 , OUTPUT);
}

void loop(){

 
 
 lcd.setCursor(0,0);         
 lcd.print(analogRead(0));
        
 lcd.setCursor(7,0);            
 lcd_key = read_LCD_buttons();  // read the buttons function
 switch (lcd_key)               // depending on which button was pushed, we perform an action
 { case btnLEFT:
     { 
       lcd.clear();
       lcd.print("Relay 2 HIGH");
       digitalWrite( 2, HIGH);
       break;
     }
   case btnUP:
     { 
       lcd.clear();
       lcd.print("Relay 3 HIGH");
       digitalWrite( 1, HIGH);
       break;
     }
   case btnRIGHT:
     { 
       lcd.clear();
       lcd.print("Relay 4 HIGH");
       digitalWrite( 0, HIGH);
       break;
     }
   case btnSELECT:
     { 
       lcd.clear();
       lcd.print("Relay 1 HIGH");
       digitalWrite( 3, HIGH);
       break;
     }
   case btnNONE:
     { 
       lcd.clear();
       lcd.print("All low ");
       digitalWrite(0, LOW);
       digitalWrite(1, LOW);
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       
       break;
     }
    case btnDOWN:
    { lcd.clear();
      lcd.print("NOT USED");
    }
    }
 delay(30);  //wait 40ms
}

lupo99:
OK I finally did this and it works :slight_smile:

Please be aware that relay circuits may have different switching logic:

  • switching “active LOW” ==> most relay boards with mechanical relays
  • switching “active HIGH” ==> i.e. Solid State Relays
    If your relay is “active LOW” switching, please be careful about the correct setup initialization, or otherwise you might get relay clacking at power-on!

I have rewritten your code for another example.
You can define in the code whether the relay type is “active HIGH” or “active LOW” switching:

#include <LiquidCrystal.h>             //Sample using LiquidCrystal library

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);   // select the pins used on the LCD panel

// define some values used by the panel and buttons
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
int lcd_key     = 0;
int adc_key_in  = 0;

byte relayPins[]={0,1,2,3};
#define NUMRELAYS (sizeof(relayPins)/sizeof(relayPins[0]))
boolean relayState[NUMRELAYS];
#define RELAY_ACTIVE LOW  // most mechanical relay boards are "active LOW" switching

int read_LCD_buttons()                    //function for detection of pressed keypad button
{
 adc_key_in = analogRead(0);              // read the analog value from the sensor 
 if (adc_key_in > 1000) return btnNONE;   // We make this the 1st option for speed reasons since it will be the most likely result
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
 return btnNONE;  // when all others fail, return this...
}

void relayOutput()
{
  lcd.setCursor(0,0);
  for (int i=0;i<NUMRELAYS;i++)
  {
    if (relayState[i]==true)
    {
      lcd.print("ON  ");
      digitalWrite(relayPins[i], RELAY_ACTIVE);
    }
    else
    {
      lcd.print("OFF ");
      digitalWrite(relayPins[i], !RELAY_ACTIVE);
    }
  }
}

void setup()
{ 
 lcd.begin(16, 2);                // start the LCD library
 lcd.setCursor(0,0);              // set cursor position at start
 for (int i=0;i<NUMRELAYS;i++)
 {
   digitalWrite(relayPins[i] , !RELAY_ACTIVE); // necessary for "active LOW" relays!
   pinMode(relayPins[i] , OUTPUT);
 }
 relayOutput();
}

void loop()
{
  lcd_key = read_LCD_buttons();  // read the buttons function
 
  if (lcd_key<NUMRELAYS) // depending on which button was pushed
  {
    relayState[lcd_key] = !relayState[lcd_key]; // toggle relay state
    relayOutput();
  }

 // wait for button release 
 while (lcd_key!=btnNONE) lcd_key = read_LCD_buttons(); 
}

Thank you :slight_smile:
Works better … Now I can Let the Relay on and it wont switch again after releasing the Button

You saved me a lot of time Dude and helped me to understand more of Arduino :slight_smile:

But how to change the Order of the buttons allocating them to the order of the Display states

lupo99:
But how to change the Order of the buttons allocating them to the order of the Display states

Say what? The switches have physical locations on the shield. The meaning that you associate with each switch is entirely up to you.

lupo99:
But how to change the Order of the buttons allocating them to the order of the Display states

The order of buttons is declared in the 'relayPins' array:

byte relayPins[]={0,1,2,3};

You could easily change the array to use different pins (that are not already in use by the shield):

byte relayPins[]={2,3,A4,A5};

Or you could also change the order of relay pins assigned to the buttons

byte relayPins[]={A4,2,A5,3};

As your buttons are defined like that:

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3

The relayPins[0] pin belongs to btnRIGHT
The relayPins[1] pin belongs to btnUP
The relayPins[2] pin belongs to btnDOWN
The relayPins[3] pin belongs to btnLEFT
Just from left to right in the relayPins[3] array.