Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int ledPin = LED_BUILTIN;// the number of the LED pin
- int redl,greenl,bluel;
- int redPin = 11;
- int greenPin = 10;
- int bluePin = 9;
- const int buttonPin = 8;
- int buttonState = 0;
- const long interval = 30;
- const long longlight = 60000;
- const long clicktime = 15000;
- const long colortime = 5000;
- int fade = 0; // how bright the LED is
- int fadeAmount = 5;
- int clickcount = 0;
- unsigned long previousMillis = 0;
- unsigned long longMillis = 0;
- unsigned long clickMillis = 0;
- unsigned long colorMillis = 0;
- boolean animate = false;
- boolean light = false;
- //#define COMMON_ANODE
- void setup()
- {
- pinMode(ledPin, OUTPUT);
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- pinMode(buttonPin, INPUT_PULLUP);
- digitalWrite(ledPin, LOW);
- redl = 255; greenl = 0; bluel = 0;//set red default
- }
- void loop()
- {
- // setColor(255, 0, 0); // red
- // setColor(0, 255, 0); // green
- // setColor(0, 0, 255); // blue
- // setColor(255, 255, 0); // yellow
- // setColor(80, 0, 80); // purple
- // setColor(0, 255, 255); // aqua
- buttonState = digitalRead(buttonPin); //odczytanie guzika
- unsigned long currentMillis = millis();
- if(animate ){
- if (currentMillis - longMillis <= longlight){
- if (currentMillis - previousMillis >= interval) { // animate fade
- previousMillis = currentMillis;
- fade = fade + fadeAmount;
- if (fade == 0 || fade == 255) {
- fadeAmount = -fadeAmount ;
- }
- setColorfade(redl, greenl,bluel,fade);
- } //fade animation
- }else{animate = false;setColor(0,0,0);}}//do animate
- if(light ){
- while (digitalRead(buttonPin) == HIGH){
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) { // animate fade
- previousMillis = currentMillis;
- fade = fade + fadeAmount;
- if (fade == 0 || fade == 255) {
- fadeAmount = -fadeAmount ;
- }
- setColorfade(redl, greenl,bluel,fade);
- } //fade animation
- }
- clickcount = 0;
- light = false;setColor(0,0,0);
- while (digitalRead(buttonPin) == LOW){}
- delay(1000);
- }//do light
- if (buttonState == LOW) {
- colorMillis =currentMillis ;
- while (digitalRead(buttonPin) == LOW){
- // TU NAPISZ FUNKCJĘ ZMIANY KOLORÓW
- unsigned long currentMillis = millis();
- // setColor(255, 0, 0); // red
- // setColor(0, 255, 0); // green
- // setColor(0, 0, 255); // blue
- // setColor(255, 255, 0); // yellow
- // setColor(80, 0, 80); // purple
- // setColor(0, 255, 255); // aqua
- //if (currentMillis - colorMillis >= colortime ) {
- int sec =0;
- int var = currentMillis - colorMillis;
- sec = sec + 5000;
- if(var >sec && var < sec+5000){redl = 255; greenl = 0; bluel = 0; setColor(redl, greenl,bluel);}//SET red
- sec = sec + 5000;
- if(var >sec && var < sec+5000){redl = 0; greenl = 255; bluel = 0; setColor(redl, greenl,bluel);}//SET green
- sec = sec + 5000;
- if(var >sec && var < sec+5000){redl = 0; greenl = 0; bluel = 255; setColor(redl, greenl,bluel);}//SET blue
- sec = sec + 5000;
- if(var >sec && var < sec+5000){redl = 255; greenl = 255; bluel = 0; setColor(redl, greenl,bluel);}//SET yellow
- sec = sec + 5000;
- if(var >sec && var < sec+5000){redl = 80; greenl = 0; bluel = 80; setColor(redl, greenl,bluel);}//SET purple
- sec = sec + 5000;
- if(var >sec && var < sec+5000){redl = 0; greenl = 255; bluel = 255; setColor(redl, greenl,bluel);}//SET aqua
- sec = sec + 5000;
- if(var >sec && var < sec+5000){colorMillis =currentMillis-5000 ;}//start over
- //}
- }//loop button
- setColor(0, 0,0);
- colorMillis =0;
- clickcount = clickcount + 1;
- clickMillis = currentMillis;
- } //button on
- if (clickcount == 1 && !animate){animate = true;longMillis = currentMillis;}
- if (clickcount == 2 ){light = true;animate = false;}
- if (currentMillis - clickMillis <= clicktime ){
- //clickMillis = currentMillis;
- }else{clickcount = 0;digitalWrite(ledPin, HIGH);}
- } //loop
- void setColor(int red, int green, int blue)
- {
- #ifdef COMMON_ANODE
- red = 255 - red;
- green = 255 - green;
- blue = 255 - blue;
- #endif
- analogWrite(redPin, red);
- analogWrite(greenPin, green);
- analogWrite(bluePin, blue);
- }
- void setColorfade(int red, int green, int blue,int fade)
- {
- #ifdef COMMON_ANODE
- red = 255 - red;
- green = 255 - green;
- blue = 255 - blue;
- #endif
- analogWrite(redPin, (red > 0 ) ? (fade>red)?0:red-fade : red);
- analogWrite(greenPin, (green > 0) ? (fade>green)?0:green-fade : green);
- analogWrite(bluePin, (blue>0)?(fade>blue )?0:blue-fade:blue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement