Flickering OLED

For some reason Hz, previousHz and currentHz are the same ?

#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>

#define TFT_MOSI 23  // Data out
#define TFT_SCLK 18  // Clock out
#define TFT_CS   22// 
#define TFT_DC    21 //
#define TFT_RST   -1  // pin# 2

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

int Hz;
int currentHz;
int previousHz = 0;

void setup()
{
  Serial.begin(115200);

  tft.init(170, 320);  // high, with
  tft.setRotation(3); // Adjust rotation as needed (0-3),0 = viertical, 1= horyzontal upside down, 2= viertical 180 deg , 3=horyzontal normal,
  tft.fillScreen(ST77XX_BLUE);
  tft.setTextWrap(false); // Disable text wrapping
  tft.setTextSize(4);     // Set text size

  pinMode(12, INPUT_PULLUP);  //amp ++
  pinMode(14, INPUT_PULLUP);  // amp --
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  // int Hz;
  if (digitalRead(12) == LOW)
  {
    Hz++;
  }
  if (digitalRead(14) == LOW)
  {
    Hz--;
  }
  currentHz = Hz;
  previousHz = currentHz;
  //if ( previousHz != Hz) //  re-writing the old data in background color
  if (currentHz != previousHz)

    //if ( previousHz != currentHz )
  {
    tft.setCursor(0, 0);
    tft.setTextColor(ST77XX_BLUE); // background color
    tft.print( previousHz);
    // tft.print( currentHz);
    tft.setTextColor(ST77XX_YELLOW);
    tft.print("Hz = ");
    tft.print( Hz);
    //tft.println( currentHz);
  }
  // previousHz = currentHz;
  /*
    ///////////////////////
    tft.setTextColor(ST77XX_YELLOW);
    if (currentHz != previousHz)
    {
      tft.setCursor(30, 0);
      tft.fillRect(29, 0, 30, 10, ST77XX_BLUE); //depends on the hight of the font and how long the static text is
      tft.setTextColor(ST77XX_YELLOW);
      tft.print(Hz);
    }
    ///////////////////
  */
  Serial.print("  prev =  "); Serial.print(previousHz);
  Serial.print("  cur =  "); Serial.print(currentHz);
  Serial.print("  Hz =  "); Serial.print(Hz);
  Serial.println();
  delay(100);
}
 =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  0  cur =  0  Hz =  0
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -1  cur =  -1  Hz =  -1
  prev =  -2  cur =  -2  Hz =  -2
  prev =  -2  cur =  -2  Hz =  -2
  prev =  -2  cur =  -2  Hz =  -2
  prev =  -2  cur =  -2  Hz =  -2
  prev =  -3  cur =  -3  Hz =  -3
  prev =  -3  cur =  -3  Hz =  -3
  prev =  -3  cur =  -3  Hz =  -3

done

#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>

#define TFT_MOSI 23  // Data out
#define TFT_SCLK 18  // Clock out
#define TFT_CS   22// 
#define TFT_DC    21 //
#define TFT_RST   -1  // pin# 2

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

int Hz;
int currentHz;
int previousHz ;

void setup()
{
  Serial.begin(115200);

  tft.init(170, 320);  // high, with
  tft.setRotation(3); // Adjust rotation as needed (0-3),0 = viertical, 1= horyzontal upside down, 2= viertical 180 deg , 3=horyzontal normal,
  tft.fillScreen(ST77XX_BLUE);
  tft.setTextWrap(false); // Disable text wrapping
  tft.setTextSize(4);     // Set text size

  pinMode(12, INPUT_PULLUP);  //amp ++
  pinMode(14, INPUT_PULLUP);  // amp --
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  
  if (digitalRead(12) == LOW)
  {
    Hz++;
  }
  if (digitalRead(14) == LOW)
  {
    Hz--;
  }
  currentHz = Hz;
 

    if ( previousHz != currentHz )
  {
    tft.setCursor(0, 0);
    tft.setTextColor(ST77XX_BLUE); // background color
    tft.print("Hz = ");
    tft.print( previousHz );
    tft.setCursor(0, 0);
    tft.setTextColor(ST77XX_YELLOW);
    tft.print("Hz = ");
    tft.print( currentHz);
  
    Serial.print("  prev =  "); Serial.print(previousHz);
    Serial.print("  cur =  "); Serial.print(currentHz);
    Serial.print("  Hz =  "); Serial.print(Hz);
    Serial.println();
    previousHz = currentHz;

  }
  delay(300);
}

Thanks everyone for suggestions.

  currentHz = Hz;
  previousHz = currentHz;

I wonder why that could be ?

modification of this program

https://www.google.com/search?q=arduino+current+previous&sca_esv

int previousSensorValue = 0;
int currentSensorValue;

void setup() {
  Serial.begin(9600);
}

