View difference between Paste ID: 6vYirUte and pL55zKXj
SHOW: | | - or go back to the newest paste.
1
/*************************************************************
2
3
Written by Titafubaki
4
You are Free to use, share and change this code like you want.
5
But do not sell it or parts of it and do not tell others you have
6
written it.
7
8
To compile this code You will need the additional Librarys:
9
- Adafruit_NeoPixel ( Search in the Library Manager or get it from GitHub: https://github.com/adafruit/Adafruit_NeoPixel )
10
- Blynk ( Search in the Library Manager or get it from there page: https://www.blynk.cc/ )
11
- BlynkESP8266_Lib ( Search in the Library Manager or get it from there page: https://www.blynk.cc/ )
12
13
Board: NodeMCU 1.0 (ESP-12 Module) -> Install ESP8266 via Board Manager
14
Upload Speed: 115200
15
16
For the Blynk App you can use this QR Code to Copy my Project: http://blynk-cloud.com/1d10593575434dd0acf4a2e92ee224fb/qr
17
18
If you want to use Google To Controll it you will need IFTTT
19
Each function and Color is one Applet. THis will take some time to configure but will be worth it. Trust me :D
20
First of all. Open an cmd and type in "blynk-cloud.com" it will return you an IP address.
21
You should use this address in the URL not the DNS name. For some reason this is not working
22
23
Create a new Applet in IFTTT and select Google Asistant (Should also work with Alexa but I dont own one)
24
Choose a trigger like "Say a simple phrase"
25
Than choos as Action "Webhooks" -> Make a web request
26
Here you tipe in the URL, Method is PUT, Content Type: "application/json" and then the Boddy.
27
Here is a List of some Commands for Google.
28
29
Turn On the Kristal:
30
  URL: http://IPToBlynk/AuthToken/update/V0
31
  Method: PUT
32
  Content Type: application/json
33
  Body: [ "1" ]
34
35
Turn Off the Kristal:
36
  URL: http://IPToBlynk/AuthToken/update/V0
37
  Method: PUT
38
  Content Type: application/json
39
  Body: [ "0" ]
40
41
For the colors you can use a color Picker from a Website and Change the Body it is in the Formate RGB: [ "R", "G", "B" ]
42
Change Color to Blue
43
  URL: http://IPToBlynk/AuthToken/update/V3
44
  Method: PUT
45
  Content Type: application/json
46
  Body: [ "0", "0", "255" ]
47
48
The Body contains the intex of the Mode (1-6)
49
Change Mode to ColorFade:
50
  URL: http://IPToBlynk/AuthToken/update/V4
51
  Method: PUT
52
  Content Type: application/json
53
  Body: ["5"]
54
 *************************************************************/
