Skip to content

Instantly share code, notes, and snippets.

@remy
Created April 24, 2014 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remy/11264660 to your computer and use it in GitHub Desktop.
Save remy/11264660 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// If using software SPI (the default case):
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
int lightPin = 0; //define a pin for Photo resistor
int buttonPin = 7;
boolean lightOn = false;
RTC_DS1307 RTC;
void setup() {
Serial.begin(9600);
// enable internal pull-up
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC);
// init done
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Ready.");
display.display();
delay(1000);
display.clearDisplay();
display.display();
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void reset() {
lightOn = false;
display.println("Resetting...");
display.display();
delay(1000);
display.clearDisplay();
display.setCursor(0,0);
display.println("Done");
display.display();
delay(1000);
display.clearDisplay();
display.display();
}
void timenow() {
DateTime now = RTC.now();
display.clearDisplay();
display.setCursor(0,0);
display.print(now.hour(), DEC);
display.print(':');
display.print(now.minute(), DEC);
display.print(':');
display.print(now.second(), DEC);
display.println();
display.display();
}
void loop() {
// light level 850 for light is on, less than that, is ambient light
int value = analogRead(lightPin);
int resetFlag = digitalRead(buttonPin); // read input value
if (resetFlag == LOW) { // check if the input is HIGH (button released)
reset();
}
if (lightOn == false && value > 850) {
lightOn = true;
timenow();
} else if (lightOn && value < 700) {
reset();
}
delay(10); //short delay for faster response to light.
}
@asyncanup
Copy link

This is awesome. I instantly feel like porting it to Johnny Five :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment