Last active
June 12, 2016 23:26
-
-
Save terrag42/d13e3e4a2a438a4901885a7af5cd5e22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
YouTube Video Link: https://youtu.be/oCWGiHfL3NY | |
Schematic: https://goo.gl/photos/wKB94DZT4ECusWmcA | |
This mushroom Lights project is built using a TCRT5000 infrared LED/transistor pair. | |
For the infrared LED, a 100ohm resistor is connected in series with +5v and the LED. | |
A 4.7k resistor connected in series between +5v and the emitter of the transistor. | |
Analog pin A1 (pin 2 on digispark) is connected to the emitter of the transistor. | |
I'm assuming the project is switched off at least once every 50 days so the millis() does not overflow back to zero. | |
*/ | |
const uint8_t MUSHPIN [] = {0,1,4}; //PWM pins on the Digispark. Connect the infrared sensor to pin 2 (=A1) | |
int fade[]= {255,255,255}; //fade amount to use with analogWrite(). 255=off | |
bool switched = false; | |
bool flag = false; //mushrooms on or off? | |
void setup() { | |
// initialize serial communication at 9600 bits per second: | |
for (uint8_t i=0; i<3; i++){ | |
pinMode(MUSHPIN[i], OUTPUT); //PWM pins = 0,1,4 | |
digitalWrite(MUSHPIN[i], HIGH); //we'll put the mushrooms on the high side (inverse logic) | |
} | |
//A1 analog read pin (2 on digispark) does not need to be initialized. | |
delay(3000); | |
} | |
unsigned long onTime, offTime; | |
void fadetoblackby(int a, uint8_t b){ //a= fade amount per cycle, b= pin and fade assignment | |
fade[b]= min(fade[b] + a, 255); //do not increase the "fade" variable to more than 255 | |
analogWrite(MUSHPIN[b], fade[b]); | |
} | |
void fadetowhiteby(int a, uint8_t b){//a= fade amount per cycle, b= pin and fade assignment | |
fade[b]= max(fade[b] - a, 0); //do not increase the "fade" variable to less than 0 | |
analogWrite(MUSHPIN[b], fade[b]); | |
} | |
void fadeOn(){ | |
fadetowhiteby(8, 0); //0= first mushroom | |
if (fade[0] < 220) //checks to see if the first one is turning on to stagger the fade on of the mushrooms | |
fadetowhiteby(6, 1); | |
if (fade[0] < 185) | |
fadetowhiteby(4, 2); | |
} | |
void fadeOff(){ | |
fadetoblackby(5, 0); | |
if (fade[0] > 35 ) | |
fadetoblackby(3, 1); | |
if (fade[0] > 70 ) | |
fadetoblackby(2, 2); | |
} | |
uint8_t shroom=0; | |
void loop() { | |
// read the input on analog pin 0: | |
unsigned int sensorValue = analogRead(A1); //pin P2 on the digispark | |
switch (shroom){ | |
case 0: //Shrooms are off, fading on /off with infrared switch. detect 3 second dwell. | |
if (sensorValue < 850){ | |
onTime = millis(); | |
switched = true; | |
} | |
else { | |
offTime = millis(); | |
switched = false; | |
} | |
if (sensorValue < 850 && onTime-3000 > offTime) //detect 3 second dwell | |
shroom = 1; //enter case 1 if dwell time is triggered | |
break; | |
case 1: //Shrooms are latched on, send confirmation (only once) | |
switched = true; | |
if (!flag){ //flag is used to send confirmation flash only once. | |
flag = true; //set flag in this case | |
for (uint8_t i=0; i<3; i++){ //confirmation flash | |
digitalWrite(MUSHPIN[i], HIGH); //switch lights OFF for confirmation flash | |
} | |
delay(100); | |
for (uint8_t i=0; i<3; i++){ | |
digitalWrite(MUSHPIN[i], LOW); //switch lights ON for confirmation flash | |
} | |
} | |
/* if (sensorValue > 850) | |
offTime=millis(); | |
if (sensorValue < 850) | |
onTime = millis(); | |
if (sensorValue > 850 && onTime-1000 > offTime){ //one second delay for debouncing | |
*/ | |
if (sensorValue > 900){ | |
flag = false; | |
shroom = 2; | |
} | |
break; | |
case 2: //enter case 2 if shrooms are on and hand is away. Detect if user triggers infrared. | |
if (sensorValue < 850){ //detects switch-off | |
switched = false; | |
} | |
if (fade[2]>254) //detects when last shroom is off | |
shroom = 0; //go back to off state | |
break; | |
} | |
if (switched){ | |
fadeOn(); | |
} | |
else { | |
fadeOff(); | |
} | |
// Serial.println(sensorValue); // print out the value you read: (debugging) | |
delay(30); // delay in between reads for stability | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment