Happy Friday everybody,
Pardon the long code but I have a small issue with the OLED display of an image inside the millis function.
What I would like the image (invoked, for example, in the line "display.drawBitmap(104, 30, AirOn, 16, 16, WHITE);") to do is to remain for the entire duration of the condition. Instead, it flashes once and disappears. Is there a way to make the bitmap image remain solidly in the same way that the lightbulb bitmap does?
I believe the problem begins from the line "if (PUMP_state == LOW)"
Code is below.
Thanks in advance
//LIBRARIES
#include <SPI.h> //Call the SPI library
#include <Wire.h> //Call the Wire library
#include <Adafruit_GFX.h> //Call the Adafruit GFX library
#include <Adafruit_SSD1306.h> //Call the Adafruit SSD1306 library
#include "DHT.h" //Call the DHT library
//SCREEN AND SENSOR DEFINITIONS
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire); //Set dimensions of screen
//#define OLED_RESET 4
#define DHTTYPE DHT11 //Define the type of DHT Sensor
#define DHTPIN 2 //Define its pin
DHT dht(DHTPIN, DHTTYPE);
//PIN DEFINITIONS
int LDRPin = A0;
int PUMP = 3;
int PUMP_state = LOW;
//TIMER VALUES
const unsigned long Pump_ON = 500UL;
const unsigned long Pump_OFF = 2000UL;
unsigned long previousTime = 0;
const unsigned char myImage [] PROGMEM = {
//paste in the HEX code generated from https://javl.github.io/image2cpp/ here
//INTRO SCREEN
};
const unsigned char LightOn [] PROGMEM = {
//paste in the HEX code generated from https://javl.github.io/image2cpp/ here
0x07, 0xc0, 0x0f, 0x20, 0x1f, 0x90, 0x3f, 0xc8, 0x3f, 0xe8, 0x3f, 0xf8, 0x3f, 0xf8, 0x3f, 0xf8,
0x1f, 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x00, 0x00, 0x07, 0xc0, 0x07, 0xc0
};
const unsigned char LightOff [] PROGMEM = {
//paste in the HEX code generated from https://javl.github.io/image2cpp/ here
0x07, 0xc0, 0x08, 0x20, 0x10, 0x10, 0x20, 0x08, 0x20, 0x08, 0x20, 0x08, 0x20, 0x08, 0x20, 0x08,
0x10, 0x10, 0x08, 0x20, 0x04, 0x40, 0x04, 0x40, 0x07, 0xc0, 0x00, 0x00, 0x07, 0xc0, 0x07, 0xc0
};
const unsigned char AirOn [] PROGMEM = {
//paste in the HEX code generated from https://javl.github.io/image2cpp/ here
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x30, 0x00, 0x30, 0x1f, 0xf0, 0x36, 0x98, 0x6a, 0xac, 0x62, 0x9c, 0x2a, 0xa8, 0x1f, 0xf0
};
const unsigned char AirOff [] PROGMEM = {
//paste in the HEX code generated from https://javl.github.io/image2cpp/ here
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x30, 0x00, 0x30, 0x1f, 0xf0, 0x20, 0x08, 0x40, 0x04, 0x40, 0x04, 0x20, 0x08, 0x1f, 0xf0
};
void setup() {
Serial.begin(230400);
pinMode (LDRPin, INPUT);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.clearDisplay(); // Make sure the display is cleared
// Draw the bitmap:
// drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
display.drawBitmap(0, 0, myImage, 64, 64, WHITE); //bitmap width and height are dimensions of image
// Update the display
display.display();
}
void loop() {
unsigned long currentTime = millis();
int LDR_Threshold = 40; //Set to your preference
int LDR_val=analogRead(LDRPin);
Serial.print("LDR value is: ");
Serial.println(LDR_val);
int temp = dht.readTemperature();
int humid = dht.readHumidity();
display.clearDisplay();
display.setTextColor(WHITE);
//Serial.print("Temperature: ");
//Serial.println(temp);
display.setTextSize(1);
display.setCursor(10,0);
display.print("Temp.");
display.setCursor(2,8);
display.setTextSize(2);
display.print(temp);
display.print(char(167));
display.print("C");
display.setTextSize(1);
//Serial.print("Humidity: ");
//Serial.println(humid);
display.setCursor(56,0);
display.print("Humidity");
display.setCursor(60,8);
display.setTextSize(2);
display.print(humid);
display.print("%");
//DRAW THE PUMP TIMER
//drawRoundRect(x, y, width, height, colour)
display.drawRoundRect(0, 38, 100, 8, 2, WHITE);
//fillRoundRect(x, y, width, height, radius, colour)
display.fillRoundRect(0, 38 , 50, 8, 2, WHITE);
display.setCursor (0, 28);
display.setTextSize(1);
display.print("Next stir");
//DRAW THE LIGHT METER
//drawRoundRect(x, y, width, height, colour)
display.drawRoundRect(0, 56, 100, 8, 2, WHITE);
//fillRoundRect(x, y, width, height, radius, colour)
display.fillRoundRect(0, 56,20 + LDR_val * 0.148, 8, 2, WHITE);
display.setCursor (0, 48);
display.setTextSize(1);
display.print("Light Level");
if (LDR_val<LDR_Threshold) {
display.drawBitmap(104, 48, LightOn, 16, 16, WHITE); //bitmap width and height are dimensions of image
display.display();
} else {
display.drawBitmap(104, 48, LightOff, 16, 16, WHITE); //bitmap width and height are dimensions of image
display.display();
}
if (PUMP_state == LOW) {
if((currentTime - previousTime) >= Pump_OFF) {
PUMP_state = HIGH; //change state of pump to ON
display.drawBitmap(104, 30, AirOn, 16, 16, WHITE); //bitmap width and height are dimensions of image
display.display();
previousTime = currentTime;
}
}
else {
if((currentTime - previousTime) >= Pump_ON) {
PUMP_state = LOW; //change state of pump to ON
display.drawBitmap(104, 30, AirOff, 16, 16, WHITE); //bitmap width and height are dimensions of image
display.display();
previousTime = currentTime;
}
display.display();
}
}


