Hei. I struggling to make the oled display still show the temperature and the analogRead from a potentiometer while it is executing a WHILE-LOOP. Is it possible to make the oled display jump over the While loop?
#include <Wire.h>
#include <Adafruit_SSD1306.h>
/*Temp Sensor*/
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(7); // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
analogWrite(3, 0);
pinMode(A7, INPUT_PULLUP);
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
sensors.begin(); // initialize the sensor
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(3.2);
oled.setRotation(2);// text size
oled.setTextColor(WHITE); // text color
// text to display
}
void loop() {
sensors.requestTemperatures();
int tempCelsius = sensors.getTempCByIndex(0);
oled.setCursor(0, 0); // position to display
oled.println(tempCelsius);
oled.display();// show on OLED
oled.clearDisplay();
oled.setCursor(35,0);
oled.print('C');
int tempKontroll = analogRead(A7);
int tempvalue = map(tempKontroll, 0, 1023, 20, 0);
oled.setCursor(85,0);
oled.println(tempvalue);
if (tempvalue < tempCelsius){while (tempvalue < tempCelsius) {analogWrite(3, 50);
oled.setCursor(0, 0); // position to display
oled.println(tempCelsius);
oled.display();// show on OLED
oled.clearDisplay();
oled.setCursor(35,0);
oled.print('C');
int tempKontroll = analogRead(A7);
int tempvalue = map(tempKontroll, 0, 1023, 20, 0);
oled.setCursor(85,0);
oled.println(tempvalue);
delay(5000);}}
else{analogWrite(3, 0);}
}```
Your while loop introduces two new variables with the same names as the loop control variables.
This means that they are not in scope for the while condition
N.B. an if" followed by a "while" with the same condition is usually redundant.
int tempvalue = map(tempKontroll, 0, 1023, 20, 0);
using the variable-type left from the variable-name means declare a variabe
Within different curly braces { } you can have the same variable name
because the variable is only "visible" inside this curly braces the variable is declared in
Using the variable-name without the type means assign a new value to an existing variable.
anyway if you want to do more things than a single thing
non-blocking code is the way to go.
This requires to learn something new
first thing to learn is :
avoid each and every while-loop
avoid each and every for-loop (except for iterating very fast through arrays)
If you need to repeat a part of the code let do void loop ITSELF do the looping
If this code shall not always be repeated make the code-execution of this part of the code conditional
As the next step you should post a normal worded description what your code shall do.
It is very important that your potential helpers do really understand what you want to do.
Based on this functional description this will be possible.
Hello! Thank you for all the good answers. I can see that many of you don't understand what I'm asking about, so I will try to explain the whole project. I am making a device that controls a Peltier element in a fridge using a MOSFET. I also have a small OLED display that I want to show the temperature from the temperature sensor, and I also want it to show the analogRead from a potentiometer. The plan for the potentiometer is that I can set the desired temperature in the fridge using the potentiometer. I want to give the Peltier element 0.7V every 5 minutes until it reaches the same number as the potentiometer.
When I wrote the last part of the code (from the if(tempvalue < ... )), the OLED display becomes very laggy and the temperature freezes on the display. I think this has something to do with the delay.
I was wondering if it was possible to have the OLED display the temperature and potentiometer number without freezing in some way.
You seem to still hanging on to the idea that your code can keep most of his structure
and just to a small modification to make the display updating the values.
This seems to block your thinking. Just like delay() blocks code-execution.
The way to do the opposite of "freezing the display" which is
the way of keeping the display active and responsive is:
avoid each and every while-loop
by letting do void loop() all the looping.
avoid each and every delay() by
replacing
all the delay()'s with non-blocking timing based on function millis()
From this you can see your code needs re-structuring
for making the code non-blocking = responsive
Your thinking about code-execution must change from linear top to down
1.
2.
3.
...
to a high-frequent looping with timely conditional execution of parts of your code
Here is a tutorial that is intended to explain this concept
From your description it is still not clear to me how you switch on / off the peltier-element.
Can you please draw a timing-chart where the pattern how you want to switch on / off the peltier-element is shown.
If there are additional conditions describe these conditions too.