Attachment 'Light_Time_Switch.ino'
Download 1 /*
2 Author : Loe Feijs
3 Date : 20130302
4
5 This example shows how to control a LED (LED via 470 Ohm
6 between pin 9 and GND). The light goes on when the button
7 switch is pushed. Switch between GND and pin 2. The light
8 source goes off after 4 seconds (programmed as a finite
9 state machine).
10 */
11
12 //connect LED, Button:
13 #define ledPin 9
14 #define buttonPin 2
15
16 //FSM states:
17 #define OFF 0
18 #define ON 1
19
20 // initialisation
21 int state = OFF;
22 int timer = 0;
23
24 void setup() {
25 pinMode(ledPin, OUTPUT);
26 pinMode(buttonPin, INPUT_PULLUP);
27 }
28
29 void loop() {
30 LEDstep();
31 delay(10); // one step every 10 ms
32 }
33
34 /*
35 Finite state machine implementation
36 */
37 void LEDstep(){
38
39 switch (state){
40 case OFF:
41 if (digitalRead(buttonPin) == LOW){
42 // change state
43 state = ON;
44 // state transition actions
45 timer = 400; // 10 millisecond steps
46 analogWrite(ledPin,255); // max brightness
47 }
48 break;
49 case ON:
50 if (timer <= 0){
51 // change state
52 state = OFF;
53 // state transition actions
54 analogWrite(ledPin,0);
55 }
56 else timer--;
57 break;
58 }
59 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.