/* * Schreibtischlampe * a project from Fablab-Cottbus e.v. * www.fablab-cottbus.de * * programm von Johan Levon * * * utility links: * recommanded Library for LEDs management * https://www.norwegiancreations.com/2018/01/programming-digital-rgb-led-strips-with-arduino-and-the-fastled-library/ * advice on switch management with digital pins * https://forum.arduino.cc/t/a-switch-between-two-digital-pins-is-it-safe/450887/3 * * * */ #include "FastLED.h" // that's the library we used to control the WS2812B Leds // How many leds in your strip? (here default 4) #define NUM_LEDS 4 #define DATA_PIN D3 // the pin were the data are sent //#define CLOCK_PIN 4 // here not necessesary but can be important for other LEDs stripes CRGB leds[NUM_LEDS]; // here we defined the leds type (RGB) and name it leds #supposition (has to be verified) but the step is important anyway // Here we are using a three position switch (ON1-OFF-ON2)there for we need two detection pins and one "Ground" pin, that we call control pin. #define Rswitch D6 // will be used as reading pin #define Lswitch D5 // will be used as reading pin #define Controlpin D0// will be always wird immer LOW Bleiben unsigned long timer = 0; // time variable unsigned long Fadetime = 100; // waiting time to change the color in ms byte Hue =0; // the starting color number used to control the rainbow effect void setup() { FastLED.addLeds(leds, NUM_LEDS); // here the leds type and number is added in the commnunication system pinMode(Rswitch,INPUT_PULLUP); pinMode(Lswitch,INPUT_PULLUP); pinMode(Controlpin,OUTPUT); digitalWrite(Controlpin, LOW); //stay LOW (the R- und Lswitch ends are default 5V (PULLUP) only wenn connected with Control Pin they switch to LOW timer=millis(); } void loop() { // if(digitalRead(Rswitch) == LOW){ leds[0]= CHSV(10,50,255); // in the CHSV funktion there is three number to choose: leds[1]= CHSV(75,50,255); // the first one called Hue defined the color [0-255] leds[2]= CHSV(150,50,255); // the second one is Saturation [0-255], 0 is 0% of the choosen color (that's white) 255 is 100% of choosen color. leds[3]= CHSV(225,50,255); // the third one is Value [0-255], it's corresponding to the intensity of the light. 0 correspind to black (no light) 255 corresponding to the maximal intensity) FastLED.show(); // here we use a white light lightly colored (on each on the leds) with maximal intensity } if(digitalRead(Rswitch) == HIGH and digitalRead(Lswitch) == HIGH){ leds[0]= CHSV(0,0,0); // here we switch the light off(third parameter : Value is set to 0) leds[1]= CHSV(0,0,0); leds[2]= CHSV(0,0,0); leds[3]= CHSV(0,0,0); FastLED.show(); } //Rainbow if(digitalRead(Lswitch) == LOW){ if(millis()>timer+Fadetime){ timer=millis(); Hue+=1; // Hue is a byte when it is at 255, and one is added, it goes to 0 leds[0]=CHSV(Hue,200,255); //Here we have 4 shades of the same color with the maximal light intensity leds[1]=CHSV(Hue+1,200,255); // the colors change every Fadetime (100ms. everysecond we gain 10 Hue (255/10=25,5) to do a full cycle 28 seconds are needed leds[2]=CHSV(Hue+2,200,255); leds[3]=CHSV(Hue+3,200,255); FastLED.show(); } } }