55
56
/* Comment this out to disable prints and save space */
57
#define BLYNK_PRINT Serial
58
59
60
#include <ESP8266WiFi.h>
61
#include <BlynkSimpleEsp8266.h>
62
#include <Adafruit_NeoPixel.h>
63
64
// You should get Auth Token in the Blynk App.
65
// Go to the Project Settings (nut icon).
66
char auth[] = "e75a9243961a4e848fb0f5827c3ceb1e";
67
68
// Your WiFi credentials.
69
// Set password to "" for open networks.
70
char ssid[] = "Anhalter42";
71
char pass[] = "Kauf dir Internet du Schnorrer!";
72
73
//GPIO used on the Board
74
#define PIN 14
75
//Count of LEDs
76
#define PIXELNUMBER 30
77
//The minimal brightness in Breath mode
78
#define BREATHMIN 30
79
//You should consider to change this Value depending on your power supply. (0 - 255)
80
#define OVERALLBRIGHTNESS 255
81
const int numReadings = 20;
82
83
//init of all needed Objects
84
BlynkTimer timer;
85
int prevMod = 0;
86
int selectedMod = 0;
87
int selectedColorR = 254;
88
int selectedColorG = 254;
89
int selectedColorB = 254;
90
bool step = 0;
91
int breathPercentage = 100;
92
int colorWheelCount = 255 / PIXELNUMBER;
93
int rainbowcolor = 0;
94
int speed = 50;
95
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELNUMBER, PIN, NEO_GRB + NEO_KHZ800);
96
int ch1;
97
int readings[numReadings];      // the readings from the analog input
98
int readIndex = 0;              // the index of the current reading
99
int total = 0;                  // the running total
100
int average = 1;                // the average
101
102
//This Function is used to set the Color when you are in the Color Mode
103
void changeColor (){
104
  if(selectedMod == 3 || selectedMod == 0){
105
    for (int i = 0; i < strip.numPixels(); i++)
106
    {
107
      strip.setPixelColor(i, selectedColorR,selectedColorG,selectedColorB);
108
    }
109
    strip.show();
110
  }
111
}
112
113
//This Function will run once after the setup so you can see that the Crystal is ready. (It will fade the brightness to 100% and back to 0)
114
void setupCompleted(){
115
  for (int i=0; i<100; i++){
116
    int breathR = (selectedColorR / 100) * i;
117
      int breathG = (selectedColorG / 100) * i;
118
      int breathB = (selectedColorB / 100) * i;
119
120
      for (int i2 = 0; i2 < strip.numPixels(); i2++){
121
          strip.setPixelColor(i2, breathR,breathG,breathB);
122
        }
123
      strip.show();
124
      delay(5);
125
  }
126
  
127
  for (int i=100; i>0; i--){
128
    int breathR = (selectedColorR / 100) * i;
129
      int breathG = (selectedColorG / 100) * i;
130
      int breathB = (selectedColorB / 100) * i;
131
132
      for (int i2 = 0; i2 < strip.numPixels(); i2++){
133
          strip.setPixelColor(i2, breathR,breathG,breathB);
134
        }
135
      strip.show();
136
      delay(5);
137
  }
138
  
139
  for (int i = 0; i < strip.numPixels(); i++)
140
  {
141
    strip.setPixelColor(i, 0);
142
  }
143
  strip.show();
144
  selectedMod = 6;
145
}
146
147
//This is the Wheel Funktion from Neopixel. I did not write it. It will give you a Color when you give it a value from 0 - 255
148
uint32_t Wheel(byte WheelPos) {
149
  WheelPos = 255 - WheelPos;
150
  if(WheelPos < 85) {
151
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
152
  } else if(WheelPos < 170) {
153
    WheelPos -= 85;
154
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
155
  } else {
156
   WheelPos -= 170;
157
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
158
  }
159
}
160
161
//This is one step in the Breath mode. It is as function so I am able to do more then one step in my timer without copy the hole code again and again :D
162
void stepBreath(){
163
  if (step){
164
          if (breathPercentage >= 100){          
165
            step = 0;
166
            breathPercentage = 100;
167
          }else{breathPercentage += +1;}
168
        }
169
        else
170
        {
171
          if (breathPercentage <= BREATHMIN){
172
            step = 1;
173
            breathPercentage = BREATHMIN;
174
          }else{breathPercentage += -1;}
175
        }
176
        
177
      int breathR = (selectedColorR / 100) * breathPercentage;
178
      int breathG = (selectedColorG / 100) * breathPercentage;
179
      int breathB = (selectedColorB / 100) * breathPercentage;
180
181
      for (int i = 0; i < strip.numPixels(); i++){
182
          strip.setPixelColor(i, breathR,breathG,breathB);
183
        }
184
      
185
      strip.show();
186
}
187
188
//This is one step in the RainbowWheel mode. It is as function so I am able to do more then one step in my timer without copy the hole code again and again :D
189
void stepColorWheel(){
190
  if (rainbowcolor > 255){
191
        rainbowcolor = 0;
192
      }      
193
      for (int i=0; i<strip.numPixels(); i++) {
194
        int myrainbowcolor = rainbowcolor + (colorWheelCount*i);
195
        while (myrainbowcolor > 255){
196
          myrainbowcolor+= -255;
197
        }  
198
        strip.setPixelColor(i, Wheel(myrainbowcolor));
199
      }
200
      strip.show();
201
      rainbowcolor--;
202
}
203
204
//This is one step in the ColorFade mode. It is as function so I am able to do more then one step in my timer without copy the hole code again and again :D
205
void stepColorFade (){
206
  if (rainbowcolor > 255){
207
        rainbowcolor = 0;
208
      }      
209
      for (int i=0; i<strip.numPixels(); i++) {
210
        strip.setPixelColor(i, Wheel(rainbowcolor));
211
      }
212
      strip.show();
213
      rainbowcolor++;
214
}
215
216
//This is the Main function the timer will work with. Each Case represent one selected mode
217
void playModus () {
218
  ch1 = pulseIn(12, HIGH, 25000);
219
  
220
  int speedHz = ch1;
221
  if (speedHz > 1989){
222
    speedHz = 1990;
223
  }
224
  if (speedHz < 1490){
225
    speedHz = 1490;
226
  }
227
  speed = 200 - (((1991 - speedHz) * 2)/5);
228
  if (speed > 199){
229
    speed = 199;
230
  }
231
  if (speed < 20){
232
    speed = 20;
233
  }
234
235
  // subtract the last reading:
236
  total = total - readings[readIndex];
237
  // read from the sensor:
238
  readings[readIndex] = speed;
239
  // add the reading to the total:
240
  total = total + readings[readIndex];
241
  // advance to the next position in the array:
242
  readIndex = readIndex + 1;
243
244
  // if we're at the end of the array...
245
  if (readIndex >= numReadings) {
246
    // ...wrap around to the beginning:
247
    readIndex = 0;
248
  }
249
250
  // calculate the average:
251
  average = total / numReadings;
252
  speed = average;
253
  if (speed > 199){
254
    speed = 199;
255
  }
256
  if (speed < 1){
257
    speed = 1;
258
  }
259
  
260
  Serial.print("Channel 1:"); // Print the value of 
261
  Serial.println(speed);
262
  
263
 switch (selectedMod){
264
    case 1: {    
265
      for (int i = 0; i < strip.numPixels(); i++)
266
      {
267
        if (step){
268
          strip.setPixelColor(i, selectedColorR,selectedColorG,selectedColorB);
269
          step = 0;
270
        }
271
        else
272
        {
273
          strip.setPixelColor(i, 0);
274
          step = 1;
275
        }
276
      }
277
      strip.show();
278
      if (step){step = 0;}else{step = 1;}
279
      
280
      break;
281
    }
282
    case 2: {
283
      stepBreath();
284
      delay(50);
285
      stepBreath();
286
      delay(50);
287
      stepBreath();
288
      delay(50);
289
      stepBreath();
290
      break;
291
    }
292
    case 3: {
293
294
      break;
295
    }
296
    case 4: {
297
      for (int i = 0; i < strip.numPixels(); i++){
298
        strip.setPixelColor(i, random(254),random(254),random(254));
299
      }
300
      strip.show();
301
      break;
302
    }
303
    case 5: {
304
      stepColorFade();
305
      delay(100);
306
      stepColorFade();
307
      break;
308
    }
309
    case 6: {
310
      int mydelay = (200/speed);
311
      for (int i = 0; i < speed -1; i++){
312
        stepColorWheel();
313
        delay(mydelay);
314
      }
315
      break;
316
    }
317
  }
318
}
319
320
//Here I get the selected mode from Blynk and store it in a variable
321
BLYNK_WRITE(V4)
322
{
323
  switch (param.asInt()){
324
    case 1: {
325
      selectedMod = 1;
326
      break;
327
    }
328
    case 2: {
329
      selectedMod = 2;
330
      break;
331
    }
332
    case 3: {
333
      selectedMod = 3;
334
      changeColor();
335
      break;
336
    }
337
    case 4: {
338
      selectedMod = 4;
339
      break;
340
    }
341
    case 5: {
342
      selectedMod = 5;
343
      break;
344
    }
345
    case 6: {
346
      selectedMod = 6;
347
      break;
348
    }
349
  }
350
}
351
352
//Here I get the Color from Blynk
353
BLYNK_WRITE(V3)
354
{
355
  selectedColorR = param[0].asInt();
356
  selectedColorG = param[1].asInt();
357
  selectedColorB = param[2].asInt();
358
  changeColor();
359
}
360
361
//This is the Function to turn the lamp on and off
362
BLYNK_WRITE(V0)
363
{
364
  if(param.asInt() == 0){
365
  for (int i = 0; i < strip.numPixels(); i++)
366
  {
367
    strip.setPixelColor(i, 0);
368
  }
369
  if (!selectedMod==0){
370
  prevMod = selectedMod;
371
  }
372
  selectedMod = 0;
373
  strip.show();
374
  }
375
  else {
376
    if (prevMod==0){
377
      prevMod = 3;
378
    }
379
    selectedMod = prevMod;
380
    changeColor();
381
  }
382
}
383
384
//Speed
385
BLYNK_WRITE(V1)
386
{
387
  speed = param.asInt();
388
}
389
390
void setup()
391
{
392
  pinMode(12, INPUT);
393
  // Debug console
394
  Serial.begin(9600);
395
  randomSeed(analogRead(0));
396
  
397
  Blynk.begin(auth, ssid, pass);
398
  // You can also specify server:
399
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
400
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
401
402
  //Turns off all LEDs at the beginn
403
  strip.begin();
404
  strip.setBrightness(OVERALLBRIGHTNESS);
405
  strip.show();
406
407
  //Init of all Variables used in this program
408
  timer.setInterval(200L, playModus);
409
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
410
    readings[thisReading] = 1;
411
  }
412
  setupCompleted();
413
  
414
}
415
416
void loop()
417
{
418
  Blynk.run();
419
  timer.run();
420
  
421
  
422
}