sterretje:
As val0 is always positive and hysteresis is positive, the if condition will always evaluate true.
What you probably want is to remember the previous value and compare the old value with the new value taking hysteresis into account.
BOTH conditions evaluate to TRUE! Once you make the change @sterretje suggested, I think you also need to swap the "less than" and "greater than" comparisons.
bulldoglowell i did longer delay already and it makes reading on the screen consistent, but then the screen becomes laggy when you want to update the screen with more frequent data
Also i`m not sure if remembering the old value is correct in this
#include "U8glib.h"
int anPin0 = A0;
int val0 = 0;
int oldvalue = 0;
byte hysteresis = 10;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawCircle(115, 3, 1.5);
u8g.setFont(u8g_font_profont22);
u8g.setPrintPos(0, 15);
u8g.print("TEMP: C");
u8g.setPrintPos(60, 15);
u8g.print(val0);
}
void setup(void) {
// start serial port
Serial.begin(9600);
// flip screen, if required
// u8g.setRot180();
}
void loop(void)
{
val0 = analogRead(anPin0);
Serial.println(val0);
oldvalue = val0;
val0 = map(val0, 0, 1023, 0, 60);
if (oldvalue < (val0 - hysteresis))
{
updateOled();
}
else if (oldvalue > (val0 + hysteresis))
{
updateOled();
}
delay(50);
}
void updateOled(void)
{ // picture loop
u8g.firstPage();
do {
draw();
} while ( u8g.nextPage() );
}
mOskit:
bulldoglowell i did longer delay already and it makes reading on the screen consistent, but then the screen becomes laggy when you want to update the screen with more frequent data
because you used delay()
look at the alternate code I gave you which should eliminate your laggy-ness.
Thants what im saying. It does exactly the same thing either with delay or millis and it`s still not solving the hysteresis issue. Working hysteresis should eliminate any need for delay i guess
my point was that the 50ms refresh rate is silly. If you get that normal, and then you can see if your hysteresis is working, which in its present state, well... isn't
Also to test hysteresis simply setting the value say to "10" should impact on the output as the display should not update if the value is - or + say 10 or more then the val0 . Correct me if i`m wrong.
mOskit:
Also to test hysteresis simply setting the value say to "10" should impact on the output as the display should not update if the value is - or + say 10 or more then the val0 . Correct me if i`m wrong.
something like this:
void loop(void)
{
static int oldvalue = 0;
int val0 = analogRead(anPin0);
Serial.println(val0);
val0 = map(val0, 0, 1023, 0, 60);
if (val0 != oldvalue)
{
if (val0 > oldvalue + hysteresis or val0 < oldvalue - hysteresis)
{
oldValue = val0;
updateOled();
}
}
}