0

I am trying to code an OLED which consists of 2 buttons interfaced. The working is as follows:

  1. With the first button press it should switch on the OLED, show the opening screen.

  2. Then by pressing the 2nd button each time it should keep on changing its screen accordingly.

I have got to make it work for the first button. But the real problem is I am not sure how to make other screens switch with a single button is pressed each time.

1 Answers1

0

You first need to map the buttons to the variables.

on first button press the first variable should be set, and the second button can be mapped as a counter which counts the number of presses. based on the count you can change the screens.

ex:

bool var1;
int var2=0;
if(button1Pressed()==true){
    var1=true;
    switchOnOled();
}
if(var1==true&&button2pressed()==true){
    var2=var2%totalScreens;
    var2++;
}
switch(var2){
    case 1:
        screen1();
        break;
    case 2:
        ....
        ....

Hope that helps.

  • It answers to the most of my question. But one more thing on this, What if i have to run a certain a set of statement instead of var1. For example if i have to run a function?? I got about var2 on how to do this, but not clear with var1. – Nitin Nagaraj Dec 25 '15 at 07:59
  • What do you mean by `run a certain set of statement instead of var1` – Parag Agarwal Dec 25 '15 at 08:04
  • I mean to tell how to link or define var1 to certain statements. it is like SeeedGrayOled.clearDisplay(); SeeedGrayOled.drawBitmap(logo,96*96/8); Task_sleep(5000); I want to run these satements when button 1 is pressed. I got on how to run the var2 but not this. – Nitin Nagaraj Dec 25 '15 at 08:10
  • Got it thanks!! It was not described initially!! – Nitin Nagaraj Dec 25 '15 at 08:13
  • Write all those functions after `switchOnOled();` ie inside the 'if(button1Pressed()==true)` block. – Parag Agarwal Dec 25 '15 at 08:14
  • `var1` is unitialised if `button1Pressed() == false`. By nesting the second `if()...` in the first, `var1` can be eradicated entirely in any case. Explicitly testing a boolean function for true of false is unnecessary, i..e. `if( button1pressed() )` is simpler and more easily maintained. It would be good practice to use meaningful names for variables; perhaps `screen_on` and `screen_select` for example. – Clifford Dec 25 '15 at 08:47