因此,我正在制作一個Arduino RichShield計算器,它的作業原理是這樣的:
我選擇第一個數字的值
我需要解決的是,如果我按住按鈕,它就會不斷切換狀態。我需要讓它變得只有正常的按鈕才能改變狀態。以下是代碼:
#include "Display.h"/span>
//declaring the button and LEDs
const int PIN_BUTTON = 9;
int buttonState = 0;
const int PIN_LEDR = 4;
const int PIN_LEDG = 5;
const int PIN_LEDB = 6;
const int PIN_LEDY = 7;
//declaring potentiometer variables
const int PIN_POTMETER = 14;
int MAX_NUM = 20;
int MIN_NUM = -20;
int potValue = 0;
int potValueForCalculations = 0;
//設定按鈕、LED、電位器的輸入/輸出。
void setup(){
pinMode(PIN_LEDR, OUTPUT)。
pinMode(PIN_LEDG, OUTPUT)。
pinMode(PIN_LEDB, OUTPUT)。
pinMode(PIN_LEDY,OUTPUT)。
pinMode(PIN_POTMETER,INPUT)。
pinMode(PIN_BUTTON, INPUT_PULLUP)。
Display.show("----"/span>);
delay(3000)。
}
/declaring variables for mathematical operations/button counter; }
int buttonCounter = 0;
float inputValue1;
float inputValue2;
float result;
char mathOperator;
void loop() {
buttonState = digitalRead(PIN_BUTTON)。
//state 1-選擇第一個值。
if (buttonCounter == 0) {
//映射并在顯示幕上顯示potValueForCalculations。
potValue = analogRead(PIN_POTMETER)。
potValueForCalculations = map(potValue, 0, 1023, MIN_NUM, MAX_NUM)。
Display.show(potValueForCalculations)。
delay(100)。
//如果我們按下按鈕就會進入狀態2。
if (buttonState == LOW) {
inputValue1 = potValueForCalculations;
buttonCounter = 1;
delay(1000)。
}
}
//state 2 選擇數學運算的符號。
else if (buttonCounter == 1) {
potValue = analogRead(PIN_POTMETER)。
if (potValue <= 256) {
Display.show("a")。
mathOperator = 'a';
}
else if (potValue > 256 && potValue < = 512) {
Display.show("s")。
mathOperator = 's';
}
else if (potValue > 512 && potValue < = 768) {
Display.show("t")。
mathOperator = 't';
}
else {
Display.show("d") 。
mathOperator = 'd';
}
//if we press button we go into state 3; }
if (buttonState == LOW) {
buttonCounter = 2;
delay(500)。
}
}
//state 3- choosing the second value.
else if (buttonCounter == 2) {
potValue = analogRead(PIN_POTMETER)。
potValueForCalculations = map(potValue, 0, 1023, MIN_NUM, MAX_NUM)。
Display.show(potValueForCalculations)。
delay(100)。
//如果我們按下按鈕就會進入狀態4。
if (buttonState == LOW) {
inputValue2 = potValueForCalculations;
buttonCounter = 3;
delay(500)。
}
}
//state 4- showing the result.
else if (buttonCounter == 3) {
digitalWrite(PIN_LEDG, HIGH)。
digitalWrite(PIN_LEDR, LOW);
if (mathOperator == 'a'/span>) {
結果 = inputValue1 inputValue2;
Display.show(結果)。
}
else if (mathOperator == 's'/span>) {
結果 = inputValue1 - inputValue2。
Display.show(結果)。
}
else if (mathOperator == 't'/span>) {
結果 = inputValue1 * inputValue2;
Display.show(結果)。
}
else if (mathOperator == 'd'/span>) {
//如果我們試圖除以0,紅色的LED就會亮起,顯示 "Err"。
if (inputValue2 == 0) {
digitalWrite(PIN_LEDR, HIGH)。
digitalWrite(PIN_LEDG, LOW);
Display.show("Err") 。
}
else {
結果 = inputValue1 / inputValue2;
Display.show(結果)。
}
}
//如果我們再次按下按鈕,計數器進入復位閾值--計算器重新啟動。
if (buttonState == LOW) {
buttonCounter = 4;
delay(500)。
}
}
else if (buttonCounter >= 4) {
digitalWrite(PIN_LEDR, LOW)。
digitalWrite(PIN_LEDG, LOW);
digitalWrite(PIN_LEDB, LOW)。
digitalWrite(PIN_LEDY, LOW)。
Display.show(""/span>)。
buttonCounter = 0;
delay(500)。
}
uj5u.com熱心網友回復:
與其檢查buttonState是LOW,不如檢查buttonState改變為LOW。
它看起來大致是這樣的:
int prevButtonState =0;
int newButtonState = 0;
void loop() {
newButtonState = digitalRead(PIN_BUTTON)。
if( newButtonState == LOW && prevButtonState != LOW) {
/ ...。
}
prevButtonState = newButtonState。
}
N.B. 根據所涉及的硬體,你有時還需要考慮到按鈕在釋放時的 "彈跳 "情況。你可以通過檢查按鈕在被視為 "按下 "之前是否保持低位一段時間來實作這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/306962.html
標籤:
