/* Dumbleduino V1.0 */ /* Author: Gene Lu 01/24/2012 */ /* USER SETTINGS */ int timeInterval0 = 5000; int timeInterval1 = 10000; int timeInterval2 = 15000; int timeInterval3 = 20000; int fadeSpeed = 25; // Time between each time the light gets brighter. /* Edit items below at your own risk. :) */ const int photoIn = 0; const int buttonPin = 2; const int LEDpin = 9; // The order here may vary based on how the LEDs are wired. // For me... const int LEDpin12 = 12; // Represents the default state. 1st LED from the left. const int LEDpin11 = 11; // Represents the 2nd LED from the left. Note: my LED isn't working for this one. LED connections are probably touching. const int LEDpin10 = 10; // The 3rd LED from the left. const int LEDpin13 = 13; // Rightmost LED. int timeCounter = 0; // Detects how many times the button has been pressed. int aiValue = 0; // The input value of the phototransistor. int buttonState = 0; int brightness = 0; // How bright the main LED is. int fadeAmount = 5; // How many points to fade the main LED by. int timeOn = timeInterval0; // How long the LED stays on for. int timeRunningCounter = 0; // Tracks how long the main LED has been on for. int lightTolerance = 115; // Anything below this value (phototransistor input) will trigger the main LED to turn on. void setup(){ Serial.begin(9600); pinMode(LEDpin, OUTPUT); pinMode(LEDpin10, OUTPUT); pinMode(LEDpin11, OUTPUT); pinMode(LEDpin12, OUTPUT); pinMode(LEDpin13, OUTPUT); pinMode(buttonPin, INPUT); } void loop(){ aiValue = analogRead(photoIn); // Read in value via the phototransistor. buttonState = digitalRead(buttonPin); // Detects whether or not the button has been pushed. Serial.println(aiValue); /*-- TIME INTERVAL DETECT --*/ /* The following IF statements detects which time interval is selected via the button on the back of the doll. If the button is pressed, the code increments to the next state via timeCounter variable and sets the time that the LED stays on for. This value is stored in the variable timeOn. */ // This is the default state and first state within the line of 4 LEDs if(buttonState == HIGH && timeCounter == 0){ digitalWrite(LEDpin12, HIGH); delay(500); // Gives time for people to see that the system has registered the button being pushed. digitalWrite(LEDpin12, LOW); timeCounter++; delay(500); buttonState = LOW; timeOn = timeInterval0; // 11 seconds timeRunningCounter = 0; // If the button has been pushed, reset the time on how long the main LED has been on for. } if(buttonState == HIGH && timeCounter == 1){ // This is the state of 10 seconds digitalWrite(LEDpin11, HIGH); delay(500); digitalWrite(LEDpin11, LOW); timeCounter++; delay(500); buttonState = LOW; timeOn = timeInterval1; timeRunningCounter = 0; } if(buttonState == HIGH && timeCounter == 2){ // This is the state of 10 seconds digitalWrite(LEDpin10, HIGH); delay(500); digitalWrite(LEDpin10, LOW); timeCounter=0; delay(500); buttonState = LOW; timeOn = timeInterval2; timeRunningCounter = 0; } if(buttonState == HIGH && timeCounter == 3){ digitalWrite(LEDpin13, HIGH); delay(500); digitalWrite(LEDpin13, LOW); timeCounter=0; delay(500); buttonState = LOW; timeOn = timeInterval3; timeRunningCounter = 0; } /*-- LIGHT DETECT --*/ // If there is no light in the room and the time that the LED has been on is below the set time, turn the main LED on. if((aiValue <= lightTolerance) && (timeRunningCounter < timeOn)){ timeRunningCounter++; // Increase brightness of the LED. if(brightness < 255 && brightness >= 0){ brightness = brightness + fadeAmount; analogWrite(LEDpin, brightness); delay(fadeSpeed); // slows down the fading of the light } } // If there is no light in the room and we have exceeded our set timer, turn the main LED off. else if(aiValue <= lightTolerance && timeRunningCounter >= timeOn){ // decrease brightness of the LED if(brightness > 0 && brightness <=255){ brightness = brightness - fadeAmount; analogWrite(LEDpin, brightness); delay(fadeSpeed); } // If there is light in the room, turn the main LED off. } else if(aiValue > lightTolerance){ if(brightness > 0 && brightness <=255){ brightness = brightness - fadeAmount; analogWrite(LEDpin, brightness); delay(fadeSpeed); } timeRunningCounter = 0; } else { timeRunningCounter=0; } // Not sure if this is needed. }