I was working on a code to get the climate in my greenhouse automated. Everything was working fine until yesterday, i uploaded this code:
#include <BH1750.h>
#include <SoftwareSerial.h>
#include <MHZ.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SparkFunHTU21D.h"
#include <DS3231.h>
HTU21D myHumidity;
DS3231 rtc(SDA, SCL);
BH1750 lightMeter;
MHZ co2(6, MHZ14A);
Time t;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);
unsigned long prevTime = 0;
int SSD_State = 0;
bool vocht;
void setup() {
Serial.begin(9600);
myHumidity.begin();
rtc.begin();
lightMeter.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
pinMode(2, OUTPUT); // R2.4 Verdamper
pinMode(3, OUTPUT); // R2.3 Exit Fan
pinMode(4, OUTPUT); // R2.2 Luchtcirculatie
pinMode(5, OUTPUT); // R2.1 Entry Fan
pinMode(6, INPUT); // PWM CO2 Meter
pinMode(10, OUTPUT); // R1.4 -
pinMode(11, OUTPUT); // R1.3 -
pinMode(12, OUTPUT); // R1.2 -
//RTC Instellen van de TIJD
rtc.setDOW(FRIDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(15, 10, 50); // Set the time to 12:00:00 (24hr format)
rtc.setDate(5, 5, 2023); // Set the date to January 1st, 2014
}
void loop() {
t = rtc.getTime();
int ppm_pwm = co2.readCO2PWM();
float lux = lightMeter.readLightLevel();
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();
Serial.print(SSD_State);
Serial.print(" Vocht: ");
Serial.println(vocht);
if (millis() - prevTime >= 5000) {
SSD_State = (SSD_State+1)%5;
Serial.println(SSD_State);
prevTime = millis();
}
// display time
if (SSD_State == 0){
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Tijd:");
display.setTextSize(3);
display.setCursor(12,11);
display.print(t.hour);
display.setCursor(42,11);
display.print(":");
display.setCursor(54,11);
display.print(t.min);
display.display();
}
// display temperature
if (SSD_State == 1) {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperatuur:");
display.setTextSize(3);
display.setCursor(12,11);
display.print(temp);
display.print(" ");
display.setTextSize(1);
display.setCursor(105,13);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
display.display();
}
// display humidititty
if (SSD_State == 2){
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Luchtvochtigheid:");
display.setTextSize(3);
display.setCursor(12,11);
display.print(humd);
display.print(" ");
display.setTextSize(1);
display.setCursor(105,13);
display.setTextSize(2);
display.print("%");
display.display();
}
// Display CO2
if (SSD_State == 3){
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("CO2 gehalte:");
display.setTextSize(3);
display.setCursor(12,11);
display.print(ppm_pwm);
display.display();
}
// Display Light intensity
if (SSD_State == 4){
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Lichtintensiteit:");
display.setTextSize(3);
display.setCursor(12,11);
display.print(lux);
display.display();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ EFFECTIEVE REGELING >\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Luchtbevochter
if(lux >= 100) { //Aanschakelen aan de hand van LICHTSTERKTE
if(humd <= 78){
digitalWrite(2, LOW);
vocht = true;
}
if(humd >= 82){
digitalWrite(2, HIGH);
}
else{
digitalWrite(2, HIGH);
}
}
// Entry Fan
if(lux >= 100){
digitalWrite(5, LOW);
}else{
digitalWrite(5, HIGH);
}
// Exit Fan
if(temp >= 29.0 || humd >= 85){
digitalWrite(3, LOW);
}else {
digitalWrite(3, HIGH);
}
// Luchtcirculatie ventilator
if(lux >= 100){ //Tijd wanneer ventilator aanstaat
digitalWrite(4, LOW);
} else{
digitalWrite(4, HIGH);
}
}
I first made the code for all my sensors including:
- MHZ-14a
- HTU21D
- DS3231
- BH1750
OLED display included, then i combined the code with the part that controls all devices. Separatly the codes worked fine. Until i combined them, nothing would work like it was supposed to, but now my biggest problem that i cannot upload a new sketch to the arduino uno R3. Even when the IDE says the new code has been succesfully uploaded, the old code shown above keeps running.
I've mentioned this in another topic, but i thought i'd make a new one since they're different problems now.