Hello,
I need help with a part of my arduino program that doesn't work as I want.
Indeed, I have to make a manual and automatic control program. With the help of a keyboard, I have to control the fan speed according to the temperature of a radiator that I display with an LCD screen. The manual control works well. That is to say that I increase the speed or decrease it when I want. However, when I press the # key on the keyboard I should have an automatic regulation. The fan speed should change by itself each time the temperature exceeds a certain value.
My problem is that when I press #, the regulation is done automatically the first time but when the temperature changes again, I have to press # again to have a new regulation when it should be done automatically.
I am attaching my code for you to look at.
Thanks in advance.
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <String.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,20,4); //20 caractères peuvent être affichés sur chacune des 4 lignes de l'écran LCD
float Temperature1 = 0; //Température1
float Temperature2 = 0;
float Temperature3 = 0;
float Temperature4 = 0;
int start = 0; //ventilateur allumé ou non
int pwm_v = 0; // Commande de la vitesse du ventilateur
String T1;
String T2;
String T3;
String T4;
String V;
String Mode;
int relais = 42;
int moteurPin1 = 24;
int moteurPin2 = 25;
int moteurEN = 4 ; //Pin4 PWM
//Configuration du clavier
const byte LIGNES = 4; // 4 lignes
const byte COLONNES = 3; // 3 colonnes
char keys[LIGNES][COLONNES] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[LIGNES] = {7, 8, 9, 10};
byte colPins[COLONNES] = {11, 12, 13};
Keypad CLV = Keypad( makeKeymap(keys), rowPins, colPins, LIGNES, COLONNES);
void setup() {
Serial.begin(9600);
pinMode(relais, OUTPUT);
pinMode(moteurPin1, OUTPUT);
pinMode(moteurPin2, OUTPUT);
pinMode(moteurEN, OUTPUT);
//Transmission des valeurs sur Excel
Serial.println("CLEARDATA");
Serial.println("LABEL, Date, Temps, Te1, Te2, Te3, Te4, pwm");
//Initialisation de l'écran
lcd.init();
lcd.backlight();
}
void loop() {
char key = CLV.getKey();
if (key != NO_KEY){
Serial.println(key);
if (key == '3'){digitalWrite(relais, HIGH);}
if (key == '6'){digitalWrite(relais, LOW);}
}
//1ère partie du code : Evolution des températures sans ventilateur (pas de régulation)
//Lecture des valeurs du capteurs
int reading1 = analogRead (14);
int reading2 = analogRead (15);
// int reading3 = analogRead (2);
// int reading4 = analogRead (3);
//Changement des valeurs de température en degré
Temperature1 = reading1 * (5.0 / 1023.0) * 100.0;
Temperature2 = reading2 * (5.0 / 1023.0) * 100.0;
// Temperature3 = reading3 * (5.0 / 1023.0) * 100.0;
// Temperature4 = reading4 * (5.0 / 1023.0) * 100.0;
//Mettre les valeurs de température dans Excel
Serial.println((String)"DATA, DATE, TIME," + Temperature1 + "," + Temperature2 + "," + pwm_v);
T1 = "T1=";
T1 += Temperature1;
T2 = "T2=";
T2 += Temperature2;
T3 = "T3=";
T3 += Temperature3;
T4 = "T4=";
T4 += Temperature4;
//Intervention du ventilateur
digitalWrite (moteurPin1, 1); // Envoie l'électricité dans in1 donc dans la broche 22 ce qui fait tourner le moteur dans le sens in1
digitalWrite (moteurPin2, 0); // On met le in2 à 0 pour continuer à tourner dans le sens de rotation de in1
analogWrite (moteurEN, pwm_v);
if (key != NO_KEY){
Serial.println(key);
if (key == '4' ) {
pwm_v = 50; //Allumage du ventilateur
start = 1;
V = "V = ";
V += pwm_v;
Mode = "Allu";
}
if (key == '*'){
pwm_v = 0;
start = 0;
V = "V="; //Extinction du ventilateur
V += pwm_v;
Mode = "Stop";
}
if (start == 1 && key == '2'){
pwm_v += 10;
if (pwm_v > 255){pwm_v = 255;}
Mode = "REG MAN";
V = "V=";
V += pwm_v;
}
if (start == 1 && key == '8'){
pwm_v -= 20;
if (pwm_v < 0){pwm_v = 0;}
Mode = "REG MAN";
V = "V=";
V += pwm_v;
}
//Régulation automatique
while(start == 1 && key == '#'){
char key = CLV.getKey();
if (Temperature1 < 25){
pwm_v = 60;
V = "V=";
V += pwm_v;
Mode = "Auto";
}
if (Temperature1 > 25 && Temperature1 < 30){
pwm_v = 75;
V = "V=";
V += pwm_v;
Mode = "Auto";
}
if (Temperature1 > 30){
pwm_v = 150;
V = "V=";
V += pwm_v;
Mode = "Auto";
}
if (key == '9'){
V = "V=";
V += pwm_v;
Mode = "Fin reg";
}
break;
}
}
//Affichage des valeurs sur l'écran
lcd.clear();
lcd.setCursor (0,0);
lcd.print(T1);
lcd.setCursor (0,1);
lcd.print(T2);
lcd.setCursor (9,0);
lcd.print(V);
lcd.setCursor (9,1);
lcd.print(Mode);
delay (250);
}
ProgrammeArd2.ino (4.36 KB)