我的 lowConc 和 highConc 变量在哪里将其值更改为零? Bug、Arduino、输入、输出、计算

问题描述 投票:0回答:1

我编写了一个程序来计算两个值之间的步长,以输出总共 6 个值来校准溶液。在我的代码中的某处是一个将变量的值重置为零的错误,因为 6 个值的输出总是变为 0.

//SDA to Pin A4
//SCL to Pin A5


#include <Keypad.h>
#include <LCD_I2C.h>


//_Declaration_
//Display
LCD_I2C lcd(0x27, 16, 2);
//Keypad
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6' ,'B'},
  {'7', '8', '9' ,'C'},
  {'*', '0', '#', '.'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6};   //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

//*Variables
//Keypad
String inputString; //Working memory
long inputInt; //long term memory

byte whileloop = 0; //to keep while loops running

//User input number pad
float lowConc; // Memory of the lowest Concentration
float hiConc; // Memory f the highest Concentration

// Calculated distances between lowConc and highConc to make 6 Meassurements
float Abs =(hiConc-lowConc)/5;

//Concentration of the Calibrationsolutions
float Conc1 = lowConc;
float Conc2 = lowConc+Abs;
float Conc3 = Conc2+Abs;
float Conc4 = Conc3+Abs;
float Conc5 = Conc4+Abs;
float Conc6 = Conc5+Abs;

//Textbloecke
String low = "lowest";
String hi = "highest";



void setup() {
  lcd.begin();
  lcd.backlight();
  inputString.reserve(10); // maximum number of digit for a number is 10, change if needed

}
//Programming blocks

void dcd (int a, int b) //Code block to delay, clear display and delay again
  {   
  delay(a);
  lcd.clear();
  delay(b);
  }


void printSolution () //Ausgabe von Text um die berechneten Konzentrationen anzuzeigen
 {  
 lcd.setCursor(0, 0); 
 lcd.print("Concentration of");
 lcd.setCursor(0, 1);
 lcd.print("the solutions:");
 dcd(4000, 500);
     
     lcd.print(Conc1);
     dcd(2000, 400);
     lcd.print(Conc2);
     dcd(2000,400);
     lcd.print(Conc3);
     dcd(2000, 400);
     lcd.print(Conc4);
     dcd(2000,400);
     lcd.print(Conc5);
     dcd(2000, 400);
     lcd.print(Conc6);
     dcd(2000,400);
    }

void cursor0 ()
{
  lcd.setCursor(0, 0);
}

