// CODE BIBLIOGRAPHY // The source for this code originally started from DIOT Lab: Sensors to Action int knobPin = A0; int knobReading = 0; int mappedKnobReading = 0; int ledPin = D2; int ledBrightness = 0; bool pulse_up = true; int ledPin2 = D3; int buttonState; int buttonPin = D6; //int last_button_press = -1; int photoCellPin = A5; int photoCellReading = -1; //first log should be program start time int last_photocell_log = millis(); int last_water_log = 0; //bool sendSMS = false; //milliseconds in a second int cycle_start_time = millis(); int seconds = 1000; int cycle_length = 10 * seconds; int led_state =0; bool button_pressed_during_cycle = false; void setup() { pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode( buttonPin , INPUT_PULLUP); } void loop() { //sendSMS = false; photoCellReading = analogRead(photoCellPin); //3556 bright //900 pitch black pulse_led(); knobReading = analogRead(knobPin); mappedKnobReading = map( knobReading, 0, 4095, 0, 100); analogWrite(ledPin, ledBrightness); analogWrite(ledPin2, ledBrightness); buttonState = digitalRead(buttonPin); //adjust the cycle time using the knob cycle_length = cycle_length * mappedKnobReading / 100; //cycle_length = cycle_length * fraction; //every x second cycle, trend the LED to it's weaker state /* PSUEDO CODE cycle starts during cycle if button pressed state = state + 1 button flag = true cycle ends was a button pressed during this cycle? (yes/no) yes - state = state no - state = state -1 reset button flag = false reset cycle_start_time = current_time */ if (buttonState == LOW) //if you push the button down { if(button_pressed_during_cycle == false) { if (led_state == 2) { led_state = 2; } else { led_state = led_state + 1; } button_pressed_during_cycle = true; } } if (cycle_start_time + cycle_length < millis() ) { //end of cycle if(button_pressed_during_cycle) { //led_state = led_state //only publish an event once every 60 seconds to not overload the IFTTT event handler if( last_water_log == 0 or (last_water_log + cycle_length < millis()) ) { //recent logtime to current time last_water_log = millis(); //publish photoreading to our spreadsheet Particle.publish( "log_water", "1" ); } } else { if (led_state == 0) { led_state = 0; } else { led_state = led_state - 1; } } cycle_start_time = millis(); button_pressed_during_cycle = false; } //publish all variables to Particle console for debugging Particle.variable("knobPin", knobReading); Particle.variable("mappedKnobPin", mappedKnobReading); Particle.variable("ledBrightness", ledBrightness); Particle.variable("button",buttonState); //Particle.variable("lastButtonPress",last_button_press ); Particle.variable("photoCell", photoCellReading); Particle.variable("last_log_water", last_water_log); Particle.variable("last_log_photo", last_photocell_log); Particle.variable("led_state", led_state); Particle.variable("cycle_length", cycle_length); //Particle.variable("fraction", fraction); delay(50); //every 2 minutes, publish the photo reading to our spreadsheet if( last_photocell_log + 120 * seconds < millis() ) { last_photocell_log = millis(); Particle.publish( "log_photocell", String( photoCellReading ) ); } } /////////////////////////////////////////////////////////////////////////////////////////////// //This function pulses the led in 3 distinct visual states void pulse_led() { //LED STATE 0 is the weakest state if (led_state == 0) { //fixed state ledBrightness = 20; } //LED STATE 1 else if (led_state == 1) { if (ledBrightness >= 255) { pulse_up = false; } else if (ledBrightness <= 0) { pulse_up = true; } if (pulse_up) { ledBrightness = ledBrightness + 5; } else { ledBrightness = ledBrightness - 5; } }//LED STATE 2 is the brightest state else if (led_state == 2) { if (ledBrightness >= 255) { pulse_up = false; } else if (ledBrightness <= 0) { pulse_up = true; } if (pulse_up) { ledBrightness = ledBrightness + 40; } else { ledBrightness = ledBrightness - 40; } } } //The code below was part of the debugging work in progress and is not used in the current codebase // CODE BIBLIOGRAPHY // The source for this function originally pulled from DIOT sensors to action /* bool button_delay() { if( last_button_press + cycle_length < millis() ) { last_button_press = millis(); return true; } else { return false; } } */ // state flow goes 0 -> 1 -> 2 -> 1 -> 0 /* void change_led_state_up() { if (led_state == 0) { led_state = 1; } else if (led_state ==1) { led_state = 2; } else if (led_state ==2) { led_state = 2; } } // state flow goes 0 -> 1 -> 2 -> 1 -> 0 void change_led_state_down() { if (led_state == 0) { last_led_state = 0; led_state = 0; } else if (led_state ==1) { if(last_led_state ==0) //if you just changed from 0 -> 1, let the cycle run at 1 once { last_led_state = 1; led_state = 1; } else { last_led_state = 1; led_state = 0; } } else if (led_state ==2) { last_led_state = 2; led_state = 1; } } */