如何在不更改屏幕的情况下调整光标位置?

时间:2018-11-12 23:28:33

标签: c++ input arduino

我有一个正在使用LCD屏幕,两个按钮和一个电位计的项目,它是一个二进制计算器。当我达到screenState == 3时,我试图更改数字,以便我可以跟踪它们并在跟踪溢出的同时添加一个8位数字和另一个8位数字。但是,每当我开始使用输入== 0的按钮时,它都会移动光标,然后我的下一个按钮再次在屏幕上循环。输入== 0应该只将lcd.cursor值更改为0到1之间的值。如何在不更改屏幕的情况下调整一个值?

#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);

const int numInputs = 2;
const int inputPins[numInputs] = {13, 12};

// tracks screen state
int screenState = 0;
int prevScreenState = 0;
int binaryValue = 0;

// tracks if we are on the right screen to acceptInput and if our value is 0 or 1
bool canAcceptInput = false;
bool valIsZero = false;

int inputState[numInputs];
int lastInputState[numInputs] = {LOW,LOW};
bool inputFlags[numInputs] = {LOW,LOW};
int inputCounter[numInputs];

int cursorPosX = 0;
int cursorPosY = 0;
int blinkingPosX = cursorPosX;
int blinkingPosY = cursorPosY;

unsigned long lastDebounceTime[numInputs] = {0,0};
long debounceDelay = 50;

void setup() {

  for(int i = 0; i < numInputs; i++) {
    pinMode(inputPins[i], INPUT);
    digitalWrite(inputPins[i], HIGH); // pull-up 20k
  }
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop() {
  setInputFlags();
  resolveInputFlags();
}

void setInputFlags() {
  for(int i = 0; i < numInputs; i++) {
    int reading = digitalRead(inputPins[i]);
    if (reading != lastInputState[i]) {
      lastDebounceTime[i] = millis();
    }
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading != inputState[i]) {
        inputState[i] = reading;
        if (inputState[i] == HIGH) {
          inputFlags[i] = HIGH;
        }
      }
    }
    lastInputState[i] = reading;
  }
}

void resolveInputFlags() {
  for(int i = 0; i < numInputs; i++) {
    if(inputFlags[i] == HIGH) {
      // Input Toggle Logic // all invoked upon bttn press
      inputCounter[i]++;
      updateScreenState(i); 
      printString(i);
      inputFlags[i] = LOW;
    }
  }
}
void updateScreenState(int input) {
 // input 0 = State 0 and 1
if(input == 0) {
   if(screenState > 2 & screenState < 19) {
     canAcceptInput = true;
     binaryValue = 1;
     updateScreenValues();
   }
   else if (screenState == 1) {
     //screenState = 0;
     binaryValue = 0;
     updateScreenValues();
   }
   else {
    //screenState = 2;
    binaryValue = 1;
   }
 // input 1 = State 2 to 6
 }else if(input == 1) { 
    // we have 20 max screen states, this cycles through them using button 2
    if(screenState == 0 || screenState == 1 || screenState > 19) {
      screenState = 2;
      updateScreen();
    }else{
      screenState++;
      updateScreen();
    }
  }
}

void setCursorValues( int cursorX, int cursorY) {
  lcd.setCursor(cursorX, cursorY);
  cursorPosX = cursorX;
  cursorPosY = cursorY;
}

void updateScreenValues() {

  if(canAcceptInput == true) {
      if(binaryValue == 0 ){
         lcd.print("0");
      }
      if(binaryValue == 1) {
         lcd.print("1");
         binaryValue = 0;
      }     
  }
}
// void update -----------------------------------------------------------------------------
void updateScreen() {

    // welcome screen -----------------------------    
    if(screenState == 2){
        canAcceptInput = false;
        lcd.clear();
        setCursorValues(0, 0);
        lcd.print("Welcome Screen");   
        setCursorValues(0, 1);
        lcd.print("Press Bttn 1");
    }

    // start of byte 1 screen ----------------------------------------------------------------
    if(screenState == 3){
        canAcceptInput = true;
        lcd.clear();
        setCursorValues(0, 0);          
        lcd.print("Byte 1 Screen");
        setCursorValues(0, 1);
        lcd.print("   0000  0000   ");
        //move cursor to MSD and blink
        setCursorValues(3, 1);
        lcd.blink();
    }
    //if screenState changes, so will the cursorValues-----------
    if(screenState == 4) {
      setCursorValues(4, 1);
    }
     if(screenState == 5) {
        setCursorValues(5, 1);
    }
    if(screenState == 6) {
        setCursorValues(6, 1);
    }
    if(screenState == 7) {
        setCursorValues(9, 1);
    }
    if(screenState == 8) {
        setCursorValues(10, 1);
    }
    if(screenState == 9) {
        setCursorValues(11, 1);
    }
    if(screenState == 10) {
        setCursorValues(12, 1);
    }   
    //start of byte 2 screen -------------------------------------------------------
    if(screenState == 11){
        lcd.clear();
        setCursorValues(0, 0);          
        lcd.print("Byte 2 Screen");
        setCursorValues(0, 1);
        lcd.print("   0000  0000   ");
        setCursorValues(3, 1);
        lcd.blink();
    }
        //if screenState changes, so will the cursorValues-----------
    if(screenState == 12) {
      setCursorValues(4, 1);
    }
     if(screenState == 13) {
        setCursorValues(5, 1);
    }
    if(screenState == 14) {
        setCursorValues(6, 1);
    }
    if(screenState == 15) {
        setCursorValues(9, 1);
    }
    if(screenState == 16) {
        setCursorValues(10, 1);
    }
    if(screenState == 17) {
        setCursorValues(11, 1);
    }
    if(screenState == 18) {
        setCursorValues(12, 1);
    }  
    if(screenState == 19){
        lcd.clear();
        setCursorValues(0, 0);          
        lcd.print("Solution Screen");
        lcd.noCursor();
    }
    if(screenState == 20){
        lcd.clear();
        setCursorValues(0, 0);          
        lcd.print("Contrast Screen");
    }
}
void printString(int output) {
      Serial.print("Input ");
      Serial.print(output);
      Serial.print(" was pressed ");
      Serial.print(inputCounter[output]);
      Serial.println(" times.");
      Serial.print("screenState = ");
      Serial.println(screenState);
      Serial.print("binaryValue = ");
      Serial.println(binaryValue);
      Serial.print("cursorPosX = ");
      Serial.print(cursorPosX);
      Serial.print(" cursorPosY = ");
      Serial.println(cursorPosY);
      Serial.print('\n');
}

0 个答案:

没有答案