#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int _button ;
int _time ;
// For these LCD controls to work you MUST replace the standard LCD library from...
// https://github.com/marcoschwartz/LiquidCrystal_I2C
// Direct download https://github.com/marcoschwartz/LiquidCrystal_I2C/archive/master.zip
// Your project will not compile until this is done.
LiquidCrystal_I2C lcd_I2C_27(0x27,16,2); // set the LCD address for a 16 chars and 2 line display
void setup(){ // put your setup code here, to run once:
lcd_I2C_27.init (); // 設定LCD面板
lcd_I2C_27.backlight();
pinMode( 10 , INPUT); // sets the digital pin as input
pinMode( 12 , OUTPUT); // sets the digital pin as output
pinMode( 11 , OUTPUT); // sets the digital pin as output
_button = 0 ; // 設定按鈕數值
_time = 0 ; // 設定時間初始值
lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0
lcd_I2C_27.print( "Start Timing!" ); // Print a message to the LCD.
}
void loop(){ // 主程式迴圈
if (digitalRead( 10 )) { // 檢測按鈕是否按下
if ( _button == 0 ) {
_button = 1 ;
delay( 500 ); // waits a few milliseconds
}
else {
_button = 0 ;
delay( 500 ); // waits a few milliseconds
}
}
if ( _time == 10 ) { // 這個if迴圈是設定時間到後的聲音
_button = 0 ;
_time = 0 ;
digitalWrite( 12 , LOW ); // sets the digital pin on/off
digitalWrite( 11 , HIGH ); // sets the digital pin on/off
tone(8, 392, 200); //buzzer or speaker frequency
delay( 200 ); // waits a few milliseconds
tone(8, 524, 200); //buzzer or speaker frequency
delay( 200 ); // waits a few milliseconds
tone(8, 660, 200); //buzzer or speaker frequency
delay( 200 ); // waits a few milliseconds
tone(8, 784, 200); //buzzer or speaker frequency
delay( 200 ); // waits a few milliseconds
tone(8, 1046, 200); //buzzer or speaker frequency
delay( 200 ); // waits a few milliseconds
tone(8, 1318, 200); //buzzer or speaker frequency
delay( 200 ); // waits a few milliseconds
tone(8, 1568, 500); //buzzer or speaker frequency
delay( 200 ); // waits a few milliseconds
tone(8, 1046, 2000); //buzzer or speaker frequency
}
if ( _button == 1 ) { // 當按鈕按下時的LED燈
digitalWrite( 12 , HIGH ); // sets the digital pin on/off
digitalWrite( 11 , LOW ); // sets the digital pin on/off
_time = ( _time + 1 ) ;
lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0
lcd_I2C_27.print( _time ); // Print a message to the LCD.
delay( 1000 ); // waits a few milliseconds
}
else { // 當按鈕沒被按下時的LED燈
_time = 0 ;
digitalWrite( 12 , LOW ); // sets the digital pin on/off
digitalWrite( 11 , HIGH ); // sets the digital pin on/off
}
}