//Variables: //Random generators: long outLys = 0; long outLyset = 0; long outLysto = 0; long hundredP = 0; //Random generators for choosing output light pin! long lightOne = 0; long lightOneAfter = 0; //Random amount of lightnings long multiLight = 0; //Potpin reader: int valLys = 0; //Earlier pootpin readings: int valbefore = 0; //Updated potpin readings: (maybe unnecessary :) could just use the variable valLys) int valafter = 0; //Program chooser: int pgm = 1; //Reading button presses: int inpPgm = 0; //Last button press int sidsteInpPgm = 0; //Color savers: int valRed = 0; int valGre = 0; int valBlu = 0; //step in color per 1 potpin change. Don't change! 51 steps of 5 per interval => 0 to 255 int clrtstp = 5; //Used instead of delay. If delay is used button pressed wont be registered. unsigned long lastMillis = 0; //Counter of lightnings, used in pgm 3: int lightCounter = 0; //change of the delays between lightnings or partylight! float lightModDly = 1; //Preset variables: //Delay between lightnings (pgm == 3), in ms const long Lightdly = 200; //Delay between lights in party mode (pgm = 4), in ms const long partydly = 150; //Delay between a random amount of lightnings const long multiLightDly = 1000; boolean ifInProgress = false; boolean newHundredP = false; boolean newRanMultiLights = false; int k = 0; // Pins: int button = 13; int potPin = 1; int redPin = 11; int bluPin = 10; int grePin = 9; int lilEtPin = 2; int lilToPin = 3; int lilTrePin = 4; int lilFirePin = 5; int lilFemPin = 6; int storSeksPin = 7; // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output. pinMode(button, INPUT); pinMode(redPin, OUTPUT); pinMode(grePin, OUTPUT); pinMode(bluPin, OUTPUT); pinMode(lilEtPin, OUTPUT); pinMode(lilToPin, OUTPUT); pinMode(lilTrePin, OUTPUT); pinMode(lilFirePin, OUTPUT); pinMode(lilFemPin, OUTPUT); pinMode(storSeksPin, OUTPUT); randomSeed(analogRead(A0)); //Needs to be here to create valbefore valLys = analogRead(potPin); valbefore = valLys; } // the loop function runs over and over again forever void loop() { inpPgm = digitalRead(button); //Tjekker at det er et nyt tryk på knappen if (inpPgm != sidsteInpPgm) { //Ved tryk på knappen if (inpPgm==LOW) { pgm++; //3 forskellige programmer: 1 farver, 2 lysstyrke, 3 lyn, 4 random?. if (pgm==5) { pgm=1; } } //Serial.println(pgm); //Variables used in pauses. Makes the pgm 3 and 4 run from the beginning again, instead of in the middle of the code. lightModDly = 1; newHundredP = false; ifInProgress = false; newRanMultiLights = false; hundredP = 0; lightCounter = 0; k = 0; valLys = map(analogRead(potPin), 0, 1023, 0, 255); valbefore = valLys; delay(50); lastMillis = millis(); } sidsteInpPgm = inpPgm; //1. farver: Tændt lys, potPin angiver forskellige farver. if (pgm == 1) { digitalWrite(lilEtPin,HIGH); digitalWrite(lilToPin,HIGH); digitalWrite(lilTrePin,HIGH); digitalWrite(lilFirePin,HIGH); digitalWrite(lilFemPin,HIGH); digitalWrite(storSeksPin,HIGH); valLys = analogRead(potPin); valafter = valLys; //White: if (valLys == 1023) { analogWrite(bluPin, 255); analogWrite(redPin, 255); analogWrite(grePin, 255); valBlu = 255; valRed = 255; valGre = 255; } //From white to Blue: if (valLys >= 971 && valLys <= 1022) { analogWrite(bluPin, 255); analogWrite(redPin, 255 - (clrtstp * (1022 - valLys))); analogWrite(grePin, 255 - (clrtstp * (1022 - valLys))); valBlu = 255; valRed = 255 - (clrtstp * (1022 - valLys)); valGre = 255 - (clrtstp * (1022 - valLys)); } //Blue with more Red: if (valLys >= 919 && valLys <= 970) { analogWrite(bluPin, 255); analogWrite(redPin, 0 + (clrtstp * (970 - valLys))); analogWrite(grePin, 0); valBlu = 255; valRed = 0 + (clrtstp * (970 - valLys)); valGre = 0; } //Red with more Green and less Blue: if (valLys >= 867 && valLys <= 918) { analogWrite(bluPin, 255 - (clrtstp * (918 - valLys))); analogWrite(redPin, 255); analogWrite(grePin, 0 + (clrtstp * (918 - valLys))); valBlu = 255 - (clrtstp * (918 - valLys)); valRed = 255; valGre = 0 + (clrtstp * (918 - valLys)); } //Red with less Green: if (valLys >= 815 && valLys <= 866) { analogWrite(bluPin, 0); analogWrite(redPin, 255); analogWrite(grePin, 255 - (clrtstp * (866 - valLys))); valBlu = 0; valRed = 255; valGre = 255 - (clrtstp * (866 - valLys)); } //Red with more Blue: if (valLys >= 763 && valLys <= 814) { analogWrite(bluPin, 0 + (clrtstp * (814 - valLys))); analogWrite(redPin, 255); analogWrite(grePin, 0); valBlu = 0 + (clrtstp * (814 - valLys)); valRed = 255; valGre = 0; } //Blue with less Red and more Green: if (valLys >= 711 && valLys <= 762) { analogWrite(bluPin, 255); analogWrite(redPin, 255 - (clrtstp * (762 - valLys))); analogWrite(grePin, 0 + (clrtstp * (762 - valLys))); valBlu = 255; valRed = 255 - (clrtstp * (762 - valLys)); valGre = 0 + (clrtstp * (762 - valLys)); } //Blue with less Green if (valLys >= 659 && valLys <= 710) { analogWrite(bluPin, 255); analogWrite(redPin, 0); analogWrite(grePin, 255 - (clrtstp * (710 - valLys))); valBlu = 255; valRed = 0; valGre = 255 - (clrtstp * (710 - valLys)); } //Blue to Green: if (valLys >= 607 && valLys <= 658) { analogWrite(bluPin, 255 - (clrtstp * (658 - valLys))); analogWrite(redPin, 0); analogWrite(grePin, 0 + (clrtstp * (658 - valLys))); valBlu = 255 - (clrtstp * (658 - valLys)); valRed = 0; valGre = 0 + (clrtstp * (658 - valLys)); } //Green with more Red: if (valLys >= 555 && valLys <= 606) { analogWrite(bluPin, 0); analogWrite(redPin, 0 + (clrtstp * (606 - valLys))); analogWrite(grePin, 255); valBlu = 0; valRed = 0 + (clrtstp * (606 - valLys)); valGre = 255; } //Green with more Blue and less Red: if (valLys >= 503 && valLys <= 554) { analogWrite(bluPin, 0 + (clrtstp * (554 - valLys))); analogWrite(redPin, 255 - (clrtstp * (554 - valLys))); analogWrite(grePin, 255); valBlu = 0 + (clrtstp * (554 - valLys)); valRed = 255 - (clrtstp * (554 - valLys)); valGre = 255; } //Green with less Blue: if (valLys >= 451 && valLys <= 502) { analogWrite(bluPin, 255 - (clrtstp * (502 - valLys))); analogWrite(redPin, 0); analogWrite(grePin, 255); valBlu = 255 - (clrtstp * (502 - valLys)); valRed = 0; valGre = 255; } //Green to Blue: if (valLys >= 399 && valLys <= 450) { analogWrite(bluPin, 0 + (clrtstp * (450 - valLys))); analogWrite(redPin, 0); analogWrite(grePin, 255 - (clrtstp * (450 - valLys))); valBlu = 0 + (clrtstp * (450 - valLys)); valRed = 0; valGre = 255 - (clrtstp * (450 - valLys)); } //Blue to Red: if (valLys >= 347 && valLys <= 398) { analogWrite(bluPin, 255 - (clrtstp * (398 - valLys))); analogWrite(redPin, 0 + (clrtstp * (398 - valLys))); analogWrite(grePin, 0); valBlu = 255 - (clrtstp * (398 - valLys)); valRed = 0 + (clrtstp * (398 - valLys)); valGre = 0; } //Red to Green: if (valLys >= 295 && valLys <= 346) { analogWrite(bluPin, 0); analogWrite(redPin, 255 - (clrtstp * (346 - valLys))); analogWrite(grePin, 0 + (clrtstp * (346 - valLys))); valBlu = 0; valRed = 255 - (clrtstp * (346 - valLys)); valGre = 0 + (clrtstp * (346 - valLys)); } //Green to Red: if (valLys >= 243 && valLys <= 294) { analogWrite(bluPin, 0); analogWrite(redPin, 0 + (clrtstp * (294 - valLys))); analogWrite(grePin, 255 - (clrtstp * (294 - valLys))); valBlu = 0; valRed = 0 + (clrtstp * (294 - valLys)); valGre = 255 - (clrtstp * (294 - valLys)); } //Red with more green and more blue if (valLys >= 191 && valLys <= 242) { analogWrite(bluPin, 0 + (clrtstp * (242 - valLys))); analogWrite(redPin, 255); analogWrite(grePin, 0 + (clrtstp * (242 - valLys))); valBlu = 0 + (clrtstp * (242 - valLys)); valRed = 255; valGre = 0 + (clrtstp * (242 - valLys)); } //Red and Blue with less Green if (valLys >= 139 && valLys <= 190) { analogWrite(bluPin, 255); analogWrite(redPin, 255); analogWrite(grePin, 255 - (clrtstp * (190 - valLys))); valBlu = 255; valRed = 255; valGre = 255 - (clrtstp * (190 - valLys)); } //Different colors check http://www.rapidtables.com/web/color/RGB_Color.htm //for color info. //Different colors: (Pleasing colors! Bewitched tree :)) if (valLys >= 127 && valLys <= 138) { analogWrite(bluPin, 204); analogWrite(redPin, 255); analogWrite(grePin, 255); valBlu = 204; valRed = 255; valGre = 255; } //Different colors: (Purple) if (valLys >= 113 && valLys <= 126) { analogWrite(bluPin, 255); analogWrite(redPin, 153); analogWrite(grePin, 51); valBlu = 255; valRed = 153; valGre = 51; } //Different colors: (Orange) if (valLys >= 99 && valLys <= 112) { analogWrite(bluPin, 0); analogWrite(redPin, 255); analogWrite(grePin, 128); valBlu = 0; valRed = 255; valGre = 128; } //Different colors: (Rosa) if (valLys >= 85 && valLys <= 98) { analogWrite(bluPin, 127); analogWrite(redPin, 255); analogWrite(grePin, 0); valBlu = 127; valRed = 255; valGre = 0; } //Different colors: (Brown) if (valLys >= 71 && valLys <= 84) { analogWrite(bluPin, 51); analogWrite(redPin, 102); analogWrite(grePin, 51); valBlu = 51; valRed = 102; valGre = 51; } //Different colors: (Grey) if (valLys >= 57 && valLys <= 70) { analogWrite(bluPin, 160); analogWrite(redPin, 160); analogWrite(grePin, 160); valBlu = 160; valRed = 160; valGre = 160; } //Different colors: (Pink) if (valLys >= 43 && valLys <= 56) { analogWrite(bluPin, 185); analogWrite(redPin, 255); analogWrite(grePin, 0); valBlu = 185; valRed = 255; valGre = 0; } //Different colors: (Yellow) if (valLys >= 29 && valLys <= 42) { analogWrite(bluPin, 0); analogWrite(redPin, 255); analogWrite(grePin, 255); valBlu = 0; valRed = 255; valGre = 255; } //Different colors: (Green) if (valLys >= 15 && valLys <= 28) { analogWrite(bluPin, 0); analogWrite(redPin, 0); analogWrite(grePin, 255); valBlu = 0; valRed = 0; valGre = 255; } //Different colors: (Red) if (valLys >= 1 && valLys <= 14) { analogWrite(bluPin, 0); analogWrite(redPin, 255); analogWrite(grePin, 0); valBlu = 0; valRed = 255; valGre = 0; } //Different colors: (Blue) if (valLys == 0) { analogWrite(bluPin, 255); analogWrite(redPin, 0); analogWrite(grePin, 0); valBlu = 255; valRed = 0; valGre = 0; } //Needs to be here to determine if the potpin is turned when changing the brightness in pgm == 2 valLys = map(analogRead(potPin), 0, 1023, 0, 255); valbefore = valLys; } //2. Lysstyrke: if (pgm == 2) { valLys = map(analogRead(potPin), 0, 1023, 0, 255); valafter = valLys; // Nedenstående er til smooth transitions via potpin: //brug "valafter+2" hvis der skal være smooth overgang if ((valafter+2) < valbefore) { analogWrite(bluPin, valBlu * valLys/255); analogWrite(redPin, valRed * valLys/255); analogWrite(grePin, valGre * valLys/255); valbefore = valafter; } //brug "valafter-2" hvis der skal være smooth overgang if ((valafter-2) > valbefore) { analogWrite(bluPin, valBlu * valLys/255); analogWrite(redPin, valRed * valLys/255); analogWrite(grePin, valGre * valLys/255); valbefore = valafter; } } //3. Lyn: if (pgm == 3) { //Sluk alt lys if (hundredP==0) { digitalWrite(lilEtPin,LOW); digitalWrite(lilToPin,LOW); digitalWrite(lilTrePin,LOW); digitalWrite(lilFirePin,LOW); digitalWrite(lilFemPin,LOW); digitalWrite(storSeksPin,LOW); } if (newRanMultiLights==false) { //Last one is omitted by the code underneath and Arduino also emits one number (random(4,9-1)), //hence random(4,9) delivers 4 to 7 iderations of the code beneath! multiLight = random(4,9); newRanMultiLights=true; } if (ifInProgress == false && newHundredP==false) { //hundredP = occurence of lightnings. lightOne=random small LED choosen. hundredP = random(1,101); lightOne = random(2,7); newHundredP=true; } //Lightning with all the lights! (1 %) if (hundredP == 1 && newHundredP == true && multiLight != lightCounter) { ifInProgress = true; if (k==0) { digitalWrite(lilEtPin,HIGH); digitalWrite(lilToPin,HIGH); digitalWrite(lilTrePin,HIGH); digitalWrite(lilFirePin,HIGH); digitalWrite(lilFemPin,HIGH); digitalWrite(storSeksPin,HIGH); normPause(); } if (k==1) { digitalWrite(lilEtPin,LOW); digitalWrite(lilToPin,LOW); digitalWrite(lilTrePin,LOW); digitalWrite(lilFirePin,LOW); digitalWrite(lilFemPin,LOW); digitalWrite(storSeksPin,LOW); normPause(); } if (k==2) { ifInProgress = false; k=0; } } //Lightning with just the big line of LEDs (2 %) else if (hundredP >= 2 && hundredP <= 3 && newHundredP == true && multiLight != lightCounter) { ifInProgress = true; if (k==0) { digitalWrite(storSeksPin,HIGH); normPause(); } if (k==1) { normPause(); digitalWrite(storSeksPin,LOW); } if (k==2) { ifInProgress = false; k=0; } } //Three lightnings in a row (4 %) else if (hundredP >=4 && hundredP <= 7 && newHundredP == true && multiLight != lightCounter) { ifInProgress = true; if (k==0) { normPause(); digitalWrite(lightOne,HIGH); } if (k==1) { smallPause(); digitalWrite(lightOne,LOW); } if (k==2) { digitalWrite(lightOne,HIGH); normPause(); } if (k==3) { smallPause(); digitalWrite(lightOne,LOW); } if (k==4) { digitalWrite(lightOne,HIGH); normPause(); } if (k==5) { normPause(); digitalWrite(lightOne,LOW); } if (k==6) { ifInProgress = false; k=0; } } //Two lightnings in a row (10 %) else if (hundredP >= 8 && hundredP <= 17 && newHundredP == true && multiLight != lightCounter) { ifInProgress = true; if (k==0) { digitalWrite(lightOne,HIGH); normPause(); } if (k==1) { smallPause(); digitalWrite(lightOne,LOW); } if (k==2) { digitalWrite(lightOne,HIGH); normPause(); } if (k==3) { normPause(); digitalWrite(lightOne,LOW); } if (k==4) { ifInProgress = false; k=0; } } //One lightning (73 %) else if (hundredP >= 18 && newHundredP == true && multiLight != lightCounter) { ifInProgress = true; if (k==0) { digitalWrite(lightOne,HIGH); normPause(); } if (k==1) { normPause(); digitalWrite(lightOne,LOW); } if (k==2) { ifInProgress = false; k=0; } } else { } if (newHundredP == true && ifInProgress == false && multiLight == lightCounter) { if (k==0) { longPause(); } if (k==1) { lightCounter=0; newHundredP = false; newRanMultiLights = false; k=0; } } else if (newHundredP == true && ifInProgress == false && multiLight != lightCounter) { lightCounter++; newHundredP = false; k=0; } else { } valLys = map(analogRead(potPin), 0, 1023, 0, 255); valafter = valLys; // Nedenstående er til smooth transitions via potpin: //brug "valafter+2" hvis der skal være smooth overgang if ((valafter+2) < valbefore) { if (valafter<=85) { lightModDly = 1.5; valbefore = valafter; } else if (valafter>85 && valafter<=170) { lightModDly = 1; valbefore = valafter; } else if (valafter>170 && valafter<=255) { lightModDly = 0.5; valbefore = valafter; } } //brug "valafter-2" hvis der skal være smooth overgang if ((valafter-2) > valbefore) { if (valafter<=85) { lightModDly = 1.5; valbefore = valafter; } else if (valafter>85 && valafter<=170) { lightModDly = 1; valbefore = valafter; } else if (valafter>170 && valafter<=255) { lightModDly = 0.5; valbefore = valafter; } } } //4. Party if (pgm == 4) { if (ifInProgress==false) { lightOne = random(2,8); //Random color selection outLys = random(256); analogWrite(bluPin, outLys); // Serial.println(outLys); outLyset = random(256); analogWrite(redPin, outLyset); // Serial.println(outLyset); outLysto = random(256); analogWrite(grePin, outLysto); // Serial.println(outLysto); if (outLys<50 && outLyset<50 && outLysto<50) { if(outLys>outLyset && outLys>outLysto) { analogWrite(bluPin, outLys*3); } else if (outLysto>outLyset && outLysto>outLys) { analogWrite(grePin, outLysto*3); } else { analogWrite(redPin, outLyset*3); } } ifInProgress=true; k=0; } if (k==0) { digitalWrite(lightOne,HIGH); k++; } if (k==1) { if ((unsigned long)millis() - lastMillis >= partydly * lightModDly) { lastMillis = millis(); k++; } } if (k==2) { digitalWrite(lightOne,LOW); lightOneAfter = lightOne; k++; } if (k==3) { if ((unsigned long)millis() - lastMillis >= partydly * lightModDly) { lastMillis = millis(); ifInProgress=false; } } valLys = map(analogRead(potPin), 0, 1023, 0, 255); valafter = valLys; // Nedenstående er til smooth transitions via potpin: //brug "valafter+2" hvis der skal være smooth overgang if ((valafter+2) < valbefore) { if (valafter<=85) { lightModDly = 1.5; valbefore = valafter; } else if (valafter>85 && valafter<=170) { lightModDly = 1; valbefore = valafter; } else if (valafter>170 && valafter<=255) { lightModDly = 0.5; valbefore = valafter; } } //brug "valafter-2" hvis der skal være smooth overgang if ((valafter-2) > valbefore) { if (valafter<=85) { lightModDly = 1.5; valbefore = valafter; } else if (valafter>85 && valafter<=170) { lightModDly = 1; valbefore = valafter; } else if (valafter>170 && valafter<=255) { lightModDly = 0.5; valbefore = valafter; } } } } void longPause() { if ((unsigned long)millis() - lastMillis >= multiLightDly * lightModDly) { lastMillis = millis(); k++; } } void normPause() { if ((unsigned long)millis() - lastMillis >= Lightdly * lightModDly) { lastMillis = millis(); k++; } } void smallPause() { if ((unsigned long)millis() - lastMillis >= Lightdly/3 * lightModDly) { lastMillis = millis(); k++; } }