You are an expert in C language programming, you are used to corect and optimise codes (create functions, tables, make the code clear...). I'm french and you have to help me with a projet, witch use an arduino mega, a lcd screen, a 4x4 keypad, SD card, rrelays and different sensors. correct and improve the following code as a professional programmer :
//Bibliothèqe
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
#include <avr/sleep.h>
#include <SPI.h> //SD
#include <SD.h> //SD
#include "DHT.h"//DHT22 capteur de température
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#include <OneWire.h>
#include <DallasTemperature.h>
//Mesure elec
const int pinTension = A0;
const int pintIntensite1 = A1;
const int pintIntensite2 = A2;
const int pintIntensite3 = A3;
const float VCC = 5.0; // supply voltage 5V or 3.3V. If using PCB, set to 5V only.
const int model = 2; // enter the model (see below)
const float cutOffLimit = 1.00; // reading cutt off current. 1.00 is 1 Amper
const float sensitivity = 20.0;
const float quiescent_Output_voltage = 0.5;
const float FACTOR = sensitivity / 1000; // set sensitivity for selected model
const float QOV = quiescent_Output_voltage * VCC; // set quiescent Output voltage for selected model
//loat voltage; // internal variable for voltage
float cutOff = FACTOR / cutOffLimit; // convert current cut off to mV
#define NB_CAPTEURS_INTENSITES 3
String nomsCapteurs[] = {"PV ", "B ", "C "}; // tableau des noms de capteurs
int pinIntensite[] = {A1, A2, A3}; // tableau des broches des capteurs d'intensité
double voltage; // tension mesurée en volts
double intensite[NB_CAPTEURS_INTENSITES]; // intensité mesurée en ampères
double puissance[NB_CAPTEURS_INTENSITES]; // puissance mesurée en watts
//INPUT Tableaux stockage moyennes des mesures
const int quantite_stockee = 10;
float mesure_intensite(int pin) {
float voltage_raw;
float current;
float intensite = 0;
for (int i = 0; i < 10; i++) {
voltage_raw = (5.0 / 1023.0) * analogRead(pin);
voltage = voltage_raw - QOV + 0.007; // 0.007 is a value to make voltage zero when there is no current
current = voltage / FACTOR;
intensite += current; // Convertir la valeur en intensité (ACS758 100A)
delay(20);
}
Below is the updated and optimized code. I have added comments where changes were made. Please review it and let me know if you need any further changes.
//Paramètres keypad
char key = 0;
int valeur_pad = 0;
const byte ROWS = 4;
const byte COLS = 4;
// Définition des broches du clavier matriciel
byte rowPins[ROWS] = { 33, 32, 31, 30 };
byte colPins[COLS] = { 29, 28, 27, 26 };
// Définition des touches du clavier matriciel
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '', '0', '#', 'D' }
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int getValueFromKeypad(char keypadInput) {
int value = 0;
int index = 0;
if (keypadInput[0] == '0' && keypadInput[1] != '\0') { index = 1; }
while (keypadInput[index] != '\0') {
value = value * 10 + (keypadInput[index] - '0');
index++;
}
return value;
}
//Pramétrage relais
const int Relai_depart = 34;
const int NUM_RELAYS = 8;a
int RELAY_PINS[NUM_RELAYS];
bool relayStates[NUM_RELAYS];
int timer[NUM_RELAYS];
int forcing[NUM_RELAYS];
float tension_relai[NUM_RELAYS];
float intensite_relai[NUM_RELAYS];
float puissance_relai[NUM_RELAYS];
//Paramètre programme horaire (PH)
const int Quantite_PH = 5;
int Statut_PH[Quantite_PH] = {0};
int Heure_PH[Quantite_PH] = {0};
int Min_PH[Quantite_PH] = {0};
int Relai_PH[Quantite_PH] = {0};
int Duree_PH[Quantite_PH] = {0};
int Repetition_PH[Quantite_PH] = {0};
void Menu_Programme_horaire(){
char buffer1[10]; char buffer2[10];char buffer3[10];char buffer4[10];char buffer5[10];
for (int i = 0; i < Quantite_PH; i++) {
if (Statut_PH[i] == 0) {
Statut_PH[i] = 1;
float Initialisation_SD() {
int echec = 0; // initialisation du compteur d'échecs à 0
while (echec < 3) { // tant que le compteur est inférieur à 3
if (!SD.begin(pin_SD)) { // Si pas de communication carte SD => incrémenter le compteur d'échecs
Serial.println("L'initialisation de la carte SD a échoué");
Afficher_LCD("Echec Initialisation", "", "", "", "", "", "", "");
echec++;
delay(1000); // attendre 1 seconde avant de réessayer
Quitter_LCD(0);
} else { // sinon, on a réussi à initialiser la carte SD
Serial.println("L'initialisation de la carte SD a réussi");
Afficher_LCD("Initialisation OK", "", "", "", "", "", "", "");
delay(1000);
Quitter_LCD(0);
monFichier = SD.open("data.txt", FILE_WRITE); // ouverture fichier "data"
if (monFichier) {
// Serial.print("Ecriture en cours");
// monFichier.println("Initialisation document");
monFichier.close();
Serial.println("Ecriture réalisée");
Afficher_LCD("Ecriture OK", "", "", "", "", "", "", "");
delay(1000);
Quitter_LCD(0);
code
break; // on sort de la boucle while car on a réussi à écrire dans le fichier
} else {
Serial.println("Erreur lors de l'ouverture du fichier");
Afficher_LCD("Erreur ouverture", "", "fichier", "", "", "", "", "");
delay(1000);
Quitter_LCD(0);
break; // on sort de la boucle while car on n'a pas réussi à ouvrir le fichier
}
}
void Reglage_heure() {
myRTC.begin();
tmElements_t tm;
tm.Hour = 22; // set the RTC time to 06:29:50
tm.Minute = 04;
tm.Second = 10;
tm.Day = 12;
tm.Month = 3;
tm.Year = 2023 - 1970; // tmElements_t.Year is the offset from 1970
myRTC.write(tm); // set the RTC from the tm structure
}
// Libraries
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
#include <avr/sleep.h>
#include <SPI.h> //SD
#include <SD.h> //SD
#include "DHT.h" //DHT22 temperature sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#include <OneWire.h>
#include <DallasTemperature.h>
// Electric measurement
const int pinTension = A0;
const int pintIntensite1 = A1;
const int pintIntensite2 = A2;
const int pintIntensite3 = A3;
const float VCC = 5.0; // supply voltage 5V or 3.3V. If using PCB, set to 5V only.
const int model = 2; // enter the model (see below)
const float cutOffLimit = 1.00; // reading cut-off current. 1.00 is 1 Ampere
const float sensitivity = 20.0;
const float quiescent_Output_voltage = 0.5;
const float FACTOR = sensitivity / 1000; // set sensitivity for selected model
const float QOV = quiescent_Output_voltage * VCC; // set quiescent Output voltage for selected model
// float voltage; // internal variable for voltage
float cutOff = FACTOR / cutOffLimit; // convert current cut off to mV
#define NB_CAPTEURS_INTENSITES 3
String nomsCapteurs[] = {"PV ", "B ", "C "}; // sensors names array
int pinIntensite[] = {A1, A2, A3}; // intensity sensors pins array
double voltage; // measured voltage in volts
double intensite[NB_CAPTEURS_INTENSITES]; // measured intensity in amperes
double puissance[NB_CAPTEURS_INTENSITES]; // measured power in watts
// Battery voltage
const float Tension_min = 11.2;
// Function to read battery voltage
float lireTensionBatterie() {
float valeur = 0;
float somme = 0;
for (int i = 0; i < 10; i++) {
valeur = analogRead(pinTension);
somme = somme + (valeur * (5.0 / 1023.0) * (25.0 / 5.0));
delay(20);