-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVibgyor+Debounce
65 lines (62 loc) · 1.57 KB
/
Vibgyor+Debounce
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
const int buttonPin = 2;
int i=1;
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)
{
lastDebounceTime = millis(); // Reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) // Check if state change has lasted for longer time than debounce delay
{
if (reading != buttonState)
{
buttonState = reading;
if (buttonState == HIGH) // Only shift if button is pressed
{
i++;
if (i==8)
{
i=1;
}
}
}
}
switch(i)
{
case 1:setColor(255, 0, 0); // red
break;
case 2:setColor(255, 100, 0); // orange
break;
case 3:setColor(255,255,0); // yellow
break;
case 4:setColor(0, 255, 0); // green
break;
case 5:setColor(0,0,255); // blue
break;
case 6:setColor(75,0,130); // indigo
break;
case 7:setColor(148,0,211); // violet
break;
}
lastButtonState = reading;
}
void setColor(int red, int green, int blue) // set RGB colour intensities
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}