/
KitchenOven.ino
200 lines (158 loc) · 4.74 KB
/
KitchenOven.ino
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************
Sketch for my Ikea Kids Kitchen oven mod.
By Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Twitter: https://twitter.com/witnessmenow
*******************************************************************/
#include <Adafruit_NeoPixel.h>
// Library for controlling the neopixel ring
// Search for "Neopixel" in the Arduino Library manager
// https://github.com/adafruit/Adafruit_NeoPixel
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
// Library for using the Rotary Encoder
// Search for "encoder" in the Arduino Library manager and select the one
// by Paul Stoffregen
// https://github.com/PaulStoffregen/Encoder
#define COLOUR_WHITE 0
#define COLOUR_RED 1
#define COLOUR_BLUE 2
#define COLOUR_GREEN 3
// --------
// Buttons
// --------
// One leg of the buttons connected to Ground
// The other connected to the following
const int blueButton = D4;
const int greenButton = D7;
const int whiteButton = D5;
const int redButton = D6;
#define NUM_BUTTONS 4
// --------
// Neopixels
// --------
const int ledPin = D8;
#define NUMPIXELS 32
// --------
// Rotary Encoder
// --------
const int reButtonPin = D3;
const int reDTPin = D2;
const int reCLKPin = D1;
#define DEFAULT_BRIGHTNESS 128;
bool lightsAreOn = false;
uint32_t colour = 0;
int brightness = DEFAULT_BRIGHTNESS;
int currentColourCode = 0;
unsigned long reButtonCoolDown = 1000;
unsigned long reButtonReadAllowedTime = 0;
unsigned long ovenTimeout = 0;
long oldPosition = -999;
int buttons[NUM_BUTTONS] = {blueButton, greenButton, whiteButton, redButton};
int buttonsCodes[NUM_BUTTONS] = {COLOUR_BLUE, COLOUR_GREEN, COLOUR_WHITE, COLOUR_RED};
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
Encoder myEnc(reDTPin, reCLKPin);
void setup() {
Serial.begin(115200);
colour = getColour(COLOUR_RED);
// initialize the pushbutton pin as an input:
pinMode(blueButton, INPUT_PULLUP);
pinMode(greenButton, INPUT_PULLUP);
pinMode(whiteButton, INPUT_PULLUP);
pinMode(redButton, INPUT_PULLUP);
pinMode(reButtonPin, INPUT_PULLUP);
// Get current Position
oldPosition = myEnc.read();
pixels.begin();
turnOffAllPixels();
setAllPixels(getColour(0));
}
void setAllPixels(uint32_t targetColour) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, targetColour);
}
pixels.show();
}
void turnOffAllPixels(){
setAllPixels(pixels.Color(0, 0, 0));
}
uint32_t getColour(int colourCode) {
uint32_t tempColour = 0;
switch (colourCode) {
case COLOUR_WHITE:
tempColour = pixels.Color(brightness, brightness, brightness);
break;
case COLOUR_RED:
tempColour = pixels.Color(brightness, 0, 0);
break;
case COLOUR_BLUE:
tempColour = pixels.Color(0, 0, brightness);
break;
case COLOUR_GREEN:
tempColour = pixels.Color(0, brightness, 0);
break;
}
return tempColour;
}
bool getBrightnessChange() {
bool brightnessChanged = false;
int amountBirghtnessChange = 5;
long newPosition = myEnc.read();
if (newPosition > oldPosition) {
brightnessChanged = true;
brightness += amountBirghtnessChange;
if (brightness > 255)
{
brightness = 255;
}
} else if (newPosition < oldPosition) {
brightnessChanged = true;
brightness -= amountBirghtnessChange;
if (brightness < 10)
{
brightness = 10;
}
}
if(newPosition != oldPosition){
Serial.print("Brigthness: ");
Serial.println(brightness);
}
oldPosition = newPosition;
return brightnessChanged;
}
void loop() {
//uint32_t tempColour = 0;
bool newColour = false;
for (int i = 0; i < NUM_BUTTONS; i++) {
if (digitalRead(buttons[i]) == LOW) {
currentColourCode = buttonsCodes[i];
//colour = getColour(buttonsCodes[i]);
newColour = true;
Serial.print("Button Pressed: ");
Serial.println(i);
}
}
bool newBrightness = getBrightnessChange();
if (reButtonReadAllowedTime < millis()) {
if (digitalRead(reButtonPin) == LOW) {
reButtonReadAllowedTime = millis() + reButtonCoolDown;
lightsAreOn = !lightsAreOn;
if (lightsAreOn) {
setAllPixels(colour);
} else {
turnOffAllPixels();
}
}
}
if (newColour || newBrightness) {
ovenTimeout = millis() + 300000;
colour = getColour(currentColourCode);
lightsAreOn = true;
setAllPixels(colour);
}
if (millis() == ovenTimeout && lightsAreOn) {
lightsAreOn = false;
turnOffAllPixels();
}
}