Skip to content

Instantly share code, notes, and snippets.

@jlconlin
Last active January 30, 2021 03:05
Show Gist options
  • Save jlconlin/fc521b5b4200e7ff1bfc4a1ef030a09d to your computer and use it in GitHub Desktop.
Save jlconlin/fc521b5b4200e7ff1bfc4a1ef030a09d to your computer and use it in GitHub Desktop.
Snooze-Swatter
#include <Servo.h>
#include <Wire.h>
#include <DS3231.h>
#include <Adafruit_GFX.h> // Include Adafruit graphics library
#include <Adafruit_SSD1306.h> // Include Adafruit SSD1306 OLED driver
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Servo servo;
Time t;
DS3231 rtc(SDA, SCL);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char temperature[6];
int pos = 0;
bool active = false;
const int buttonPin = 2; // the number of the pushbutton pin
void setupClock(){
// Setup Serial connection
Serial.begin(115200);
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}
// Initialize the rtc object
rtc.begin();
}
void setupDisplay(){
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
// Clear the display buffer.
display.clearDisplay();
display.display();
}
void setDate(){
// The following lines can be uncommented to set the date and time
rtc.setDOW(FRIDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(17, 32, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(29, 1, 2021); // Set the date to January 1st, 2014
}
void setup() {
setupClock();
setupDisplay();
// setDate();
servo.attach(9);
servo.write(pos);
pinMode(buttonPin, INPUT);
}
int activeHour = 18;
int activeMin = 35;
int alarmHour = 18;
int alarmMin = 38;
void loop() {
t = rtc.getTime();
//printTime();
displayWrite();
// Activate at midnight
if( ( t.hour == activeHour ) and ( t.min == activeMin ) and ( t.sec == 0 ) ){
// Make sure we don't activate on the weekends
if( t.dow < 6 ){
Serial.println("Activated by time.");
active = true;
}
}
// Turn off if button is pressed
bool buttonPressed = digitalRead( buttonPin );
if( buttonPressed ){
Serial.println("Deactivated by button.");
active = false;
}
if( active ){
// Swat if past wake-up time
if( ( t.hour >= alarmHour ) and ( t.min >= alarmMin ) ){
swat();
}
}
delay(1000);
}
void displayWrite(){
t = rtc.getTime();
display.setTextColor(WHITE, BLACK);
display.drawRect(117, 56, 3, 3, WHIT E); // Put degree symbol ( ° )
float temp = rtc.getTemp();
dtostrf( temp, 5, 2, temperature );
//sprintf( temperature, "%f", temp );
Serial.println( temperature );
draw_text(0, 56, "TEMPERATURE =", 1);
draw_text(122, 56, "C", 1);
draw_text( 80, 56, temperature, 1 );
draw_text( 4, 14, rtc.getDateStr(), 2 );
draw_text( 16, 35, rtc.getTimeStr(), 2 );
Serial.print( rtc.getDateStr() );
Serial.print( " " );
Serial.println( rtc.getTimeStr() );
}
void swat(){
int del = 5;
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(del); // wait for the servo to reach the position
}
for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(del); // wait for the servo to reach the position
}
}
void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
display.setCursor(x_pos, y_pos);
display.setTextSize(text_size);
display.print(text);
display.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment