Demo entry 6738247

ssss

   

Submitted by anonymous on May 03, 2018 at 18:21
Language: Arduino. Code size: 4.2 kB.

#include<EEPROM.h>
int segmentPins[] = {3, 2, 19, 16, 18, 4, 5, 17};//七段管数字控制,A0~A5是14~19
int displayPins[] = {14, 7, 15, 6}; //七段管开关对应管脚
int times[] = {5, 10, 15, 20, 30, 45, 100, 130, 200, 230, 300, 400, 500, 600, 700, 800, 900, 1000, 1500, 2000, 3000}; //时间数组
int numTimes = 19;
byte selectedTimeIndex;
int timerMinute;
int timerSecond;
int buzzerPin = 11; // 控制蜂鸣器的引脚
int aPin = 8;
int bPin = 10;
int buttonPin = 9; // 控制旋转编码器的三个引脚

boolean stopped = true;
byte digits[10][8] = {     //数字对应的数码管
  // a b c d e f g.
  {1, 1, 1, 1, 1, 1, 0, 0}, // 0
  {0, 1, 1, 0, 0, 0, 0, 0}, // 1
  {1, 1, 0, 1, 1, 0, 1, 0}, // 2
  {1, 1, 1, 1, 0, 0, 1, 0}, // 3
  {0, 1, 1, 0, 0, 1, 1, 0}, // 4
  {1, 0, 1, 1, 0, 1, 1, 0}, // 5
  {1, 0, 1, 1, 1, 1, 1, 0}, // 6
  {1, 1, 1, 0, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1, 0}, // 8
  {1, 1, 1, 1, 0, 1, 1, 0}, // 9
}; // 将输出的信号表示出来, 即1对应着高电平,使相应的灯亮起组成数字


void setup () {
  for (int i = 0; i < 8; i++) {
    pinMode(segmentPins[i], OUTPUT); //依次定义输出管脚:pinMode()定义输入输出
  }
  for (int i = 0; i < 4; i++) {
    pinMode(displayPins[i], OUTPUT); // 成为数组时 输出方便统一写
  }
  pinMode(buzzerPin, OUTPUT); // 将接着蜂鸣器的引脚作为输出
  pinMode(buttonPin, INPUT);
  pinMode(aPin, INPUT);
  pinMode(bPin, INPUT); // 将旋转编码器的三个引脚作为输出端
  selectedTimeIndex = EEPROM.read(0); //读取地址中存储的东西,断电后保存上一次
  timerMinute = times[selectedTimeIndex] / 100; //找到数组中第k个,
  timerSecond = times[selectedTimeIndex] % 100;//为何100?:230就是2分30秒
}


void loop() {
  if (digitalRead(buttonPin)) {  //若旋转编码器开关9有信号输入,开始设定时间状态
    stopped = !stopped; // 改变状态值
    digitalWrite(buzzerPin, LOW);  // 给蜂鸣器一个低电平,使他不发出声音
    while (digitalRead(buttonPin)) {}; // 在低电平时跳出, SW默认为高电平,按下后为低电平
    EEPROM.write(0, selectedTimeIndex); //将选择的数据保存到地址0 
  } 
  updateDisplay();
}


void updateDisplay() {          //更新显示模块函数  // mmss 
  int minsecs = timerMinute * 100 + timerSecond;
  int v = minsecs;
  for (int i = 0; i < 4; i++) {      //因为最高4位数所以最多做4次
    int digit = v % 10;
    setDigit(i);   //调用函数  
    setSegments(digit);  //调用函数  
    v = v / 10;
    process();
  }
  setDigit(5);   // all digits off to prevent uneven illumination 
}


void process() {   //两种状态,要么更改设定时间changeSetTime,这是一个时间流逝的过程
  for (int i = 0; i < 100; i++) {   //要么计时过程updateCountingTime	
    int change = getEncoderTurn();     //tweak this number between flicker and blur
    if (stopped) {                      //旋转编码器读取
      changeSetTime(change);             //调用函数
    }
    else {
      updateCountingTime();
    }
  }
  if (timerMinute == 0 && timerSecond == 0) {     //时间到蜂鸣器报警
    digitalWrite(buzzerPin, HIGH);
  }
}


void changeSetTime (int change) {
  selectedTimeIndex += change;        //因为旋钮是在原来的基础上增减
  if (selectedTimeIndex < 0) {
    selectedTimeIndex = numTimes;          //如果选择的东西不在数组内,就按照设定的第19个
  }
  else if (selectedTimeIndex > numTimes) {    //如果超出了就从头开始,相当于一个循环
    selectedTimeIndex = 0;
  }
  timerMinute = times[selectedTimeIndex] / 100;   //同setup 
  timerSecond = times[selectedTimeIndex] % 100;
}


void updateCountingTime() {       //倒计时函数
  static unsigned long lastMillis;    //静态变量可以保持上一次 
  unsigned long m = millis();          //读取当前cpu时间,以毫秒为单位,同c中clock 函数  
  if (m > (lastMillis + 1000) && (timerSecond > 0 || timerMinute > 0)) {  //1000的意思是每隔1秒变
    digitalWrite(buzzerPin, HIGH);
    delay(10);
    digitalWrite(buzzerPin, LOW);
    if (timerSecond == 0) {
      timerSecond = 59;
      timerMinute --;
    }
    else {
      timerSecond --;
    }
    lastMillis = m;
  }
}


void setDigit(int digit) {    //  输入数字,输出七段管 
  for (int i = 0; i < 4; i++) {
    digitalWrite(displayPins[i], (digit != i)); // 将这digit引脚设为ture,发光
  }
}


void setSegments(int n) {
  for (int i = 0; i < 8; i++) {
    digitalWrite(segmentPins[i], !digits[n][i]);
  }
}


int getEncoderTurn() {     //旋转编码器获取读数
  static int oldA = LOW;
  static int oldB = LOW;
  int result = 0; 
  int newA = digitalRead(aPin);
  int newB = digitalRead(bPin);
  if (newA != oldA || newB != oldB) {
    if (oldA == LOW && newA == HIGH) {
      result = -(oldB * 2 - 1);
    }
  }
  oldA = newA;
  oldB = newB;
  return result;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).