Created
April 11, 2018 09:34
-
-
Save glyons/7589d9157386fbc1c5f689b51397bc01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Display | |
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility | |
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) | |
#include <SPI.h> // For SPI comm (needed for not getting compile error) | |
#include <Wire.h> // For I2C comm, but needed for not getting compile error | |
// Servo | |
#include <Servo.h> | |
Servo myservo; // create servo object to control a servo | |
// Pin definitions | |
#define OLED_RESET 16 // Pin 15 -RESET digital signal | |
// Display demo definitions | |
#define NUMFLAKES 10 | |
#define XPOS 0 | |
#define YPOS 1 | |
#define DELTAY 2 | |
#define LOGO16_GLCD_HEIGHT 16 | |
#define LOGO16_GLCD_WIDTH 16 | |
ESP_SSD1306 display(OLED_RESET); // FOR I2C | |
// Timer | |
// https://playground.arduino.cc/Main/CountDownTimer | |
unsigned long Watch, _micro, time = micros(); | |
unsigned int Clock = 0, R_clock; | |
boolean Reset = false, Stop = false, Paused = false; | |
volatile boolean timeFlag = false; | |
boolean rotated=false, directionR=false; | |
int interval =10; // rotate every X seconds | |
int duration =660; // duration of development in seconds | |
void setup() | |
{ | |
// Start Serial | |
Serial.begin(115200); | |
// SSD1306 Init | |
display.begin(SSD1306_SWITCHCAPVCC); // Switch OLED | |
// Show image buffer on the display hardware. | |
// Since the buffer is intialized with an Adafruit splashscreen | |
// internally, this will display the splashscreen. | |
display.display(); | |
// Clear the buffer. | |
display.clearDisplay(); | |
// text display tests | |
display.setTextSize(2); | |
display.setTextColor(WHITE); | |
display.setCursor(0,0); | |
display.println("Spinmatic V1.0"); | |
display.display(); | |
myservo.attach(D8); // attaches the servo on D8 to the servo object | |
tone(D4, 2000, 400); | |
delay(600); | |
tone(D4, 800, 400); | |
delay(600); | |
tone(D4, 2000, 400); | |
// Timer | |
SetTimer(duration); | |
StartTimer(); | |
} | |
void loop() | |
{ | |
CountDownTimer(); // run the timer | |
int seconds = ShowSeconds(); | |
int minutes = ShowMinutes(); | |
// this prevents the time from being constantly shown. | |
if (!Stop) | |
{ | |
if (TimeHasChanged()) | |
{ | |
Serial.print(ShowHours()); | |
Serial.print(":"); | |
Serial.print(minutes); | |
Serial.print(":"); | |
Serial.print(seconds); | |
Serial.print(":"); | |
Serial.print(ShowMilliSeconds()); | |
Serial.print(":"); | |
Serial.println(ShowMicroSeconds()); | |
// Display the Countdown Time | |
display.clearDisplay(); | |
if (minutes==0) | |
{ | |
display.setCursor(30,10); | |
display.setTextSize(4); | |
display.print(ShowSeconds()); | |
display.println("s"); | |
} | |
else | |
{ | |
display.setCursor(10,20); | |
display.setTextSize(3); | |
display.print(ShowMinutes()); | |
display.print("m "); | |
display.print(ShowSeconds()); | |
display.println("s"); | |
} | |
display.setTextSize(1); | |
if (directionR) | |
{ | |
display.setCursor(90,50); | |
display.println("-->"); | |
} | |
else | |
{ | |
display.setCursor(10,50); | |
display.println("<--"); | |
} | |
display.display(); | |
} | |
if ( (seconds % interval == 0)) | |
{ | |
if (!rotated) | |
{ | |
if (directionR) | |
{ | |
Serial.println("Rotate Left"); | |
myservo.write(180); | |
directionR=false; | |
} | |
else | |
{ | |
Serial.println("Rotate Right"); | |
fwd(); | |
directionR=true; | |
} | |
} | |
rotated=true; | |
} | |
else | |
{ | |
rotated = false; | |
} | |
if (minutes==0 && seconds==01) | |
{ | |
tone(D4,3000, 200); | |
} | |
} | |
else | |
{ | |
display.clearDisplay(); | |
display.setCursor(10,20); | |
display.setTextSize(2); | |
display.println("Finished!"); | |
display.display(); | |
tone(D4, 4200, 100); | |
delay(500); | |
} | |
} | |
void fwd() | |
{ | |
int pos; | |
for(pos = 90; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees | |
{ | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(5); // waits 15ms for the servo to reach the position | |
} | |
} | |
void res() | |
{ | |
int pos; | |
for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees | |
{ // in steps of 1 degree | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(5); // waits 15ms for the servo to reach the position | |
} | |
} | |
boolean CountDownTimer() | |
{ | |
static unsigned long duration = 1000000; // 1 second | |
timeFlag = false; | |
if (!Stop && !Paused) // if not Stopped or Paused, run timer | |
{ | |
// check the time difference and see if 1 second has elapsed | |
if ((_micro = micros()) - time > duration ) | |
{ | |
Clock--; | |
timeFlag = true; | |
if (Clock == 0) // check to see if the clock is 0 | |
Stop = true; // If so, stop the timer | |
// check to see if micros() has rolled over, if not, | |
// then increment "time" by duration | |
_micro < time ? time = _micro : time += duration; | |
} | |
} | |
return !Stop; // return the state of the timer | |
} | |
void ResetTimer() | |
{ | |
SetTimer(R_clock); | |
Stop = false; | |
} | |
void StartTimer() | |
{ | |
Watch = micros(); // get the initial microseconds at the start of the timer | |
time = micros(); // hwd added so timer will reset if stopped and then started | |
Stop = false; | |
Paused = false; | |
} | |
void StopTimer() | |
{ | |
Stop = true; | |
} | |
void StopTimerAt(unsigned int hours, unsigned int minutes, unsigned int seconds) | |
{ | |
if (TimeCheck(hours, minutes, seconds) ) | |
Stop = true; | |
} | |
void PauseTimer() | |
{ | |
Paused = true; | |
} | |
void ResumeTimer() // You can resume the timer if you ever stop it. | |
{ | |
Paused = false; | |
} | |
void SetTimer(unsigned int seconds) | |
{ | |
// StartTimer(seconds / 3600, (seconds / 3600) / 60, seconds % 60); | |
Clock = seconds; | |
R_clock = Clock; | |
Stop = false; | |
} | |
int ShowHours() | |
{ | |
return Clock / 3600; | |
} | |
int ShowMinutes() | |
{ | |
return (Clock / 60) % 60; | |
} | |
int ShowSeconds() | |
{ | |
return Clock % 60; | |
} | |
unsigned long ShowMilliSeconds() | |
{ | |
return (_micro - Watch)/ 1000.0; | |
} | |
unsigned long ShowMicroSeconds() | |
{ | |
return _micro - Watch; | |
} | |
boolean TimeHasChanged() | |
{ | |
return timeFlag; | |
} | |
// output true if timer equals requested time | |
boolean TimeCheck(unsigned int hours, unsigned int minutes, unsigned int seconds) | |
{ | |
return (hours == ShowHours() && minutes == ShowMinutes() && seconds == ShowSeconds()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment