Hello.
This is my first post here, and english isn’t my first language so please forgive me :).
I have a problem with my code.
First my hardware:
- NodeMCU
- two DS18B20 sensors attatched to pin D5 and 4k7ohm resistor to 3v
- OLED display 128x64 ssd1306 i2c
- momentary switch connected to pin D6 witch 10kohm resistor.
The problem is my temperature won’t refresh. Readings do update just if I cycle trough the cases. I’ve tried using while loop inside case 1 but code stucks there. Could someone push me into right direction?
This is my code:
#include <Wire.h>
#include <SPI.h>
//#include <SimpleTimer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
//SimpleTimer timer;
#define ONE_WIRE_BUS D5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
uint8_t sensor1[8] = { 0x28, 0x00, 0xF9, 0xD5, 0x13, 0x19, 0x01, 0x65 };
uint8_t sensor2[8] = { 0x28, 0x47, 0x43, 0xDD, 0x13, 0x19, 0x01, 0x7B };
float temp_1;
float temp_2;
const int buttonPin = 12;
boolean buttonState = LOW;
boolean lastButtonState = LOW;
int Display = 1;
unsigned long lastDebounceTime = 0;
long debounceDelay = 50;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup(){
pinMode(buttonPin, INPUT);
Serial.begin(9600);
DS18B20.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.println(Display);
display.setRotation(1);
display.clearDisplay();
displayAnimation();
display.display();
}
void loop(void){
Serial.println(Display);
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if(reading == HIGH){
Display ++;
display.clearDisplay();
if (Display > 3){
Display = 1;
}
Serial.println(Display);
switch(Display){
case 1:
while(Display == 1){
DS18B20.requestTemperatures();
temp_1 = DS18B20.getTempC(sensor1);
temp_2 = DS18B20.getTempC(sensor2);
Serial.println(temp_1);
Serial.println(temp_2);
Serial.println(Display);
display.clearDisplay();
display.drawLine(0,32,64,32,WHITE);
display.drawLine(0,96,64,96,WHITE);
display.drawLine(0,64,64,64,WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(2, 9);
display.println(temp_1);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(2, 41);
display.println(temp_2);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(2, 73);
display.println("--.--");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(2, 105);
display.println("--.--");
display.display();
delay(1000);
continue;
}
break;
case 2:
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(2, 41);
display.println("ekran_2");
display.display();
break;
case 3:
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(2, 41);
display.println("ekran_3");
display.display();
break;
}
}
}
}
lastButtonState = reading;
}
Also when I restart NodeMCU, code stops after animation in void setup. Is there a way to move sequention to case 1: immediately after animation without pressing button?