Arduino程序Bug排查:lowConc与highConc值为何被重置为0?
Arduino程序输出校准浓度全为0的问题修复
问题根源
你代码中的Abs、Conc1至Conc6是全局变量,它们的初始化逻辑在程序启动阶段(setup()执行前)就会运行。此时lowConc和hiConc的默认值为0,导致这些计算变量被初始化为0。后续通过键盘输入更新lowConc和hiConc后,这些变量不会自动重新计算——全局变量的初始化只会执行一次。
手动赋值测试正常是因为你直接在全局变量定义时给lowConc/hiConc赋值,初始化阶段就能得到正确的计算结果。
修复方案
将浓度计算逻辑移到获取完lowConc和hiConc之后执行,确保每次输入新值后都重新计算校准浓度。可以把计算逻辑封装成一个函数,在确认完最高浓度后调用。
修正后的完整代码
//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 of the highest Concentration // Calculated distances between lowConc and highConc to make 6 Measurements float Abs; //Concentration of the Calibrationsolutions float Conc1, Conc2, Conc3, Conc4, Conc5, Conc6; //Textbloecke String low = "lowest"; String hi = "highest"; // 新增计算校准浓度的函数 void calculateCalibrationConcs() { Abs = (hiConc - lowConc)/5; Conc1 = lowConc; Conc2 = lowConc + Abs; Conc3 = Conc2 + Abs; Conc4 = Conc3 + Abs; Conc5 = Conc4 + Abs; Conc6 = Conc5 + Abs; } 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); } // 补充缺失的printinputConc函数 void printinputConc(String type) { lcd.clear(); cursor0(); lcd.print("Insert " + type); lcd.setCursor(0, 1); lcd.print("concentration:"); } void loop() { Insert_lowest_Concentration: printinputConc(low); //"insert lowest concentration" 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.toFloat(); // 改用toFloat支持小数输入 lcd.clear(); // clear screen inputString = ""; } } 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); } } } 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 == '*') { lowConc = 0; // 重置变量以便重新输入 lcd.clear(); goto Insert_lowest_Concentration; } } } Insert_highest_Concentration: printinputConc(hi); //"insert highest concentration" 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) { hiConc = inputString.toFloat(); // 改用toFloat支持小数输入 lcd.clear(); // clear screen inputString = ""; } } 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); } } } 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 == '*') { hiConc = 0; // 重置变量以便重新输入 lcd.clear(); goto Insert_highest_Concentration; } } } // 输入完成后计算校准浓度 calculateCalibrationConcs(); printSolution(); //输出计算后的浓度值 lcd.clear(); cursor0(); lcd.print("Start filling?"); while (whileloop == 0) { char key = keypad.getKey(); if (key) { if (key == '#') { break; } else if (key == '*') { inputString = ""; lowConc = 0; hiConc = 0; lcd.clear(); goto Insert_lowest_Concentration; } } } }
额外优化点:
- 补充了缺失的
printinputConc函数(原代码中调用但未定义) - 将
toInt()改为toFloat(),支持小数浓度输入 - 在重新输入时重置
lowConc/hiConc为0,避免循环条件失效
内容的提问来源于stack exchange,提问作者BogBod20
相关产品推荐
相关产品推荐