void loop() {
// printSolution ();
// printinputConc (low/hi);

Insert_lowest_Concentration:
  
  printinputConc(low);                                   //"insert lowest concentration"
  // keypadInput(lowConc);                                 //input w keypad 
  while (lowConc == 0) {
    char key = keypad.getKey();

    if (key) {
      lcd.print(key);

      if (key >= '0' && key <= '9') {     // only act on numeric keys
        inputString += key;               // append new character to input string
        lcd.clear();
        cursor0();
        lcd.print(inputString);
      }
      else if (key == '#')
      {
        if (inputString.length() > 0)
        {
          lowConc = inputString.toInt();
          lcd.clear();               // clear screen
          inputString = "";
          // break;  //manually breaks the while loop. Quick fix for failure of a = inputInt if inputInt is used after that
        }
      }
      else if (key == '*')
      {
        inputString = "";            // clear input
        lcd.clear();               // clear screen
      }
      else if (key == '.')
      {
      inputString += key;               // append new character to input string
        lcd.clear();
        cursor0();
        lcd.print(inputString);  
      }
    }
  }

// void Confirmlow () "[lowConc] Confirm?"      {
  lcd.clear();
  cursor0();
  lcd.print(lowConc);
  lcd.setCursor(0, 1);
  lcd.print("Confirm?");

  while (whileloop == 0) {
    char key = keypad.getKey();

    if (key) {
      if  (key == '#') {
        break;
      }
      else if (key == '*') {
        // inputString = "";
        // // inputInt = inputString.toInt();          // clear input
        // lowConc = inputString.toInt();
        // // a = inputInt;              //clear variable
        lcd.clear();               // clear screen
        goto Insert_lowest_Concentration;
      }
    }
  }

Insert_highest_Concentration:
printinputConc(hi);                                 //"insert highest concentration"
  // keypadInput(hiConc);                                 //input w keypad 
 while (hiConc == 0) {
    char key = keypad.getKey();

    if (key) {
      lcd.print(key);

      if (key >= '0' && key <= '9') {     // only act on numeric keys
        inputString += key;               // append new character to input string
        lcd.clear();
        cursor0();
        lcd.print(inputString);
      }
      else if (key == '#')
      {
        if (inputString.length() > 0)
        {
          //inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
          //a = inputInt;              // should save the number in inputInt as either lowConc or hiConc. !!!!!! This Line has to work
          hiConc = inputString.toInt();
          lcd.clear();               // clear screen
          inputString = "";
          // break;  //manually breaks the while loop. Quick fix for failure of a = inputInt if inputInt is used after that
        }
      }
      else if (key == '*')
      {
        inputString = "";            // clear input
        lcd.clear();               // clear screen
      }
    else if (key == '.')
      {
      inputString += key;               // append new character to input string
        lcd.clear();
        cursor0();
        lcd.print(inputString);  
      }
    }
  }
 
// Confirmhi();  // "[hiConc] Confirm?"
  lcd.clear();
  cursor0();
  lcd.print(hiConc);
  lcd.setCursor(0, 1);
  lcd.print("Confirm?");

  while (whileloop == 0) {
    char key = keypad.getKey();

    if (key) {
      if  (key == '#') {
        break;
      }
      else if (key == '*') {
        // inputString = "";
        // // inputInt = inputString.toInt();          // clear input
        // hiConc = inputString.toInt();
        // // a = inputInt;              //clear variable
        lcd.clear();               // clear screen
        goto Insert_highest_Concentration;
      }
    }
  }

 printSolution();                                     //"[Conc1]","[Conc1]","[Conc1]","[Conc1]","[Conc1]","[Conc1]"

// startcon() //Start filling
  lcd.clear();
  cursor0();
  lcd.print("Start filling?");

  while (whileloop == 0) {
    char key = keypad.getKey();

    if (key) {
      if  (key == '#') {
        break;
      }
      else if (key == '*') {
        inputString = "";
        // inputInt = inputString.toInt();          // clear input
        lowConc = inputString.toInt();
        // a = inputInt;              //clear variable
        lcd.clear();               // clear screen
        goto Insert_lowest_Concentration;
      }
    }
  }


}

输入和确认输出之间的值不会改变,因为它正确显示。问题不在计算中,或者至少不应该在计算中,因为当我手动为脚本中的变量输入正确的数字时,ot 正确显示了 6 个计算值。 我也找不到重置它的命令。

arduino variable-assignment
1个回答
0
投票

我完全重写了你的代码,因为它有点粗糙。您必须避免 Goto,并且必须为多次使用的编码块创建单独的函数。

这使得代码可读、易懂且易于调试。

更重要的是我使用了Tofloat()函数将输入的字符串转换为浮点数,这应该可以解决你的问题。

代码编译得很好,但我不能保证它第一次能工作。它需要一些改进来检查输入值等等。

希望有帮助!

//SDA to Pin A4
//SCL to Pin A5

#include <Keypad.h>
#include <LCD_I2C.h>

//_Declaration_
//Display
LCD_I2C lcd(0x27, 16, 2);
//Keypad
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6' , 'B'},
  {'7', '8', '9' , 'C'},
  {'*', '0', '#', '.'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6};   //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

//*Variables
//Keypad
String inputString; //Working memory
long inputInt; //long term memory

