//int ledState = HIGH; // the current state of the output pin int k=0 ; // Buzzer counter int Count_up =0 ; // Count up state int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin
constint number_of_cathode_pins = sizeof(cathode_pins) / sizeof(cathode_pins[0]);// 配列の数 int start_num=2 ; // Number to countdown from 20min=20*600sec initial 2HOURS int start_num1 = start_num;
// the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers unsignedlong time;
voidloop() { // read the state of the switch into a local variable:int reading = digitalRead(buttonPin);
// If the switch changed, due to noise or pressing: if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:// if the button state has changed: if (reading != buttonState) {
buttonState = reading;
// only increment ciunrter if the new button state is HIGH if (buttonState == HIGH) {
// ledState = !ledState;
// If button is pressed, increase timer number
if (Count_up == 0 ) {
start_num = start_num + 5 ;// If button is pushed add 30min // if ((start_num - (millis()/1000))> 50) { // start_num = 50 + (millis()/1000) ;
// 360sec =6min, Upper Limit = 50 * 6min = 300min(5 Hourrs) if (start_num - (millis()/360000) > 50 ) {
start_num = 50 + (millis()/360000) ;
}
}// If state is count up, restart from initial. else {
Count_up = 0;
k = 0; // Buzzer is InitializeddigitalWrite(RELAY_ON,HIGH); // Power_Relay is ON
// start_num = 10 + (millis()/1000);
start_num = 10 + (millis()/360000);
}
}
}
} // save the reading. Next time through the loop, // it'll be the lastButtonState:
lastButtonState = reading;
//Turn on a digit for a short amount of time switch(digit) { case 1: digitalWrite(digit1, HIGH); break; case 2: digitalWrite(digit2, HIGH); break;
}
//Turn on the right segments for this digit // lightNumber(toDisplay % 6); // toDisplay /= 6;
lightNumber(toDisplay % 10);
toDisplay /= 10;
// delayMicroseconds(DISPLAY_BRIGHTNESS); //Display digit for fraction of a second (1us to 5000us, 500 is pretty good)
//Turn off all segments delay(2) ;
lightNumber(0);
//Turn off all digits digitalWrite(digit1, LOW); digitalWrite(digit2, LOW);
} // // while( (millis() - beginTime) < 10) ; //Wait for 20ms to pass before we paint the display again
}
constint cathode_pins[] = {1,2,3,4,5,0,6}; // カソードに接続constint buttonPin = 7; // the number of the pushbutton pinconstint LED1 = 13 ; // LED blink 1sec durationconstint speker = 8 ; //speker on pin 8constint digit1 = 9; // Display digit 1constint digit2 = 10; // Display digit 2constint RELAY_ON = 12; // AC Relay Control//int ledState = HIGH; // the current state of the output pinint k=0 ; // Buzzer counterint Count_up =0 ; // Count up state int buttonState; // the current reading from the input pinint lastButtonState = LOW; // the previous reading from the input pinconstint number_of_cathode_pins = sizeof(cathode_pins) / sizeof(cathode_pins[0]);// 配列の数int start_num= 20 ; // Number to countdown from 20min=20*600sec initial 2HOURSint start_num1 = start_num;
// the following variables are long's because the time, measured in miliseconds,// will quickly become a bigger number than can be stored in an int.long lastDebounceTime = 0; // the last time the output pin was toggledlong debounceDelay = 50; // the debounce time; increase if the output flickersunsignedlong time;
// setup() は,最初に一度だけ実行されるvoidsetup() {
for (int i = 0; i < number_of_cathode_pins; i++) {
pinMode(cathode_pins[i], OUTPUT); // cathode_pinsを出力モードに設定する
}
pinMode(digit1, OUTPUT);
pinMode(digit2, OUTPUT);
pinMode(speker, OUTPUT); // speker on pin 8pinMode(RELAY_ON, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(buttonPin,INPUT); // Button Input// set initial AC Relay StatedigitalWrite(RELAY_ON,HIGH);
}
voidloop() {
// read the state of the switch into a local variable:int reading = digitalRead(buttonPin);
// If the switch changed, due to noise or pressing:if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer// than the debounce delay, so take it as the actual current state:// if the button state has changed:if (reading != buttonState) {
buttonState = reading;
// only increment ciunrter if the new button state is HIGHif (buttonState == HIGH) {
// ledState = !ledState;// If button is pressed, increase timer number if (Count_up == 0 ) {
start_num = start_num + 5 ;// If button is pushed add 30min// if ((start_num - (millis()/1000))> 50) { // start_num = 50 + (millis()/1000) ;// 360sec =6min Upper Limit = 50 * 6min = 300min (5 Hourrs)if (start_num - (millis()/360000) > 50 ) {
start_num = (50 + (millis()/360000)) ;
}
}
// If state is count up, restart from initial.else
{
Count_up = 0;
k = 0; // Buzzer is InitializeddigitalWrite(RELAY_ON,HIGH); // Power_Relay is ON// start_num = 10 + (millis()/1000);// restart from 10 x 6min = 1 Hour
start_num = 10 + (millis()/360000);
}
}
}
}
// save the reading. Next time through the loop,// it'll be the lastButtonState:
lastButtonState = reading;
//start_num limitation 50//long startTime = millis();// if((millis()/1000) < start_num){// displayNumber(start_num -(millis()/1000));// 360sec =6 min =36000 millis() count if((millis()/360000) < start_num){
displayNumber(start_num -(millis()/360000));
if ((millis()/500)% 2 == 1 ) {
digitalWrite(LED1, HIGH);
}
else {
digitalWrite(LED1, LOW);
}
}
else {
// reached zero, flash the display
Count_up =1; // count up state
k=k+1; // count_up buzzer count
time=millis();
while(millis() < time+200) {
displayNumber(0); // display 0 for 0.2 seconddigitalWrite(RELAY_ON,LOW); //TIME UP Relay OFFif(k<6) { tone (8, 1000, 100);
}
}
time=millis();
while(millis() < time+200) {
lightNumber(10); // Turn display off for 0.2 second
}
}
}
// 2ケタの表示を計算するvoid displayNumber(int toDisplay) {
long beginTime = millis();
for(int digit = 2 ; digit > 0 ; digit--) {
//Turn on a digit for a short amount of timeswitch(digit) {
case 1:
digitalWrite(digit1, HIGH);
break;
case 2:
digitalWrite(digit2, HIGH);
break;
}
//Turn on the right segments for this digit// lightNumber(toDisplay % 6);// toDisplay /= 6;
lightNumber(toDisplay % 10);
toDisplay /= 10;
// delayMicroseconds(DISPLAY_BRIGHTNESS); //Display digit for fraction of a second (1us to 5000us, 500 is pretty good)//Turn off all segmentsdelay(2) ;
lightNumber(0);
//Turn off all digitsdigitalWrite(digit1, LOW);
digitalWrite(digit2, LOW);
}
//// while( (millis() - beginTime) < 10) ; //Wait for 20ms to pass before we paint the display again
}
// 7SEG表示パターンconstint digits[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00100111, // 7
0b01111111, // 8
0b01101111, // 9
0b00000000, // 10
};
// 1けたの数字(n)を表示するvoid lightNumber (int n) {
for (int i = 0; i < number_of_cathode_pins; i++) {
digitalWrite(cathode_pins[i], digits[n] & (1 << i) ? LOW : HIGH);
}
}