极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4372|回复: 2

PCB定时器求助.

[复制链接]
发表于 2016-9-11 10:43:09 | 显示全部楼层 |阅读模式
采用ATMEGA328P MINI板。1602A LED显示。代码如下:
#include <LiquidCrystal.h>
#include <stdio.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int minutes = 0;
int seconds = 0;
int countdown = 0;
int button1 = 14;           // start
int button2 = 15;           // seconds
int button3 = 16;           // minutes
int button4 = 17;           // clear
int lightBoardPin = 9;
int buzzerPin = 8;
int ledPin = 13;
boolean startCountdown = false;
int logic1, logic2, logic3, logic4;
void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(lightBoardPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("-Exposure Time-");
  lcd.setCursor(0, 1);
  lcd.print("- Press SETUP -");
  digitalWrite(lightBoardPin, LOW);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
}
void splashscreen()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Exposure Time");
  lcd.setCursor(0, 1);
  lcd.print("* Press SETUP *");
}
void buzzer()
{
  for (int x = 0; x <= 3; x++)
  {
    tone(buzzerPin, 1500, 100);
    delay(200);
    tone(buzzerPin, 1500, 100);
    delay(200);
    tone(buzzerPin, 1500, 100);
    delay(200);
    tone(buzzerPin, 1500, 100);
    delay(200);
  }
}
void uberTime()
{
  countdown = ((minutes * 60) + seconds);
  digitalWrite(lightBoardPin, HIGH);
  digitalWrite(ledPin, HIGH);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Exposing Board");
  lcd.setCursor(0, 1);
  lcd.print("Time:");
  lcd.setCursor(11, 1);
  lcd.print(" Secs");
  for (int timer = countdown; timer >= 0; timer --)
  {
    char buffer[6];
    sprintf(buffer, "%4d", countdown);
    lcd.setCursor(7, 1);
    lcd.print(buffer);
    delay(1000);
    countdown = countdown - 1;
  }
  digitalWrite(lightBoardPin, LOW);
  digitalWrite(ledPin, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" ** ALL DONE **");
  lcd.setCursor(0, 1);
  lcd.print("  Press START");
  buzzer();
  logic1 = digitalRead(button1);
  while (logic1 == 0)
  {
    logic1 = digitalRead(button1);
  }
  startCountdown = false;
  minutes = 0;
  seconds = 0;
  countdown = 0;
}
void setupTimer()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  - Set Time -  ");
  lcd.setCursor(0, 1);
  lcd.print("Then Press Start");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Set Expose Time");
  lcd.setCursor(0, 1);
  lcd.print("Min: ");
  lcd.setCursor(9, 1);
  lcd.print("Sec: ");
  while (startCountdown == true)
  {
    logic2 = digitalRead(button2);
    if ( logic2 == 0 )                  // SECONDS ADJUST
    {
      seconds ++;
      delay(300);
      if (seconds >= 60)
      {
        seconds = 0;
        minutes ++;
      }
    }
    char buffer[3];
    sprintf(buffer, "%2d", seconds);
    lcd.setCursor(14, 1);
    lcd.print(buffer);
    logic3 = digitalRead(button3);
    if ( logic3 == 0 )                  // MINUTES ADJUST
    {
      minutes ++;
      delay(500);
    }
    lcd.setCursor(6, 1);
    lcd.print(minutes);
    logic4 = digitalRead(button4);
    if ( logic4 == 0 )                  // clear timer
    {
      seconds = 0;
      minutes = 0;
    }
    logic1 = digitalRead(button1);
    if (logic1 == 0 )                  // START
    {
      startCountdown = true;
    }
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Exposure Start");
  lcd.setCursor(0, 1);
  lcd.print("Timer = ");
  lcd.setCursor(8, 1);
  lcd.print(countdown);
  lcd.setCursor(12, 1);
  lcd.print("Secs");
  buzzer();
  uberTime();
}
void loop()
{
  splashscreen();
  logic1 = digitalRead(button1);
  logic2 = digitalRead(button2);
  logic3 = digitalRead(button3);
  if (logic1 == 0)                 // SETUP & START BUTTON
  {
    setupTimer();
  }
  Serial.print("button1 =");
  Serial.print(digitalRead(button1));
  Serial.print("button2 =");
  Serial.print(digitalRead(button2));
  Serial.print("button3 =");
  Serial.println(digitalRead(button3));
  delay(100);
}
参考了个instructable网站上的一个老外的程序。下载完成后,显示正常,不知为什么所有的按键都不起作用
回复

使用道具 举报

发表于 2016-9-11 14:07:44 | 显示全部楼层
int button1 = 14;           // start
int button2 = 15;           // seconds
int button3 = 16;           // minutes
int button4 = 17;           /

印像中,有些版本的IDE不能直接用14,15,...
要用AO,A1,..
回复 支持 反对

使用道具 举报

发表于 2016-9-12 14:44:15 | 显示全部楼层
检查一下电平?
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-10 03:27 , Processed in 0.036687 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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