//User input number pad
float lowConc; // Memory of the lowest Concentration
float hiConc; // Memory f the highest Concentration

// Calculated distances between lowConc and highConc to make 6 Meassurements
float Abs = (hiConc - lowConc) / 5;

//Concentration of the Calibrationsolutions
float Conc1 = lowConc;
float Conc2 = lowConc + Abs;
float Conc3 = Conc2 + Abs;
float Conc4 = Conc3 + Abs;
float Conc5 = Conc4 + Abs;
float Conc6 = Conc5 + Abs;

//Textbloecke
String low = "lowest";
String hi = "highest";

void setup() {
  lcd.begin();
  lcd.backlight();
  inputString.reserve(10); // maximum number of digit for a number is 10, change if needed
}


void loop() {
  Insert_lowest_Concentration(); //get low conc until '#'
  Insert_highest_Concentration(); //get high conc until '#'
  printSolution();  //print result and raz low conc 
}


void Insert_lowest_Concentration() {
  printinputConc(low);                                   //"insert lowest concentration"
  lowConc = readInput();
  // void Confirmlow () "[lowConc] Confirm?"      {
  lcd.clear();
  cursor0();
  lcd.print(lowConc);
  lcd.setCursor(0, 1);
  lcd.print("Confirm?");

  if (confirm()) Insert_lowest_Concentration();
}

void Insert_highest_Concentration() {
  printinputConc(hi);                                 //"insert highest concentration"
  hiConc = readInput();   //get hiConc
  // Confirmhi();  // "[hiConc] Confirm?"
  lcd.clear();
  cursor0();
  lcd.print(hiConc);
  lcd.setCursor(0, 1);
  lcd.print("Confirm?");

  if (confirm()) Insert_highest_Concentration();
}

void printSolution () //Ausgabe von Text um die berechneten Konzentrationen anzuzeigen
{
  lcd.setCursor(0, 0);
  lcd.print("Concentration of");
  lcd.setCursor(0, 1);
  lcd.print("the solutions:");
  dcd(4000, 500);

  lcd.print(Conc1);
  dcd(2000, 400);
  lcd.print(Conc2);
  dcd(2000, 400);
  lcd.print(Conc3);
  dcd(2000, 400);
  lcd.print(Conc4);
  dcd(2000, 400);
  lcd.print(Conc5);
  dcd(2000, 400);
  lcd.print(Conc6);
  dcd(2000, 400);

  // startcon() //Start filling
  lcd.clear();
  cursor0();
  lcd.print("Start filling?");

  if (confirm()) Insert_lowest_Concentration();
}


float readInput() {
  String typedChars;
  while (true) {
    char key = keypad.getKey();
    if (key) {
      lcd.print(key);

      if (key >= '0' && key <= '9') {     // only act on numeric keys
        typedChars += key;               // append new character to input string
        lcd.clear();
        cursor0();
        lcd.print(typedChars);
      }
      else if (key == '#')
      {
        if (typedChars.length() > 0)
        {
          lcd.clear();               // clear screen
          return typedChars.toFloat();
        }
      }
      else if (key == '*')
      {
        typedChars = "";            // clear input
        lcd.clear();               // clear screen
      }
      else if (key == '.')
      {
        typedChars += key;               // append new character to input string
        lcd.clear();
        cursor0();
        lcd.print(typedChars);
      }
    }
  }

}

boolean confirm() {
  while (true) {
    char key = keypad.getKey();

    if (key) {
      if  (key == '#') {
        return false;
      }
      else if (key == '*') {
        lcd.clear();   // clear screen
        return true;
      }
    }
  }
}

void printinputConc(String str) {
  //do what you want
}

void cursor0 ()
{
  lcd.setCursor(0, 0);
}

void dcd (int a, int b) //Code block to delay, clear display and delay again
{
  delay(a);
  lcd.clear();
  delay(b);
}
© www.soinside.com 2019 - 2024. All rights reserved.