找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4035|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

Arduino 開關(guān)計數(shù)器控制兩個LED神燈

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
#
ID:118080 發(fā)表于 2016-5-3 10:16 | 只看該作者 回帖獎勵 |正序瀏覽 |閱讀模式
本帖最后由 shanzhiyuan123 于 2016-5-3 12:51 編輯

Hi,大家好。小弟是新人發(fā)帖, 我想做一個Project 用Arduino記錄兩個開關(guān)的按鍵次數(shù)并且用LCD顯示出來,如果哪個按鍵次數(shù)多,哪個led神燈就亮。時間限制大概在一分鐘左右。 也就是說一分鐘之后比較兩個開關(guān)的按鍵次數(shù)借此來決定打開哪個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!");  
}


這是記錄開關(guān)按鍵次數(shù)的代碼:
#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;
}

我想把這部分代碼組合到一起, 在倒計時的同時記錄兩個開關(guān)的按鍵次數(shù)。 當(dāng)?shù)褂嫊r結(jié)束時,停止兩個開關(guān)的計數(shù)。不知道怎么做,總是一個工作另外一個就不工作。。求大神解答


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表