我正在从事一个项目,该项目保证在LED上运行按钮序列(以及时序)。当按下第一个按钮时,arduino开始读取第二个按钮的状态。存储第二个按钮上的按下顺序,直到再次按下第一个按钮。现在,已存储的按钮序列将复制到LED上。我想出了一个代码,该代码尝试执行除LED部分以外的基本功能。但我没有得到想要的输出。你们能帮我检查一下吗?
#include <Debounce.h>
const byte SWITCH = 2;
const byte SWITCH1 = 4;
const byte LED = 9;
int count = 0;
unsigned long startTime = 0;
unsigned long endTime = 0;
unsigned long startTime1 = 0;
unsigned long endTime1 = 0;
unsigned long elapsedTime = 0;
unsigned long elapsedTime1 = 0;
const byte debounceDelayTime = 50;
int a[100],i,j,flag = 0,flag1 = 0,b=0;
Debounce debouncer = Debounce(debounceDelayTime, SWITCH);
void setup(){
Serial.begin(9600);
pinMode(SWITCH, INPUT_PULLUP);
pinMode(SWITCH1, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
void loop(){
fun();
while(count == 1)
{
fun();
if(count == 1)
{
if(debouncer.update())
{
if(debouncer.read() == 1)
{
startTime = millis();
endTime1 = millis();
elapsedTime1 = endTime1 - startTime1;
Serial.println("Button Pressed");
Serial.println("Button Released for:");
Serial.print(elapsedTime1);
if(flag1 == 0)
{
for(j=b;j<100;j+2)
{
while(flag1 == 0)
{
b=j+2;
a[j] = elapsedTime;
flag1 = 1;
}
}
}
flag1 = 0;
}
else if(debouncer.read() == 0)
{
endTime = millis();
startTime1 = millis();
Serial.println("Button Released");
elapsedTime = endTime - startTime;
Serial.println("Button Held Down For ");
Serial.print(elapsedTime);
Serial.println(" ms.");
if(flag == 0)
{
for(j=b;j<100;j+2)
{
while(flag == 0)
{
b=j+2;
a[j] = elapsedTime;
flag = 1;
}
}
}
flag = 0;
}
}
}
else
count = 0;
}
Serial.println("A=");
for(i=0;i<100;i++)
{
Serial.println(a);
}
}
void fun()
{
if (digitalRead(SWITCH1) == 1)
{
count++;
while (digitalRead(SWITCH1) == 1);
{
delay(75);
}
}
else
count = 1;
}