-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDtoggler.pde
More file actions
112 lines (101 loc) · 2.54 KB
/
LEDtoggler.pde
File metadata and controls
112 lines (101 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* LEDtoggler
* ==========
* Waits for control values to be sent via the serial connection and toggles LEDs on and off.
*
* After loading LEDtoggler to arduino, start the serial console and start to type the commands [0-4].
*
* The number of outputs can be trivially varied to suit your needs.
*
* Command Responce
* 0 = All LED's off
* 1 = Toggle LED1 on/off
* 2 = Toggle LED2 on/off
* 3 = Toggle LED3 on/off
* 4 = Toggle LED4 on/off
* 9 = All LED's on
*
*
* Pin 5 --- 4,7K --- LED1 --- +5Volt
* Pin 6 --- 4,7K --- LED2 --- +5Volt
* Pin 7 --- 4,7K --- LED3 --- +5Volt
* Pin 8 --- 4,7K --- LED4 --- +5Volt
*
*
*
* Version:
* 2010-02-09 mk Added "9" all LED's on.
*
*/
// Use pins 5 through 8 as the digital outputs
int pins[] = { 5, 6, 7, 8 };
int num_pins = 4;
/**
* Initial configuration
*/
void setup()
{
Serial.begin(38400); // Open the serial connection to listen for commands from the host
int i;
for (i = 0; i < num_pins; i++) // The array elements are numbered from 0 to num_pins - 1
pinMode(pins[i], OUTPUT); // Set each pin as an output
digitalWrite(pins[i], LOW);
}
/**
* Main program loop
*/
void loop () {
int light;
int i;
if(Serial.available()) {
light = Serial.read();
switch (light) {
case '0': // Turn all off
Serial.println("All dark");
for (i = 0; i < num_pins; i++) // The array elements are numbered from 0 to num_pins - 1
digitalWrite(pins[i], HIGH);
break;
case '1': // LED 1
toggleLed1 ();
break;
case '2': // LED 2
toggleLed2 ();
break;
case '3': // LED 3
toggleLed3 ();
break;
case '4': // LED 4
toggleLed4 ();
break;
case '9': // Turn all on
Serial.println("All light");
for (i = 0; i < num_pins; i++) // The array elements are numbered from 0 to num_pins - 1
digitalWrite(pins[i], LOW);
break;
}
}
}
void toggleLed1 () {
int state;
state = digitalRead (pins[0]);
digitalWrite (pins[0], !state);
Serial.println("LED 1");
}
void toggleLed2 () {
int state;
state = digitalRead (pins[1]);
digitalWrite (pins[1], !state);
Serial.println("LED 2");
}
void toggleLed3 () {
int state;
state = digitalRead (pins[2]);
digitalWrite (pins[2], !state);
Serial.println("LED 3");
}
void toggleLed4 () {
int state;
state = digitalRead (pins[3]);
digitalWrite (pins[3], !state);
Serial.println("LED 4");
}