Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PCELL1_PIN A1
- #define LED_PIN 9
- #define POT_PIN A0
- #define DELAY 525
- double lightValue, photocellValue;
- bool currState = false, prevState = false, changed = false;
- bool dark(){
- return photocellValue < lightValue;
- }
- void setup() {
- Serial.begin(9600);
- analogWrite(LED_PIN, 0);
- }
- void loop() {
- photocellValue = analogRead(PCELL1_PIN);
- lightValue = analogRead(POT_PIN);
- Serial.print("Photocell: "); Serial.println(photocellValue);
- Serial.print("Potentiomater: "); Serial.println(lightValue);
- fadeTrigger();
- checkPhotoCell();
- }
- void fade(int value){
- if (value == 1){
- for (int i = 0; i < 255; i++) {
- analogWrite(LED_PIN, i+1);
- delay(DELAY/255);
- }
- } else {
- for (int i = 255; i >= 0; i--) {
- analogWrite(LED_PIN, i);
- delay(DELAY/255);
- }
- }
- }
- void checkPhotoCell(){
- if (dark()) currState = true;
- if (!dark()) currState = false;
- if (currState != prevState) changed = true;
- }
- void fadeTrigger(){
- if (changed){
- changed = false;
- if (currState){
- fade(1);//fade in
- } else {
- fade(0);//fade out
- }
- prevState = currState;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment