-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocker_LED_Controller.ino
More file actions
124 lines (119 loc) · 2.26 KB
/
Locker_LED_Controller.ino
File metadata and controls
124 lines (119 loc) · 2.26 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
112
113
114
115
116
117
118
119
120
121
122
123
124
int redled = 11;
int greenled = 10;
int blueled = 9;
int pot1 = A1;
int cycleCount = 0;
int bPin = 2;
int currentBVal = 1;
void setup() {
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(blueled, OUTPUT);
pinMode(bPin, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
int bVal = getBVal(bPin, currentBVal, 4);
switch (bVal) {
case 1:
runLEDs(255,104,1);
break;
case 2:
potLEDControl(pot1);
break;
case 3:
spectrumCycle();
break;
case 4:
colors();
break;
default:
runLEDs(0,0,0);
break;
}
digitalWrite(13, LOW);
}
void runLEDs(int red, int green, int blue) {
analogWrite(redled, red);
analogWrite(greenled, green);
analogWrite(blueled, blue);
}
int getPot(int potPin) {
int potValue = analogRead(potPin);
return potValue;
}
void colors()
{
// Red
runLEDs(255,0,0);
// Green
runLEDs(0,255,0);
delay(1000);
// Blue
runLEDs(0,0,255);
delay(1000);
// Yellow
runLEDs(255,255,0);
delay(1000);
// Cyan
runLEDs(0,255,255);
delay(1000);
//Purple
runLEDs(255,0,255);
delay(1000);
// White
runLEDs(255,255,255);
delay(1000);
}
void spectrumValueToLED(int value) {
int red;
int green;
int blue;
if (value <= 255) {
red = 255 - value;
green = value;
blue = 0;
} else if (value >= 256 && value <= 511) {
red = 0;
green = 255 - (value - 256);
blue = value - 256;
} else {
red = value - 512;
green = 0;
blue = 255 - (value - 512);
}
runLEDs(red, green, blue);
}
void potLEDControl(int potPin) {
int potValue = getPot(potPin);
double numVal = potValue;
double specValue = ((numVal / 1023) * 767);
int spectrumValue = specValue;
spectrumValueToLED(spectrumValue);
}
void spectrumCycle() {
if (cycleCount < 768) {
spectrumValueToLED(cycleCount);
cycleCount++;
}
if (cycleCount == 768) {
cycleCount = 0;
}
delay(10);
}
int getBVal(int pin, int oldVal, int caseNumber) {
Serial.println("HI");
Serial.println(digitalRead(pin));
if (digitalRead(pin) == 0) {
int newVal = oldVal + 1;
currentBVal = newVal;
return newVal;
digitalWrite(13, HIGH);
} else {
return currentBVal;
}
if (currentBVal == caseNumber) {
currentBVal = 0;
}
}