|
本帖最后由 shanzhiyuan123 于 2016-5-3 12:51 编辑
Hi,大家好。小弟是新人发帖, 我想做一个Project 用Arduino记录两个开关的按键次数并且用LCD显示出来,如果哪个按键次数多,哪个led神灯就亮。时间限制大概在一分钟左右。 也就是说一分钟之后比较两个开关的按键次数借此来决定打开哪个LED灯。有大神有代码吗?
这是计时器的代码:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
int runTimer = 1;
int runFor = 20; // time in seconds
int buzzerPin = 13;
int relayPin=4;
int data = 0;
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin,OUTPUT);
lcd.begin(16, 2);
}
void loop() {
if(runTimer == 1){
digitalWrite(relayPin,LOW); // relay is OFF during countdown
lcd.clear();
lcd.print("TIMER=");
//Start timer
timer();
}
runTimer = 0;
lcd.noDisplay();
delay(250);
for(int duration = 0; duration < 100; duration ++){
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(500);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(500);
}
lcd.display();
delay(250);
}
void timer() {
for(int timer = runFor;timer > 0; --timer){
if(timer >= 10) {
lcd.setCursor(6,0);
} else {
lcd.setCursor(6,0);
lcd.print("0");
lcd.setCursor(7,0);
}
lcd.print(timer);
lcd.print(" SECOND!");
delay(1000);
}
lcd.setCursor(0,0);
lcd.clear();
lcd.print(" TIMER OUT!");
}
这是记录开关按键次数的代码:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
const int switchPin = 5;
int hits = 0;
int switchState = 0;
int prevSwitchState = 0;
const int switchPin1 = 6;
int hits1 = 0;
int switchState1 = 0;
int prevSwitchState1 = 0;
void setup() {
// set up the number of columns and rows on the LCD
lcd.begin(16, 2);
pinMode(switchPin,INPUT);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hit the button");
pinMode(switchPin1,INPUT);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hit the button");
}
void loop() {
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) {
if (switchState == LOW) {
hits = hits + 1;
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Hits:");
lcd.setCursor(7, 1);
lcd.print(hits);
lcd.setCursor(11, 1);
lcd.print(hits1);
delay(90);
}
}
prevSwitchState = switchState;
switchState1 = digitalRead(switchPin1);
if (switchState1 != prevSwitchState1) {
if (switchState1 == LOW) {
hits1 = hits1 + 1;
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Hits:");
lcd.setCursor(11, 1);
lcd.print(hits1);
lcd.setCursor(7, 1);
lcd.print(hits);
delay(90);
}
}
prevSwitchState1 = switchState1;
}
我想把这部分代码组合到一起, 在倒计时的同时记录两个开关的按键次数。 当倒计时结束时,停止两个开关的计数。不知道怎么做,总是一个工作另外一个就不工作。。求大神解答
|
|