找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1621|回复: 1
打印 上一主题 下一主题
收起左侧

定时开关计数器

[复制链接]
跳转到指定楼层
楼主
ID:118080 发表于 2016-5-8 06:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
400黑币
这是小弟的代码
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
int runTimer = 1;
int runFor = 50; // time in seconds
int buzzerPin = 13;
int relayPin=4;
int data = 0;

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() {
{

   pinMode(buzzerPin, OUTPUT);
   pinMode(relayPin,OUTPUT);
   lcd.begin(16, 0);
}
{
   lcd.begin(16, 2);
   pinMode(switchPin,INPUT);



  pinMode(switchPin1,INPUT);

}
}

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;

   if(runTimer == 1){
      digitalWrite(relayPin,LOW); // relay is OFF during countdown

      lcd.clear();
      lcd.print("TIMER=");

      lcd.setCursor(0, 1);
      lcd.print("Hits:");
      lcd.setCursor(7, 1);
      lcd.print(hits);
      lcd.setCursor(11, 1);
      lcd.print(hits1)
      //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(1,1);
   lcd.clear();
   lcd.print(" TIMER OUT!");

      lcd.setCursor(0, 1);
      lcd.print("Hits:");
      lcd.setCursor(7, 1);
      lcd.print(hits);
      lcd.setCursor(11, 1);
      lcd.print(hits1);
}

当我用Arduino UNO 测试时,计时器工作但是开关计计数不工作。。请大神指点

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表