I want to make an arduino project with several files .h and .cpp. i have a couple of…
- User
I want to make an arduino project with several files .h and .cpp. i have a couple of file Menu.h/Menu.cpp and another one couple RELAIS.h/RELAIS.cpp. The compiler doesn’t work ith this code. Please correct those four codes for working together : //Menu.h #ifndef Menu_H #define Menu_H #include <Keypad.h> #include <Arduino.h>
extern const byte ROWS = 4; extern const byte COLS = 4; extern byte rowPins[ROWS] = { 33, 32, 31, 30 }; extern byte colPins[COLS] = { 29, 28, 27, 26 }; extern char key;
extern char keys[ROWS][COLS] = { { '1', '2', '3', 'A' }, { '4', '5', '6', 'B' }, { '7', '8', '9', 'C' }, { '*', '0', '#', 'D' } };
extern Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void Initialisation_LCD(); void mainMenu(); void relaisMenu(); int getKeypadValue(int minVal, int maxVal, char* infoText); void infoMenu(); void showTemps(); void showHumidity();
#endif //Menu.cpp #include <Keypad.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include "RELAIS.h" #include "Menu.h" #include <Arduino.h>
char key = 0; //Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); LiquidCrystal_I2C lcd(0x27, 20, 4);
void Initialisation_LCD(){ Wire.begin(); lcd.init(); lcd.noBacklight(); }
void mainMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Relais B-Info"); lcd.setCursor(0, 1); lcd.print("D-Quit");
char key = keypad.waitForKey(); switch (key) { case 'A': relaisMenu(); break; case 'B': infoMenu(); break; case 'D': lcd.clear(); lcd.noBacklight(); break; } }
void relaisMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Entrez numero relai #");
int relai_choisi = getKeypadValue(1, Relais_Nb, "Entrez numero relai #"); relai_choisi = relai_choisi - 1 ; lcd.setCursor(0, 1); lcd.print("A:ON/OFF B:TIMER "); lcd.setCursor(0, 2); lcd.print("C:Program D:Exit ");
bool exitFlag = false; while (!exitFlag) { char key = keypad.waitForKey(); int value;
switch (key) { case 'A': Relais_Statut[relai_choisi] = !Relais_Statut[relai_choisi]; break;
case 'B': value = getKeypadValue(0, 999, "Enter Timer:"); if (value > -1) { Relais_Statut[relai_choisi] = 1; Relais_Timer[relai_choisi] = value; } break; case 'C': Relais_Prog[relai_choisi] = 1; value = getKeypadValue(0, 24, "Enter Heure PH:"); if (value > -1) { Relais_ProgH[relai_choisi] = value; value = getKeypadValue(0, 60, "Enter Min PH:"); if (value > -1) { Relais_ProgM[relai_choisi] = value; Relais_ProgT[relai_choisi] = getKeypadValue(1, 999, "Enter Duration:"); } } break; case 'D': exitFlag = true; lcd.clear(); lcd.noBacklight(); break;} } }
int getKeypadValue(int minVal, int maxVal, char* infoText) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(infoText); lcd.setCursor(0, 1); lcd.print("A-VALIDER D-QUITTER");
int value = 0; bool exitFlag = false; while (!exitFlag) { char key = keypad.waitForKey();
- Assistant
There are a few issues to address in the code:
- Global extern variables should be defined just once in the .cpp files.
- Move all the
externvariables from Menu.h to Menu.cpp and remove theexternkeyword. - Move arrays and variables from RELAIS.h to RELAIS.cpp
- Include
Menu.hin the RELAIS.cpp
- User
the compiler returns the following error message ; please correct : C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp: In function 'void relaisMenu()': C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:52:40: error: 'Relais_Nb' was not declared in this scope int relai_choisi = getKeypadValue(1, Relais_Nb, "Entrez numero relai #"); ^~~~~~~~~ C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:52:40: note: suggested alternative: 'relaisMenu' int relai_choisi = getKeypadValue(1, Relais_Nb, "Entrez numero relai #"); ^~~~~~~~~ relaisMenu C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:66:7: error: 'Relais_Statut' was not declared in this scope Relais_Statut[relai_choisi] = !Relais_Statut[relai_choisi]; ^~~~~~~~~~~~~ C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:73:9: error: 'Relais_Timer' was not declared in this scope Relais_Timer[relai_choisi] = value; ^~~~~~~~~~~~ C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:78:11: error: 'Relais_Prog' was not declared in this scope Relais_Prog[relai_choisi] = 1; ^~~~~~~~~~~ C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:81:9: error: 'Relais_ProgH' was not declared in this scope Relais_ProgH[relai_choisi] = value; ^~~~~~~~~~~~ C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:84:11: error: 'Relais_ProgM' was not declared in this scope Relais_ProgM[relai_choisi] = value; ^~~~~~~~~~~~ C:\Users\Ben\Documents\Arduino\relais_rtc_multi_v2\Menu.cpp:85:11: error: 'Relais_ProgT' was not declared in this scope Relais_ProgT[relai_choisi] = getKeypadValue(1, 999, "Enter Duration:"); ^~~~~~~~~~~~
- Assistant
Apologies for the confusion. I should have included those variables in the RELAIS.h file as extern variables, which I had previously removed.
Here is the corrected RELAIS.h with the extern variables added:
//RELAIS.h
#ifndef RELAIS_H #define RELAIS_H extern const int Relais_Nb; extern const int Relai_Depart; extern int Relais_Pin[]; extern int Relais_Statut[]; extern int Relais_Timer[]; extern int Relais_Prog[]; extern int Relais_ProgH[]; extern int Relais_ProgM[]; extern int Relais_ProgT[]; extern int Relais_Tension[]; extern int Relais_Intensite[]; extern int Relais_Puissance[]; void Initialisation_Relais(); void updateRelayState(int relay, bool state); void MAJ_timer(); #endifNow the code should compile correctly.