“我正在尝试制作一个程序,我通过一个按钮控制了6种模式,问题是在SOS例程之后,例程会一直持续,并且不符合按钮按下直到除非我时间与SOS的最后一个S完全吻合。下面是程序,请解释如何只需按一下按钮即可退出例行程序。
其次可以将此程序转换为.hexfile吗?对于PIC12微控制器? 问候, Fawad“
// Uses button presses to switch an LED between off, dim, medium, high brightness & SOS.
// constants for this sketch
const int buttonPin = 2; // pushbutton pin
const int led = 9; // LED pin
int dit = 500; //MORSE CODE dit
int dah = dit*3; //MORSE CODE dah
int blankTime = 100; // GAP IN BETWEEN DIT AND DAH
// variables for this sketch
int ledMode = 1; // variable for recording button presses
int ledState = 6;
void setup()
{
// initialize the output pins:
pinMode(led, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// check if the pushbutton is pressed.
if (digitalRead(buttonPin) == HIGH)
{ delay(100);
// if it is, count a press
ledMode = ledMode + 1;
// if we've counted to an invalid mode
if (ledMode == 6)
{delay(100);
// reset to mode 1
ledMode = 1;
}
}
// Detect the current mode and set the LED appropriately
if (ledMode == 1)
{
//Mode 1 - LED is off
analogWrite(led, 0);
}
else if (ledMode == 2)
{
//Mode 2 - LED is dim
analogWrite(led, 64);
delay(100);
}
else if (ledMode == 3)
{
//Mode 3 - LED is medium
analogWrite(led, 128);
delay(100);
}
else if(ledMode==4){
//Mode 4 - LED is bright
analogWrite(led, 255);
delay(100);
}
else if(ledMode==5)
{
//Mode 5 -MORSE CODE
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); } // wait
if(ledState==6){
return;}
//return ;
}
答案 0 :(得分:0)
更频繁地使用digitalRead
。
在每个" SOS"的中间和末尾添加一个验证例程的按钮。字母以清除ledMode
。
像:
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
if (digitalRead(buttonPin) == HIGH) //READ
{ delay(100); //DELAY
ledMode = ledMode + 0; //CLEAR
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
if (digitalRead(buttonPin) == HIGH) //READ
{ delay(100); //DELAY
ledMode = ledMode + 0; //CLEAR
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
if (digitalRead(buttonPin) == HIGH) //READ
{ delay(100); //DELAY
ledMode = ledMode + 0; //CLEAR