/* Red channel is single led operational channel. Red channel is psuedo-PWM. Modes: 0 - constant light dependent on ambient light. Bright == cyan, dark == orange (red with some green), 1 minute fade slewrate. 1 - flicker like fire all channels/red for single LED 2 - RGB random color random timer 3 - RGB the 7 colors random, random timer */ //Pinout int bluepin = 0; // The PWM pin the LED is attached to int greenpin = 1; // The PWM pin the LED is attached to int redpin = 2; // The PWM pin the LED is attached to int button = 4; // Mode button press detection int LDR = 3; // LDR reading for light sensing //variables: int mode=0; int light=0; int lastlight=0; int red; int darkest; int brightest; int baseline=3; int baselight; int curlight; int truelight; // Pseudo-PWM: red channel int ledState = LOW; // ledState used to set the red LED // Generally, you should use "unsigned long" for variables that hold time // The value will quickly become too large for an int to store unsigned long previousMicros = 0; // will store last time LED was updated long interval = 100; // interval at which to blink (microseconds) int brightness = 1; // How bright the LED is int fadeAmount = 1; // How many points to fade the LED by unsigned long previousMillis = 0; // will store last time LED was updated unsigned long intervalMillis = 25; // interval at which to blink (milliseconds) void setup() { // declare pinouts pinMode(redpin, OUTPUT); pinMode(bluepin, OUTPUT); pinMode(greenpin, OUTPUT); pinMode(button, INPUT); pinMode(LDR, INPUT); //prep map values darkest=analogRead(LDR); brightest=analogRead(LDR); lastlight=brightest; truelight=brightest; baselight=brightest; randomSeed(darkest); } void loop() { if (LOW == digitalRead(button)) { analogWrite(greenpin, 0); analogWrite(bluepin, 0); digitalWrite(redpin, LOW); delay(400); mode++; if (mode>5) {mode=0;} for (int i=1; i <= mode+1; i++){ digitalWrite(redpin, HIGH); delay(50); analogWrite(redpin, LOW); delay(200); } delay(1000); intervalMillis=1; if (mode==0) { //prep map values darkest=analogRead(LDR); brightest=analogRead(LDR); } } /*temp light=analogRead(LDR); if light analogWrite(bluepin, 0); analogWrite(greenpin, 0); digitalWrite(redpin, LOW); delayMicroseconds(1000); light=analogRead(LDR); if (light>brightest) {brightest=light;} if (light= intervalMillis) { //calculate last measurement if (baseline<2) { curlight=analogRead(LDR); } if (baseline==1) { baselight=curlight; } if (baseline>0) { baseline--; } if ((curlight > (baselight+9) || curlight < (baselight-9))&& (baseline < 1)) { // start new measurement of truelight analogWrite(bluepin, 0); analogWrite(greenpin, 0); digitalWrite(redpin, LOW); delay(6000); truelight=analogRead(LDR); if (truelight>brightest) {brightest=truelight;} if (truelight= intervalMillis) { red=255; analogWrite(greenpin, 0); previousMillis = currentMillis; intervalMillis=60000; } } if (mode==2) { //firesimulation unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= intervalMillis) { red=255; analogWrite(greenpin, 14); previousMillis = currentMillis; intervalMillis=60000; } } if (mode==3) { //firesimulation unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= intervalMillis) { // save the last time you blinked the LED brightness = random(120)+135; red=brightness; analogWrite(greenpin, (brightness/10)+ random(10)); previousMillis = currentMillis; intervalMillis=random(280)+20; } } if (mode==4) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= intervalMillis) { analogWrite(bluepin, random(255)); analogWrite(greenpin, random(255)); red=random(255); previousMillis = currentMillis; intervalMillis=random(1000); } } if (mode==5) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= intervalMillis) { analogWrite(bluepin, random(0,2)*255); analogWrite(greenpin, random(0,2)*255); red=random(random(0,2)*255); previousMillis = currentMillis; intervalMillis=random(1000); } } if (mode==99) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= intervalMillis) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } // set the brightness of pins: analogWrite(redpin, brightness); analogWrite(greenpin, brightness); // analogWrite(bluepin, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; //brightness = 1; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 250) { fadeAmount = -fadeAmount ; } } } //Pseudo-PWM red channel (yes i know there is another pwm pin on the attiny85, but i had already soldered the board together, lazyness ftw) unsigned long currentMicros = micros(); if (currentMicros - previousMicros >= interval) { // save the last time you blinked the LED previousMicros = currentMicros; // if the LED is off turn it on and vice-versa: if (ledState == LOW) { if (red>0) { ledState = HIGH; } interval=red*12; digitalWrite(redpin,ledState); } else { ledState = LOW; interval=3060-(red*12); digitalWrite(redpin,ledState); } } // wait for 30 milliseconds to see the dimming effect //delay(30); }