Hi, I have small problem with my code. I have nokia display, some sensors and buttons.
In my code is Nokia 5110 display menu - you can control the menu with buttons - but when I want to control it like pressing down and up buttons it has small delays and it is unconfortable and laggy. Is there some solution to avoid it?
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Fonts/nokiafc22.h>
const unsigned char teplota [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x6d, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc,
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8c, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x1e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
0x3f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x7f, 0xb0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x7f, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x7f, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x3f, 0x30, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x1e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x80, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
boolean backlight = true;
int contrast = 50;
int menuitem = 1;
int page = 1;
volatile boolean up = false;
volatile boolean down = false;
volatile boolean middle = false;
int downButtonState = 0;
int upButtonState = 0;
int selectButtonState = 0;
int lastDownButtonState = 0;
int lastSelectButtonState = 0;
int lastUpButtonState = 0;
Adafruit_PCD8544 display = Adafruit_PCD8544(13, 11, 10, 9, 8);
//BLYNK
#define lmPin A9
#define lmPin2 A10
#define BLYNK_PRINT Serial
#define EspSerial Serial1
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
char ssid[] = "xxxxxxx";
char pass[] = "xxxxxxx";
//char ssid[] = "xxxxxx";
//char pass[] = "xxxxxx";
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxx";
BlynkTimer timer;
// DHT
#include <DHT.h>
#define DHTPIN 22
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//LED
#define RED_LED 6
#define BLUE_LED 5
#define GREEN_LED 7
#define RED_LED2 3
#define BLUE_LED2 2
#define GREEN_LED2 4
//BMP
#include <BMP280.h>
BMP280 bmp;
#define P0 1013.25
double T = 0;
double P = 0;
char measure = 0;
//TEMT6000
#define analogPin A4
int analogHodnota;
int prepocet;
//LM35
float tem = 0;
long lmVal = 0;
float tem2 = 0;
long lmVal2 = 0;
void sendLM35()
{
lmVal = analogRead(lmPin); // přečíst hodnotu pinu lmPin
tem = (lmVal * 0.0048828125 * 100); //
lmVal2 = analogRead(lmPin2); // přečíst hodnotu pinu lmPin
tem2 = (lmVal2 * 0.0048828125 * 100); //
Blynk.virtualWrite(V8, tem);
Blynk.virtualWrite(V9, tem2);
}
void sendDHT()
{
float h = dht.readHumidity();
float t2 = dht.readTemperature();
if (isnan(h) || isnan(t2)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V2, h);
Blynk.virtualWrite(V3, t2 - 2.5);
}
void sendBMP()
{
measure = bmp.startMeasurment();
if (measure != 0) {
delay(measure);
measure = bmp.getTemperatureAndPressure(T, P);
if (measure != 0) {
P = P + 20.7; // '+17' is a correction for the sensor error
T = T - 2.6; // like said above
Blynk.virtualWrite(V5, T);
Blynk.virtualWrite(V4, P);
Serial.println(T);
Serial.println(P);
}
else
Serial.println("Error.");
}
else
Serial.println("Error.");
}
void sendLIGHT()
{
analogHodnota = analogRead(analogPin);
prepocet = map(analogHodnota, 0, 1023, 0, 100);
Blynk.virtualWrite(V6, prepocet);
}
BLYNK_WRITE(V10) // zeRGBa assigned to V1
{
// get a RED channel value
int r = param[0].asInt();
// get a GREEN channel value
int g = param[1].asInt();
// get a BLUE channel value
int b = param[2].asInt();
analogWrite(RED_LED, r);
analogWrite(BLUE_LED, b);
analogWrite(GREEN_LED, g);
}
BLYNK_WRITE(V11) // zeRGBa assigned to V1
{
// get a RED channel value
int r2 = param[0].asInt();
// get a GREEN channel value
int g2 = param[1].asInt();
// get a BLUE channel value
int b2 = param[2].asInt();
analogWrite(RED_LED2, r2);
analogWrite(BLUE_LED2, b2);
analogWrite(GREEN_LED2, g2);
}
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED2, OUTPUT);
pinMode(BLUE_LED2, OUTPUT);
pinMode(GREEN_LED2, OUTPUT);
pinMode(48, INPUT_PULLUP);
pinMode(50, INPUT_PULLUP);
pinMode(52, INPUT_PULLUP);
pinMode(13, OUTPUT);
digitalWrite(13, LOW); //Turn Backlight ON
Serial.begin(9600);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass, "89.185.250.251", 8080);
dht.begin();
if (!bmp.begin()) {
delay(1000);
Serial.println("Init. failed.");
delay(1000);
Serial.println("Check wiring.");
while (1);
}
else {
Serial.println("Init. OK.");
}
bmp.setOversampling(4);
delay(2000);
sendBMP();
timer.setInterval(1500L, sendDHT);
timer.setInterval(5000L, sendBMP);
timer.setInterval(2000L, sendLIGHT);
timer.setInterval(3000L, sendLM35);
timer.setInterval(100L, initialize);
display.begin();
display.setContrast(contrast); //Set contrast to 50
display.clearDisplay();
display.display();
}
void loop() {
Blynk.run();
timer.run();
}
void checkIfDownButtonIsPressed()
{
if (downButtonState != lastDownButtonState)
{
if (downButtonState == 0)
{
down = true;
}
delay(50);
}
lastDownButtonState = downButtonState;
}
void checkIfUpButtonIsPressed()
{
if (upButtonState != lastUpButtonState)
{
if (upButtonState == 0) {
up = true;
}
delay(50);
}
lastUpButtonState = upButtonState;
}
void checkIfSelectButtonIsPressed()
{
if (selectButtonState != lastSelectButtonState)
{
if (selectButtonState == 0) {
middle = true;
}
delay(50);
}
lastSelectButtonState = selectButtonState;
}
void drawMenu()
{
if (page == 4)
{ display.setTextSize(1);
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.print("Teplota - byt");
display.setFont();
display.display();
}
if (page == 3)
{ display.setTextSize(1);
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.setFont(&nokiafc224pt7b);
display.drawBitmap(0, 0, teplota, 84, 48, BLACK);
display.setFont();
display.display();
}
else if (page == 2)
{
display.setTextSize(1);
display.clearDisplay();
display.fillRect(0, 0, 84, 9, BLACK);
display.setTextColor(WHITE, BLACK);
display.setFont(&nokiafc224pt7b);
display.setCursor(1, 7);
display.print("12:35:33");
display.setCursor(57, 7);
display.print("MENU");
if (menuitem == 1)
{
display.drawTriangle(0, 15, 0, 21, 5, 18, BLACK);
}
if (menuitem == 2)
{
display.drawTriangle(0, 25, 0, 31, 5, 28, BLACK);
}
if (menuitem == 3)
{
display.drawTriangle(0, 35, 0, 41, 5, 38, BLACK);
}
display.setTextColor(BLACK, WHITE);
display.setCursor(7, 21);
display.print("Hodiny");
display.setCursor(7, 31);
display.print("Teplota doma");
display.setCursor(7, 41);
display.print("Venkovni tepl.");
display.display();
}
else if (page == 1)
{
display.setTextSize(1);
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.setCursor(6, 3);
display.print("St 12.4.2019");
display.drawFastHLine(3, 12, 78, BLACK);
display.setCursor(2, 25);
display.setFont(&nokiafc224pt7b);
display.setTextSize(2);
display.print("12:35:33");
display.setFont();
display.setTextSize(1);
display.setCursor(30, 40);
display.print("Menu");
display.display();
}
}
void resetDefaults()
{
contrast = 50;
setContrast();
backlight = true;
turnBacklightOn();
}
void setContrast()
{
display.setContrast(contrast);
display.display();
}
void turnBacklightOn()
{
digitalWrite(13, LOW);
}
void turnBacklightOff()
{
digitalWrite(13, HIGH);
}
void initialize() {
drawMenu();
downButtonState = digitalRead(52);
selectButtonState = digitalRead(48);
upButtonState = digitalRead(50);
checkIfDownButtonIsPressed();
checkIfUpButtonIsPressed();
checkIfSelectButtonIsPressed();
if (up && page == 2 ) {
up = false;
menuitem--;
if (menuitem == 0)
{
menuitem = 3;
}
}
else if (up && page == 1 ) {
up = false;
}
else if (up && page == 3 ) {
up = false;
}
if (down && page == 2) {
down = false;
menuitem++;
if (menuitem == 4)
{
menuitem = 1;
}
}
else if (down && page == 1 ) {
down = false;
}
else if (down && page == 3 ) {
down = false;
}
if (middle) {
middle = false;
if (page == 2 && menuitem == 1) {
page = 1;
}
else if (page == 2 && menuitem == 2) {
page = 3;
}
else if (page == 2 && menuitem == 3) {
page = 4;
}
else if (page == 1) {
page = 2;
}
else if (page == 3) {
page = 2;
}
else if (page == 4) {
page = 2;
}
}
}