Arduino示例代码讲解:Project 08 - Digital Hourglass 数字沙漏
- Project 08 - Digital Hourglass 数字沙漏
-
- 程序功能概述
-
-
- 功能:
- 硬件要求:
- 输出:
-
- 代码结构
-
-
- 全局变量
- `setup()` 函数
- `loop()` 函数
- 计时和点亮LED:
- 读取倾斜开关状态:
- 重置LED和计时器:
-
- 运行过程
- 注意事项
Project 08 - Digital Hourglass 数字沙漏
/*
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 September 2012
by Scott Fitzgerald
http://arduino.cc/starterKit
This example code is part of the public domain
*/
// named constant for the switch pin
const int switchPin = 8;
unsigned long previousTime = 0; // store the last time an LED was updated
int switchState = 0; // the current switch state
int prevSwitchState = 0; // the previous switch state
int led = 2; // a variable to refer to the LEDs
// 600000 = 10 minutes in milliseconds
long interval = 600000; // interval at which to light the next LED
void setup() {
// set the LED pins as outputs
for (int x = 2; x < 8; x++) {
pinMode(x, OUTPUT);
}
// set the tilt switch pin as input
pinMode(switchPin, INPUT);