Last active
February 25, 2019 04:03
-
-
Save jlliu/00aa58a7ed77353e148a6a4c76849c44 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
int motorPin = 9; | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <stdio.h> | |
#include <elapsedMillis.h> | |
#include "Adafruit_MPRLS.h" | |
#include <Adafruit_CircuitPlayground.h> | |
elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 32 // OLED display height, in pixels | |
//// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
#define OLED_RESET 10 // Reset pin # (or -1 if sharing Arduino reset pin) | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
int iteration = 0; | |
int delayInterval = 5000; | |
const int phrasesLength = 4; | |
const int max_size = 60; | |
char *phrases[] = | |
{ "Morning! Why not walk a different way to work today?", | |
"Take a look at who's around. Who have you not talked to before?", | |
"Lunch time! What's something you've always wanted to try?", | |
"New haircut, new you, am I right?" | |
}; | |
char *intervalsText[] ={ | |
"5-SECOND ADVICE:", | |
"MINUTE ADVICE: ", | |
"HOURLY ADVICE: ", | |
}; | |
long intervalTimes[] = { | |
5000,60000,1000*3600L | |
}; | |
int currentIntervalIndex = 0; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("HELLO"); | |
pinMode(motorPin, OUTPUT); | |
// put your setup code here, to run once: | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); // Don't proceed, loop forever | |
} | |
// Show initial display buffer contents on the screen -- | |
// the library initializes this with an Adafruit splash screen. | |
display.display(); | |
// Clear the buffer | |
display.clearDisplay(); | |
// Draw a single pixel in white | |
display.drawPixel(10, 10, WHITE); | |
// Show the display buffer on the screen. You MUST call display() after | |
// drawing commands to make them visible on screen! | |
display.display(); | |
} | |
boolean leftButtonPushed = false; | |
boolean leftButtonLifted = false; | |
boolean rightButtonPushed = false; | |
boolean rightButtonLifted = false; | |
void loop() { | |
// Detect interrupts for button presses that change the delayTime | |
//Only write logic for one button press at a time | |
// If BOTH buttons are not pushed together | |
if (!(CircuitPlayground.leftButton() && CircuitPlayground.rightButton())){ | |
//If the left button is pressed at this time loop | |
if (CircuitPlayground.leftButton()){ | |
// Wait until it's released | |
while (CircuitPlayground.leftButton()){ | |
} | |
Serial.println("PRESS LEFT BUTTON"); | |
if (currentIntervalIndex > 0 ){ | |
currentIntervalIndex--; | |
delayInterval = intervalTimes[currentIntervalIndex]; | |
drawPhrase(phrases[iteration % phrasesLength]); | |
timeElapsed = 0; | |
} | |
} | |
//If the left button is pressed at this time loop | |
if (CircuitPlayground.rightButton()){ | |
// Wait until it's released | |
while (CircuitPlayground.rightButton()){ | |
} | |
Serial.println("PRESS RIGHT BUTTON"); | |
if (currentIntervalIndex < 2 ){ | |
currentIntervalIndex++; | |
delayInterval = intervalTimes[currentIntervalIndex]; | |
drawPhrase(phrases[iteration % phrasesLength]); | |
timeElapsed = 0; | |
} | |
} | |
} | |
if (timeElapsed > delayInterval) { | |
drawPhrase(phrases[iteration % phrasesLength]); | |
iteration++; | |
timeElapsed = 0; // reset the counter to 0 so the counting starts over... | |
analogWrite(motorPin,200); | |
delay(1000); | |
analogWrite(motorPin,0); | |
} | |
} | |
void drawPhrase(String phrase){ | |
display.clearDisplay(); | |
display.setCursor(0,0); | |
display.setTextSize(1); | |
display.setTextWrap(true); | |
display.setTextColor(WHITE); | |
display.cp437(true); // Use full 256 char 'Code Page 437' font | |
display.println(intervalsText[currentIntervalIndex]); | |
display.print(phrase); | |
display.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment