solved:how to freese a screen with graphic display and u8glib

I’m using a st7920 display with U8glib

I want to do this

in my main loop I check a button state
if this button is pressed I go to a second screen and put another text on the screen
so far so good
but I want that this second screen stays on till I push that button again

I don’t know how to do that

I tried different things but it don’t work

is there somewhere a trick to do that ?

I attached the main code where I need to push D4 for to go to the second screen

thanks Jean-Marie

loop.txt (850 Bytes)

Please post all your code. Please use [ code ] tags instead of attaching it.

What else is this program doing? Does it keep doing that while it's showing this screen or do you want it to stop doing that?

hi

I want to build an antenna analyser based on the design from K6BEZ and AD7C
in the main loop (void draw() )I control 3 buttons and a rotary encoder
the encoder changes the value freq
this value will be send to a dds module
this part off the software is not included yet
button 1 changes the step (1MHz, 1kHz, 5kHz,…) this works
button 2 changes the value freq to predefined values (= the allowed frequencies that I may use) this works
button 3 should open the second screen where I want to draw a curve based on the value on an analogue input
this screen must be frozen until I push button 3 again

I my sketch the second screen opens, (dummy)text is printed ,wait 2 seconds and then returns to the main screen
this screen must be “frozen” until I push again button3

jean-marie

#include <U8glib.h>
#include <rotary.h>
U8GLIB_ST7920_128X64_1X u8g(11, 12, 13);

long freq = 7100000;
long stap = 100000;
int buttonstate = 0;
String hertz = "1MHz";
int_fast32_t increment = 1000000; // starting VFO update increment in HZ.
#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
Rotary r = Rotary(2, 3); // sets the pins the rotary encoder uses.  Must be interrupt pins.
int_fast32_t freq2 = 1; // variable to hold the updated frequency

void u8g_prepare(void) {
  u8g.setFont(u8g_font_helvB10);
  int xpos = 0;
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
}

void draw(void) {

  char buf[12];
  u8g_prepare();
  buttonstate = digitalRead(4);
  if (buttonstate == LOW) {
    graph()
    ;
  };

  buttonstate = digitalRead(6);
  if (buttonstate == LOW) {
    setincrement();
  };
  buttonstate = digitalRead(5);
  if (buttonstate == LOW) {
    setband();
  };

  u8g.setPrintPos(0, 5);
  u8g.print("Freq :");
  u8g.setPrintPos(50, 5);

  u8g.print(freq / 1000);
  u8g.setPrintPos(95, 5);
  u8g.print("kHz");

  u8g.setPrintPos(0, 25);
  u8g.print("Stap :");
  u8g.setPrintPos(50, 25);

  u8g.print(hertz);

}

void setup(void) {
  u8g.setFont(u8g_font_helvB10);
  u8g.setColorIndex(1); // pixel on, black
  pinMode(6, INPUT); // Connect to a button that goes to GND on push
  digitalWrite(6, HIGH);
  pinMode(5, INPUT); // Connect to a button that goes to GND on push
  digitalWrite(5, HIGH);
  pinMode(4, INPUT); // Connect to a button that goes to GND on push
  digitalWrite(4, HIGH);
  PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();
  u8g_prepare();
  call();
}
void loop(void) {
  // collect the data

  u8g.firstPage();
  do {
    draw();

  }
  while ( u8g.nextPage() );
  // rebuild the picture after some delay
  delay(50);

}
void setincrement() {

  if (increment == 1000000) {
    increment = 1000;
    hertz = "1 kHz";
  }
  else if (increment == 1000) {
    increment = 5000;
    hertz = "5 kHz";
  }
  else if (increment == 5000) {
    increment = 10000;
    hertz = "10 kHz";
  }
  else if (increment == 10000) {
    increment = 100000;
    hertz = "100kHz";
  }
  else if (increment == 100000) {
    increment = 1000000;
    hertz = "1 MHz";
  }
  else {
    increment = 1000;
    hertz = "1 MHz";
  };

  delay(350);
};

void setband() {


  if (freq == 1900000) {
    freq = 3650000;
  }
  else if (freq == 3650000) {
    freq = 7100000;
  }
  else if (freq == 7100000) {
    freq = 10100000;
  }
  else if (freq == 10100000) {
    freq = 14200000;
  }
  else if (freq == 14200000) {
    freq = 18120000;
  }
  else if (freq == 18120000) {
    freq = 21225000;
  }
  else if (freq == 21225000) {
    freq = 24940000;
  }
  else if (freq == 24940000) {
    freq = 28500000;
  }
  else {
    freq = 1900000;
  };
  delay(250);
  increment = 1000; hertz = "1kHz";

  ;

};

ISR(PCINT2_vect) {
  unsigned char result = r.process();
  if (result) {
    if (result == DIR_CW) {
      freq = freq + increment;
    }
    else {
      freq = freq - increment;
    };
    if (freq > 30000001) {
      freq = 1000000;
    }; // UPPER VFO LIMIT
    if (freq < 1000000) {
      freq = 30000000;
    }; // LOWER VFO LIMIT
  }
}
void call() {
  u8g.firstPage();
  do {
    u8g.drawStr(30, 5, "ANTENNA");
    u8g.drawStr( 30, 25, "ANALYSER");
    u8g.drawStr(30, 45, "O N 7 E N");

  }
  while ( u8g.nextPage() );
  


  delay(3000);
 

  };

void graph() {
  u8g.firstPage();
  do {
    u8g.drawStr( 0, 5, "hier");
    u8g.drawStr( 0, 25, "komt");
    u8g.drawStr(0, 45, "O N 7 E N");
  }
  while ( 
u8g.nextPage() );
  
  
delay(2000);

  

  };

void toets() {
  u8g.firstPage();
  do {buttonstate = digitalRead(4);
  if (buttonstate == HIGH) {
    return;}
  draw();
  }
  while ( u8g.nextPage() );
};
int_fast32_t increment = 1000000; // starting VFO update increment in HZ.

What the hell is this? The _t suffix means that the type is a standard type. int_fast32_t is NOT a standard type.

  if (buttonstate == LOW) {
    graph()
    ;
  };

Why is there a carriage return/line feed between the statement and the ;?

draw() is a piss-poor name for the function. It does way more than the name implies. Fix the name or get rid of the code that does anything not related to drawing.

When you ask questions about switches, it is far better to use the names in the code in your question. 4, 5, and 6 are stupid names.

i've used pieces code from examples that i found on the net the _t suffix comes from the Original code to drive the dds module. so i suppose it is needed.

the carriage return is a mistake

this is a "workingcode" not intented to publish yet you're right i should choose better names but i'm a very new newbie i need to learn a lot

once i have a sollution for my problem i will clean up the code

once i have a sollution for my problem i will clean up the code

Once you clean up your code, you will see the solution.

i found a solution with the switch commando

greetings

#include <U8glib.h>
#include <rotary.h>
U8GLIB_ST7920_128X64_1X u8g(11, 12, 13);

long freq = 7100000;
long freqstep = 100000;
int buttonstep = 0;
int buttongraph = 0;
int buttonband = 0;
String hertz = "1MHz";
int_fast32_t increment = 1000000; // starting VFO update increment in HZ.
#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
Rotary r = Rotary(2, 3); // sets the pins the rotary encoder uses.  Must be interrupt pins.
int_fast32_t freq2 = 1; // variable to hold the updated frequency
int screen = 0;
void u8g_prepare(void) {
  u8g.setFont(u8g_font_helvB10);
  int xpos = 0;
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
}

void mainloop(void) {

  char buf[12];
  u8g_prepare();
  buttonstep = digitalRead(4);
  if (buttonstep == LOW) {
    setincrement();

  };


  buttonband = digitalRead(5);
  if (buttonband == LOW) {
    setband();
  };
 

  u8g.setPrintPos(0, 5);
  u8g.print("Freq :");
  u8g.setPrintPos(50, 5);

  u8g.print(freq / 1000);
  u8g.setPrintPos(95, 5);
  u8g.print("kHz");

  u8g.setPrintPos(0, 25);
  u8g.print("Step :");
  u8g.setPrintPos(50, 25);

  u8g.print(hertz);

}

void setup(void) {
  u8g.setFont(u8g_font_helvB10);
  u8g.setColorIndex(1); // pixel on, black
  pinMode(6, INPUT); // Connect to a button that goes to GND on push
  digitalWrite(6, HIGH);
  pinMode(5, INPUT); // Connect to a button that goes to GND on push
  digitalWrite(5, HIGH);
  pinMode(4, INPUT); // Connect to a button that goes to GND on push
  digitalWrite(4, HIGH);
  PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();
  u8g_prepare();
  showcall();
}

void loop(void) {
  u8g.firstPage();
  do {
    draw();
  }  while ( u8g.nextPage() );

  delay(100);
   buttongraph = digitalRead(6);
      if (buttongraph == LOW) {
        screen = 1;
      };

}
void setincrement() {

  if (increment == 1000000) {
    increment = 1000;
    hertz = "1 kHz";
  }
  else if (increment == 1000) {
    increment = 5000;
    hertz = "5 kHz";
  }
  else if (increment == 5000) {
    increment = 10000;
    hertz = "10 kHz";
  }
  else if (increment == 10000) {
    increment = 100000;
    hertz = "100kHz";
  }
  else if (increment == 100000) {
    increment = 1000000;
    hertz = "1 MHz";
  }
  else {
    increment = 1000;
    hertz = "1 MHz";
  };

  delay(350);
};

void setband() {


  if (freq == 1900000) {
    freq = 3650000;
  }
  else if (freq == 3650000) {
    freq = 7100000;
  }
  else if (freq == 7100000) {
    freq = 10100000;
  }
  else if (freq == 10100000) {
    freq = 14200000;
  }
  else if (freq == 14200000) {
    freq = 18120000;
  }
  else if (freq == 18120000) {
    freq = 21225000;
  }
  else if (freq == 21225000) {
    freq = 24940000;
  }
  else if (freq == 24940000) {
    freq = 28500000;
  }
  else {
    freq = 1900000;
  };
  delay(250);
  increment = 1000; hertz = "1kHz";

  ;

};

ISR(PCINT2_vect) {
  unsigned char result = r.process();
  if (result) {
    if (result == DIR_CW) {
      freq = freq + increment;
    }
    else {
      freq = freq - increment;
    };
    if (freq > 30000001) {
      freq = 1000000;
    }; // UPPER VFO LIMIT
    if (freq < 1000000) {
      freq = 30000000;
    }; // LOWER VFO LIMIT
  }
}
void showcall() {
  u8g.firstPage();
  do {
    u8g.drawStr(30, 5, "ANTENNA");
    u8g.drawStr( 30, 25, "ANALYSER");
    u8g.drawStr(30, 45, "O N 7 E N");

  }
  while ( u8g.nextPage() );



  delay(2000);


};

void graph() {
  screen = 1;
  u8g.firstPage();
  do {
    u8g.setFont(u8g_font_6x10);
    u8g.drawVLine(26, 0, 45);
    u8g.drawHLine(26, 45, 125);
    u8g.drawPixel(25, 35);
    u8g.drawPixel(25, 25);
    u8g.drawPixel(25, 15);
    u8g.drawPixel(25, 5);
    u8g.drawPixel(24, 35);
    u8g.drawPixel(24, 25);
    u8g.drawPixel(24, 15);
    u8g.drawPixel(24, 5);
    u8g.drawPixel(76, 46);
    u8g.drawPixel(76, 47);
    u8g.drawPixel(76, 35);
    u8g.drawPixel(76, 25);
    u8g.drawPixel(76, 15);
    u8g.drawPixel(76, 5);
    u8g.drawPixel(66, 35);
    u8g.drawPixel(66, 25);
    u8g.drawPixel(66, 15);
    u8g.drawPixel(66, 5);
    u8g.drawPixel(56, 35);
    u8g.drawPixel(56, 25);
    u8g.drawPixel(56, 15);
    u8g.drawPixel(56, 5);
    u8g.drawPixel(46, 35);
    u8g.drawPixel(46, 25);
    u8g.drawPixel(46, 15);
    u8g.drawPixel(46, 5);
    u8g.drawPixel(36, 35);
    u8g.drawPixel(36, 25);
    u8g.drawPixel(36, 15);
    u8g.drawPixel(36, 5);
    u8g.drawPixel(86, 35);
    u8g.drawPixel(86, 25);
    u8g.drawPixel(86, 15);
    u8g.drawPixel(86, 5);
    u8g.drawPixel(96, 35);
    u8g.drawPixel(96, 25);
    u8g.drawPixel(96, 15);
    u8g.drawPixel(96, 5);
    u8g.drawPixel(106, 35);
    u8g.drawPixel(106, 25);
    u8g.drawPixel(106, 15);
    u8g.drawPixel(106, 5);
    u8g.drawPixel(116, 35);
    u8g.drawPixel(116, 25);
    u8g.drawPixel(116, 15);
    u8g.drawPixel(116, 5);
    u8g.drawPixel(36, 35);
    u8g.drawPixel(36, 25);
    u8g.drawPixel(36, 15);
    u8g.drawPixel(36, 5);


    u8g.setPrintPos(60, 60);
    u8g.print(freq / 1000);
    u8g.setPrintPos(92, 60);
    u8g.print("kHz");
    u8g.setPrintPos(15, 8);
    u8g.print("4");
    u8g.setPrintPos(15, 18);
    u8g.print("3");
    u8g.setPrintPos(15, 28);
    u8g.print("2");
    u8g.setPrintPos(15, 38);
    u8g.print("1");


  }
  while (
    u8g.nextPage() );


  buttongraph = digitalRead(6);
  if (buttongraph == LOW) {
    screen = 0;
  };
  delay(500);

};

void draw() {
  u8g_prepare();
  switch (screen) {
    case 0: mainloop(); break;
    case 1: graph(); break;
     }}