void loop() {
  previousSensorValue = currentSensorValue; // Store the last reading
  currentSensorValue = analogRead(A0);    // Get the new reading from analog pin A0

  if (currentSensorValue != previousSensorValue) {
    Serial.print("Sensor value changed from ");
    Serial.print(previousSensorValue);
    Serial.print(" to ");
    Serial.println(currentSensorValue);
  }

  delay(100); // Small delay to prevent excessive readings
}

improper declarations in the past was causing the problems

Along with you setting all 3 values equal, of course

now they don't

A couple of further comments on the code in post #43

There is no need to use 3 variable for the frequency. Current and previous values are enough

You are printing and erasing text that will never change. This is not necessary, inefficient and should not be done

on display I am using 2 variables,for serial monitor 3 to see what is going on

You do not need 3 variables to see what is going on, current and previous would be good enough

Did you understand what I said about erasing text that will never change ?

When I'm done I'll remove the serial monitor

You don't need 3 variables in order to see what is going on in the Serial monitor because currentHz will always equal Hz in your sketch

but thanks to serial monitor I discover where was problem

How did it help you and would the problem have happened if you had only used 2 variables ?

post #42

OK, as long as you are happy and the sketch does what you want

Now, about erasing and printing the fixed text when you don't need to ...

Are you going to fix that ?

#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>

#define TFT_MOSI 23  // Data out
#define TFT_SCLK 18  // Clock out
#define TFT_CS   22// 
#define TFT_DC    21 //
#define TFT_RST   -1  // pin# 2

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

int Hz;
int currentHz;
int previousHz ;

void setup()
{
  tft.init(170, 320);  // high, with
  tft.setRotation(3); // Adjust rotation as needed (0-3),0 = viertical, 1= horyzontal upside down, 2= viertical 180 deg , 3=horyzontal normal,
  tft.fillScreen(ST77XX_BLUE);
  tft.setTextWrap(false); // Disable text wrapping
  tft.setTextSize(4);     // Set text size

  pinMode(12, INPUT_PULLUP);  //amp ++
  pinMode(14, INPUT_PULLUP);  // amp --
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  
  if (digitalRead(12) == LOW)
  {
    Hz++;
  }
  if (digitalRead(14) == LOW)
  {
    Hz--;
  }
  currentHz = Hz;
 

    if ( previousHz != currentHz )
  {
    tft.setCursor(0, 0);
    tft.setTextColor(ST77XX_BLUE); // background color
    tft.print("Hz = ");
    tft.print( previousHz );
    tft.setCursor(0, 0);
    tft.setTextColor(ST77XX_YELLOW);
    tft.print("Hz = ");
    tft.print( currentHz);
  
    previousHz = currentHz;

  }
  delay(300);
}

Some suggested changes to your sketch

#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>

#define TFT_MOSI 23  // Data out
#define TFT_SCLK 18  // Clock out
#define TFT_CS 22    //
#define TFT_DC 21    //
#define TFT_RST -1   // pin# 2

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

int newHz;
int previousHz;
const byte xPosHz = 40; 	  // use variables for screen positions.  
const byte yPosHz = 0;			//Adjust these to suit your screen layout

const byte incButtonPin = 12;  //give inputs sensible names
const byte decButtonPin = 14;

void setup()
{
    tft.init(170, 320);  // high, with
    tft.setRotation(3);  // Adjust rotation as needed (0-3),0 = viertical, 1= horyzontal upside down, 2= viertical 180 deg , 3=horyzontal normal,
    tft.fillScreen(ST77XX_BLUE);
    tft.setTextWrap(false);  // Disable text wrapping
    tft.setTextSize(4);      // Set text size
    tft.setCursor(0, yPosHz);
    tft.setTextColor(ST77XX_YELLOW);  //print the text once
    tft.print("Hz = ");

    pinMode(incButtonPin, INPUT_PULLUP);  //amp ++
    pinMode(decButtonPin, INPUT_PULLUP);  // amp --
}

void loop()
{
    if (digitalRead(incButtonPin) == LOW)
    {
        newHz++;
    }
    if (digitalRead(decButtonPin) == LOW)
    {
        newHz--;
    }

    if (previousHz != newHz)
    {
        tft.setCursor(xPosHz, yPosHz);
        tft.setTextColor(ST77XX_BLUE);  // background color
        tft.print(previousHz);
        tft.setCursor(xPosHz, yPosHz);
        tft.setTextColor(ST77XX_YELLOW);
        tft.print(newHz);

        previousHz = newHz;
    }
    delay(300);
}

I cannot test this as I don't have the hardware but it compiles. You need to adjust the value of the xPosHz variable to suit your screen depending on how many pixels the fixed text uses

Using variable for the x, y coordinates mans that you do not have to change the values in multiple places in the sketch