/*
Arduino Starter Kit example
Project 8 - Digital Hourglass
This sketch is written to accompany Project 8 in the Arduino Starter Kit
Parts required:
- 10 kilohm resistor
- six 220 ohm resistors
- six LEDs
- tilt switch
created 13 Sep 2012
by Scott Fitzgerald
https://store.arduino.cc/genuino-starter-kit
This example code is part of the public domain.
*/// named constant for the switch pin
constintswitchPin=8;unsignedlongpreviousTime=0;// store the last time an LED was updated
intswitchState=0;// the current switch state
intprevSwitchState=0;// the previous switch state
intled=2;// a variable to refer to the LEDs
// 600000 = 10 minutes in milliseconds
longinterval=600000;// interval at which to light the next LED
変数を定義しています。
setup()
32
33
34
35
36
37
38
39
40
voidsetup(){// set the LED pins as outputs
for(intx=2;x<8;x++){pinMode(x,OUTPUT);}// set the tilt switch pin as input
pinMode(switchPin,INPUT);}
voidloop(){// store the time since the Arduino started running in a variable
unsignedlongcurrentTime=millis();// compare the current time to the previous time an LED turned on
// if it is greater than your interval, run the if statement
if(currentTime-previousTime>interval){// save the current time as the last time you changed an LED
previousTime=currentTime;// Turn the LED on
digitalWrite(led,HIGH);// increment the led variable
// in 10 minutes the next LED will light up
led++;if(led==7){// the hour is up
}}// read the switch value
switchState=digitalRead(switchPin);// if the switch has changed
if(switchState!=prevSwitchState){// turn all the LEDs low
for(intx=2;x<8;x++){digitalWrite(x,LOW);}// reset the LED variable to the first one
led=2;//reset the timer
previousTime=currentTime;}// set the previous switch state to the current state
prevSwitchState=switchState;}