Push button cursor position

Hi guys,
I am trying to modify a “menu” creation code.
Original is here.

From the tutorial, I can increase SetTemp in page2 by a value of 1 by pressing two separate buttons.

But I would like to use a 3rd button to position the cursor and individually increment or decrement at a particular position.
For example:
12345
I reach cursor position at 1 and should be able to set 0 to 1 and . (decimal). Same for rest other digits positions.

Kindly suggest a way.

Thanks.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Adafruit_PCD8544.h>

#define OLED_RESET 4

Adafruit_SH1106 display(OLED_RESET);


boolean backlight = true;
int setTemp=0;

int menuitem = 1;
int page = 1;
int pos = 0;
int cpos1 = 5;
int cpos2 = 25;
String output = "0123456789.";
//int myArray[] = {' ','0', '1', '2', '3', '4','5','6','7','8','9','.',' '};
volatile boolean up = false;
volatile boolean down = false;
volatile boolean middle = false;

int downButtonState = 0;
int upButtonState = 0;  
int selectButtonState = 0;          
int lastDownButtonState = 0;
int lastSelectButtonState = 0;
int lastUpButtonState = 0;
int buttonCursor = 0;

void setup()   {                
  Serial.begin(9600);
  
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  digitalWrite(6, HIGH); 
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  
  //display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display(); 
  delay(3000);
  }


void loop() {
  display.display();
  delay(100);
  display.clearDisplay();
drawMenu();

  downButtonState = digitalRead(6);
  selectButtonState = digitalRead(7);
  upButtonState =   digitalRead(8);
  buttonCursor =   digitalRead(9);
 
  checkIfDownButtonIsPressed();
  checkIfUpButtonIsPressed();
  checkIfSelectButtonIsPressed();

  if (up && page == 1 ) {
    up = false;
    menuitem--;
    if (menuitem==0)
    {
      menuitem=3;
    }      
  }
  
  if (page == 2 ) {
    
    if (digitalRead(buttonCursor) == HIGH){
    pos ++;
    cpos1 ++;
    if (digitalRead(upButtonState) == LOW)
  {

   output [pos] = (output [pos] ++);
   display.clearDisplay();
   display.print(output);
   delay (400);
   }
    display.setCursor (cpos1, cpos2);
  }
}
  /*else if (up && page == 2 ) {
    up = false;
    display.cursor();
    setTemp--;
    //setContrast();
  }*/


  if (down && page == 1) {
    down = false;
    menuitem++;
    if (menuitem==4) 
    {
      menuitem=1;
    }      
  }
  
 /* else if (down && page == 2 ) {
    down = false;
    setTemp++;
    //setContrast();
    //display.setCursor(5, 25);
  }*/
 


  if (middle) {
    middle = false;
    
    if (page == 1 && menuitem==2) 
    {
      if (backlight) 
      {
        backlight = false;
        //turnBacklightOff();
        }
      else 
      {
        backlight = true; 
        //turnBacklightOn();
       }
    }

    if(page == 1 && menuitem ==3)
    {
      //resetDefaults();
    }


else if (page == 1 && menuitem==1) {
      page=2;
     }
else if (page == 2) {
      page=1;
     }
   }
   
  }

  void checkIfDownButtonIsPressed()
  {
    if (downButtonState != lastDownButtonState) 
  {
    if (downButtonState == 0) 
    {
      down=true;
    }
    delay(10);
  }
   lastDownButtonState = downButtonState;
  }

void checkIfUpButtonIsPressed()
{
  if (upButtonState != lastUpButtonState) 
  {
    if (upButtonState == 0) {
      up=true;
    }
    delay(10);
  }
   lastUpButtonState = upButtonState;
}

void checkIfSelectButtonIsPressed()
{
   if (selectButtonState != lastSelectButtonState) 
  {
    if (selectButtonState == 0) {
      middle=true;
    }
    delay(10);
  }
   lastSelectButtonState = selectButtonState;
}

  
  void drawMenu()
  {
    
  if (page==1) 
  {    
    //display.display();
    display.setTextSize(1);
    display.clearDisplay();
    display.setTextColor(BLACK, WHITE);
    display.setCursor(15, 0);
    display.print("ITP Labs");
    display.drawFastHLine(0,10,83,BLACK);
    display.setCursor(0, 15);
   
    if (menuitem==1) 
    { 
      display.setTextColor(WHITE, BLACK);
    }
    else 
    {
      display.setTextColor(BLACK, WHITE);
    }
    //display.display();
    display.print(">Set Temp");
    display.setCursor(0, 25);
   
    if (menuitem==2) 
    {
      display.setTextColor(WHITE, BLACK);
    }
    else 
    {
      display.setTextColor(BLACK, WHITE);
    }  
    //display.display();
    display.print(">Light: ");
    
    if (backlight) 
    {
      //display.display();
      display.print("ON");
    }
    else 
    {
      //display.display();
      display.print("OFF");
    }
    //display.display();
    
    if (menuitem==3) 
    { 
      display.setTextColor(WHITE, BLACK);
    }
    else 
    {
      display.setTextColor(BLACK, WHITE);
    }
    //display.display();
    display.setCursor(0, 35);
    display.print(">Reset");
    display.display();
  }
    
 
  else if (page==2) 
  {
//display.display();
    display.setTextSize(1);
    display.clearDisplay();
    display.setTextColor(BLACK, WHITE);
    display.setCursor(15, 0);
    display.print("setTemp");
    display.drawFastHLine(0,10,83,BLACK);
    display.setCursor(5, 15);
    display.print("Value");
    display.setTextSize(2);
    display.setCursor(5, 25);
    //display.print(setTemp);
 
    display.setTextSize(2);
    display.display();
  }
  
  }
/*
  void resetDefaults()
  {
    setTemp = 50;
    setContrast();
    backlight = true;
    turnBacklightOn();
  }

  /*void setContrast()
  {
    display.setContrast(setTemp);
    display.display();
  }*/

 /* void turnBacklightOn()
  {
    digitalWrite(7,LOW);
  }

    void turnBacklightOff()
  {
    digitalWrite(7,HIGH);*/
 // }*/

This the part of the code which probably needs correction:

if (page == 2 ) {
    
    if (digitalRead(buttonCursor) == HIGH){
    pos ++;
    cpos1 ++;
    if (digitalRead(upButtonState) == LOW)
  {

   output [pos] = (output [pos] ++);
   display.clearDisplay();
   display.print(output);
   delay (400);
   }
    display.setCursor (cpos1, cpos2);
  }
}

I tried many different solutions but can’t seem to make it work.

 if (page == 2 ) {
    while (true) {
    display.setCursor(cpos1, cpos2);
    display.print(characters[currentCharacter]);
    //display.setCursor(cpos1, cpos2);

    if (digitalRead(cursor) == HIGH){
      cursor = false;
    answer[cpos1] = characters[currentCharacter];
    cpos1++;
    currentCharacter = 0;
    if (cpos1 >= answerLength) {
        break;
      }
   
    if (up && page == 2 ) {
    up = false;
    currentCharacter--;
    display.print(currentCharacter);
  }
   if (down && page == 2 ) {
    down = false;
    currentCharacter++;
    display.print(currentCharacter);
  }
    //display.setCursor(cpos1, cpos2);
    if (currentCharacter < 0) {
      currentCharacter = currentCharacter + charactersCount;
    } else if (currentCharacter >= charactersCount) {
      currentCharacter = currentCharacter % charactersCount;
    }
  }
    }
}

I will be thankful if anyone looks into it.

Thanks,

Still no success. >:(

Do you guys have any example of button counter at individual digits? For example if I need to get 2135 number from buttons, I first go to first location by pressing a 'select' button and then pressing '+' button or '-' button to set digit 2. Pressing the 'select' button again to move cursor to second location and taking use of '+' and '-' buttons.. same process for rest two locations. At the end, 'Enter' button to save number 2135 in a variable.

Once I have your help, I will be able to adjust it in my main code.

Thanks again.

Z

Hi guys, For cycling digits, I use

int number[10] = {0,1,2,3,4,5,6,7,8,9};

But in order to have . (decimal) in the input, I need to define it as

int number[11] = {0,1,2,3,4,5,6,7,8,9,.};

But this is an error. If I define as

int number[11] = {0,1,2,3,4,5,6,7,8,9,'.'};

I see 46 on my OLED.

How can I define . (decimal, not float) properly in an array?

Thanks

How can I define . (decimal, not float) properly in an array?

You ARE defining it correctly. At least as correctly as you can when you want to store a non-integer value in an integer array. Therefore, there must be some other problem.

It doesn't make sense to me to go from '8' to '9' to '.'. But, if that IS what you want, why isn't the array of possible values of type char, and why doesn't it contain '0', '1', '2', ... '8', '9', '.'?

PaulS:
You ARE defining it correctly. At least as correctly as you can when you want to store a non-integer value in an integer array. Therefore, there must be some other problem.

It doesn’t make sense to me to go from ‘8’ to ‘9’ to ‘.’. But, if that IS what you want, why isn’t the array of possible values of type char, and why doesn’t it contain ‘0’, ‘1’, ‘2’, … ‘8’, ‘9’, ‘.’?

Because I would need to convert char to int again as the input is going to be used in a calculation.
Should this work?

int number[11] = {0,1,2,3,4,5,6,7,8,9,'/46'};

I can’t check at the moment…

Thanks.

Because I would need to convert char to int again as the input is going to be used in a calculation.

You are going to have to deal with the elephant in the room at some point, anyway. Now seems like a good time.

Should this work?

No. The escape character is , not /. Storing \46, 46, or ‘.’ in the array will have the same result.

PaulS: You are going to have to deal with the elephant in the room at some point, anyway. Now seems like a good time. No. The escape character is \, not /. Storing \46, 46, or '.' in the array will have the same result.

Yeah, you are right. I am dealing with it now.. And decimal is now on my OLED!

I wanted to ask if you know how I can have blinking cursor on OLED? I am using SH1106 (derivative of SSD1306) library.

Thanks.

hey zeus did you find the way to move cursor of SSD1306 using push button?