#include <EEPROM.h>
#include <TimerOne.h>
#include <avr/pgmspace.h>
#include "TM1637.h"
#define ON 1
#define OFF 0
int8_t TimeDisp[] = {0x00, 0x00, 0x00, 0x00};
unsigned char ClockPoint = 1;
unsigned char Update;
unsigned char microsecond_10 = 0;
unsigned char second;
unsigned char _microsecond_10 = 0;
unsigned char _second;
unsigned int eepromaddr;
boolean Flag_ReadTime;
int state;
#define CLK 16//pins definitions for TM1637 and can be changed to other ports
#define DIO 17
TM1637 tm1637(CLK, DIO);
int L;
int R;
void setup()
{
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(15, INPUT);
tm1637.init();
Timer1.initialize(10000);//timing for 10ms
Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR
tm1637.set(BRIGHT_DARKEST);
state = 0;
digitalWrite(5, LOW);
digitalWrite(6, LOW);
L = 960;
R = 970;
}
void loop()
{
stopwatchReset();
while (1)
{
if (analogRead(7) < R && analogRead(0) < L && state == 0)
{
while (analogRead(7) < R && analogRead(0) < R)
{
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(100);
if (analogRead(7) > R || analogRead(0) > L)
{
digitalWrite(5, LOW);
digitalWrite(6, LOW);
stopwatchStart();
state = 1;
Serial.println(state);
break;
}
}
}
if (analogRead(7) < R && analogRead(0) < L && state == 1)
{
stopwatchPause();
Serial.println(state);
state = 2;
Serial.println(state);
delay(500);
}
if (digitalRead(15) == HIGH && (state == 2 || state == 1))
{
stopwatchReset();
state = 0;
Serial.println(state);
}
if (analogRead(7) < R && state == 1)
{
digitalWrite(6, HIGH);
}
if (analogRead(0) < L && state == 1)
{
digitalWrite(6, HIGH);
}
if (analogRead(7) < R && analogRead(0) < L && state == 2)
{
digitalWrite(6, HIGH);
}
// case 'S':stopwatchStart();Serial.println("Start timing...");break;
// case 'P':stopwatchPause();Serial.println("Stopwatch was paused");break;
// case 'L':readTime();break;
// case 'W':saveTime();Serial.println("Save the time");break;
// case 'R':stopwatchReset();Serial.println("Stopwatch was reset");break;
if (Update == ON)
{
TimeUpdate();
tm1637.display(TimeDisp);
}
else
{
digitalWrite(6, LOW);
}
}
}
void TimingISR()
{
microsecond_10 ++;
Update = ON;
if (microsecond_10 == 100) {
second ++;
if (second == 60)
{
second = 0;
}
microsecond_10 = 0;
}
ClockPoint = (~ClockPoint) & 0x01;
if (Flag_ReadTime == 0)
{
_microsecond_10 = microsecond_10;
_second = second;
}
}
void TimeUpdate(void)
{
if (ClockPoint)tm1637.point(POINT_ON); //POINT_ON = 1,POINT_OFF = 0;
else tm1637.point(POINT_ON);
TimeDisp[2] = _microsecond_10 / 10;
TimeDisp[3] = _microsecond_10 % 10;
TimeDisp[0] = _second / 10;
TimeDisp[1] = _second % 10;
Update = OFF;
}
void stopwatchStart()//timer1 on
{
Flag_ReadTime = 0;
TCCR1B |= Timer1.clockSelectBits;
}
void stopwatchPause()//timer1 off if [CS12 CS11 CS10] is [0 0 0].
{
TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
}
void stopwatchReset()
{
stopwatchPause();
Flag_ReadTime = 0;
_microsecond_10 = 0;
_second = 0;
microsecond_10 = 0;
second = 0;
Update = ON;
}
void saveTime()
{
EEPROM.write(eepromaddr ++, microsecond_10);
EEPROM.write(eepromaddr ++, second);
}
void readTime()
{
Flag_ReadTime = 1;
if (eepromaddr == 0)
{
Serial.println("The time had been read");
_microsecond_10 = 0;
_second = 0;
}
else {
_second = EEPROM.read(-- eepromaddr);
_microsecond_10 = EEPROM.read(-- eepromaddr);
Serial.println("List the time");
}
Update = ON;
}
Comments
Please log in or sign up to comment.