Hello everyone,
I am facing a strange problem. The code I wrote (which is not yet complete) works fine but as soon as I uncheck the comments on lines 124 and 125 I have nothing left on the SSD1306 display. That said, if I comment out other things (eg the StartUp routine), I have the same phenomenon. As if the program is lost when displayed (but continues to run anyway).
Can you help me ?
Please, I am new to arduino, I started writing in C 2 weeks ago.
Thanks for your help.
Steve
[code]
/************************************************************************************
Code pour ARduino Uno (Webasto contôle)
TITLE : Start Stop du webasto, contôle des températures : intérieures, extérieurs, webasto out
Contrôle depuis l'application "Blink"
Date de création : 07.08.2021 - V0
Date de modification : - V
************************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
//Auto Control settings
byte cpt;
const float maxTemp = 32.5;
const float minTemp = 31.7;
int adresse = 0;
byte val_EEPROM;
bool Alim_Flag = LOW;
byte bouton;
byte CPT_menu;
bool Erreur_Flag = LOW;
bool Err_Flag = LOW;
byte menu;
bool Webasto_Flag = LOW;
bool Pompe_Flag = LOW;
bool Test_Flag = LOW;
//Define PIN constant
const int Alim = 2;
const int Webasto = 3;
const int RELAY_PIN_3 = 4;
const int RELAY_PIN_4 = 5;
const int RELAY_PIN_5 = 6;
const int Pompe = 7;
const int Enter = 8;
const int Up = 9;
const int Down = 10;
const int mswitch_5 = 11;
const int cmode = 12;
const int smode = 13;
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTPIN_EXT A1
#define DHTPIN_D_G A2
#define DHTPIN_D_P A3
#define DHTTYPE DHT22 // DHT 22
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels 32
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DHT dht0(DHTPIN_EXT, DHTTYPE);
DHT dht1(DHTPIN_D_G, DHTTYPE);
DHT dht2(DHTPIN_D_P, DHTTYPE);
//DHT dht(DHTPIN, DHTTYPE);
DHT dht[3] = {
{DHTPIN_EXT, DHT22},
{DHTPIN_D_G, DHT22},
{DHTPIN_D_P, DHT22},
};
float temperature[3];
char toggleState_1 = 'a'; //Define integer to remember the toggle state for switch 1
char toggleState_2 = 'b'; //Define integer to remember the toggle state for switch 2
char toggleState_3 = 'c'; //Define integer to remember the toggle state for switch 3
char toggleState_4 = 'd'; //Define integer to remember the toggle state for switch 4
char toggleState_5 = 'e'; //Define integer to remember the toggle state for switch 5
String pinStatus = "abcde000.0000.00";
int switchMode = 0;
int ModeFlag = 0;
float temperatureRead;
float humidityRead;
float ldrVal;
String displayText;
/******************************************************************************************/
void setup() {
pinMode(Pompe, INPUT);
pinMode(Enter, INPUT);
pinMode(Up, INPUT);
pinMode(Down, INPUT);
pinMode(Alim, OUTPUT);
pinMode(Webasto, OUTPUT);
Serial.begin(9600);
// Initialise la communication I²C à l’adresse 0x3C.
display.begin( SSD1306_SWITCHCAPVCC, 0x3C );
display.clearDisplay();
for (auto& sensor : dht) {
sensor.begin();
}
}
/******************************************************************************************/
void loop() {
//StartUp();
//WiFi Control
if (Serial.available() > 0)
{
char c_val = Serial.read(); // reading the data received from the ESP01
switch (c_val)
{
//case 'A': digitalWrite(Alim, HIGH); toggleState_1 = 'A'; Serial.println ( "A on" ); break; // when A is pressed on the app Turn on Pin 2
//case 'a': digitalWrite(Alim, LOW); toggleState_1 = 'a'; Serial.println ( "a off" ); break; // when a is pressed on the app Turn off Pin 2
case 'B': digitalWrite(Webasto, HIGH); toggleState_2 = 'B'; Serial.println ( "B on" ); break; // when B is pressed on the app Turn on Pin 3
case 'b': digitalWrite(Webasto, LOW); toggleState_2 = 'b'; Serial.println ( "b off" ); break; // when b is pressed on the app Turn off Pin 3
case 'C': digitalWrite(RELAY_PIN_3, HIGH); toggleState_3 = 'C'; Serial.println ( "C on" ); break; // when C is pressed on the app Turn on Pin 4
case 'c': digitalWrite(RELAY_PIN_3, LOW); toggleState_3 = 'c'; Serial.println ( "c off" ); break; // when c is pressed on the app Turn off Pin 4
case 'D': digitalWrite(RELAY_PIN_4, HIGH); toggleState_4 = 'D'; Serial.println ( "D on" ); break; // when D is pressed on the app Turn on Pin 5
case 'd': digitalWrite(RELAY_PIN_4, LOW); toggleState_4 = 'd'; Serial.println ( "d off" ); break; // when d is pressed on the app Turn off Pin 5
case 'E': digitalWrite(RELAY_PIN_5, HIGH); toggleState_5 = 'E'; Serial.println ( "E on" ); break; // when E is pressed on the app Turn on Pin 6
case 'e': digitalWrite(RELAY_PIN_5, LOW); toggleState_5 = 'e'; Serial.println ( "e off" ); break; // when e is pressed on the app Turn off Pin 6
default : break;
}
delay(100);
}
Serial.print("A");
Serial.print(cpt);
// Effectue un envoi des données toutes les x secondes
if (++cpt >= 10) {
cpt = 0;
readSensors();
displayData();
gestion();
//sendStatus();
}
delay(100);
}
/******************************************************************************************/
/* Foramatage de l'EEPROM au tout 1er start (après programmation) */
void StartUp() {
if (val_EEPROM = EEPROM.read(250) != 10) {
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 10);
}
Serial.println("EEPROM formated");
//Valeurs par défaut des consignes
EEPROM.put(10, 28.5); // Start Alim génerale
EEPROM.put(14, 30.0); // Stop Alim générale
EEPROM.put(18, 28.0); // Start Webasto
EEPROM.put(22, 29.0); // Stop Webasto
EEPROM.put(26, 30.0); // Température intérieur dôme
}
}
/******************************************************************************************/
//Lecture de la température des différents sensors et stock les valeurs dans un tableau de float.
void readSensors() {
for (int i = 0; i < 3; i++) {
temperature[i] = dht[i].readTemperature();
if (isnan(temperature[i])) { //if (isnan(humidity[i]) || isnan(temperature[i])) {
//Serial.print("Failed to read from DHT sensor!");
//Serial.println(i);
//return;
}
else {
temperatureRead = temperature[i];
}
}
}
/******************************************************************************************/
void displayData() {
// readSensors();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("T.ext ");
display.print(temperature[0]);
display.print(" ");
display.print("C ");
display.setCursor(0, 10);
display.print("T.g.dome ");
display.print(temperature[1]);
display.print(" ");
display.print("C ");
display.setCursor(0, 20);
display.print("T.p.dome ");
if (isnan(temperature[2])) {
display.print("Error");
}
else {
display.print(temperature[2]);
display.print(" ");
display.print("C ");
}
display.drawLine(0, 30, display.width() - 1, 30, WHITE);
if (Pompe_Flag == LOW) {
display.setCursor(0, 33);
display.print("Error Webasto");
}
display.display();
}
/******************************************************************************************/
void sendStatus() {
displayData();
pinStatus = String(toggleState_1) + String(toggleState_2) + String(toggleState_3) + String(toggleState_4) + String(toggleState_5) + String("0") + String(temperatureRead) + String(humidityRead);
//pinStatus ="abcdE023.1055.00";
Serial.print(pinStatus);
}
/******************************************************************************************/
//Dès que la température extérieur est en dessous de X, allumer l'alimentation. Sauf si celle-ci est déjà allumée
//Dès que la température extérieur est en dessous de X, allumer le webasto
void gestion() {
float eevalue;
if (temperature[0] <= EEPROM.get(10, eevalue)) { //Température extérieur plus petite
if (Alim_Flag == LOW) {
digitalWrite(Alim, HIGH);
for (int compteur = 5; compteur > 0; compteur--) { //50
display.clearDisplay();
display.setTextSize(1);
display.setCursor(8, 2);
display.print("Alim turn ON");
display.setCursor(10, 15);
display.print(compteur);
display.display();
delay(1000);
}
Alim_Flag = HIGH;
}
if (Erreur_Flag == LOW) {
if (temperature[0] <= EEPROM.get(18, eevalue)) {
if (Webasto_Flag == LOW) {
digitalWrite(Webasto, HIGH);
delay(2000);
digitalWrite(Webasto, LOW);
Webasto_Flag = HIGH;
for (int compteur = 3; compteur > 0; compteur--) { //30
display.clearDisplay();
display.setTextSize(1);
display.setCursor(8, 2);
display.print("Webasto turn ON");
display.setCursor(10, 15);
display.print(compteur);
display.display();
delay(1000);
}
}
else {
if (temperature[1] >= EEPROM.get(22, eevalue)) { //Température à l'intérieur trop haute alors que dehors il fait trop froid.
goto stopWeb; //Le soleil tape sur la serre. Donc stopper le webasto
}
}
}
}
else {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(8, 2);
display.print("Erreur sonde Ext");
display.display();
delay(1000);
}
}
else {
if ((temperature[0] >= EEPROM.get(14, eevalue)) && (Webasto_Flag == LOW)) {
digitalWrite(Alim, LOW);
Alim_Flag = LOW;
}
}
//Stop webasto
if ((temperature[0] >= EEPROM.get(22, eevalue)) && (Webasto_Flag == HIGH) && (Pompe_Flag == HIGH)) {
stopWeb:
digitalWrite(Webasto, HIGH);
delay(3000);
digitalWrite(Webasto, LOW);
for (int compteur = 18; compteur > 0; compteur--) { //18
display.clearDisplay();
display.setTextSize(1);
display.setCursor(8, 2);
display.print("Webasto turn OFF");
display.setCursor(10, 15);
display.print(compteur);
display.display();
delay(1000);
}
Webasto_Flag = LOW;
}
Pompe_Flag = digitalRead(Pompe);
}
[/code]