#include "MD_MAX72xx.h"
#include "RTClib.h"
#define CLK_PIN 52
#define DATA_PIN 51
#define CS_PIN 53
boolean isPushed = false;
int loveButton = A2;
int foodButton = A1;
int sleepButton = A0;
int sleepTime = 21;
int wakeTime = 9;
int minSinceWake;
int nextNeedsCareTime = 0;
int nextNeedsRestTime = 0;
int nextNeedsFoodTime = 0;
const int needsCareInterval = 600;
const int needsRestInterval = 360;
const int needsFoodInterval = 270;
boolean screenImage[9][8][8] = {
{
//Screen: 0 - Idle Frame of Slime
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 1 - Slime looks left
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 1, 0, 0, 1},
{1, 1, 0, 0, 1, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 2 - Slime looks right
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 0, 0, 1, 1},
{1, 0, 0, 1, 0, 0, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 3 - Slime blinks while looking right
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 4 - Slime is sad - needs attention
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1},
{1, 1, 1, 0, 0, 1, 1, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 0,1, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 5 - Slime blinks while sad
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 0, 0, 1, 1, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 6 - Slime opening its mouth - is hungry
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 7 - Slime is tired - needs to be told to sleep
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
{
//Screen: 8 - Slime is drowsy
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0}
},
};
MD_MAX72XX ledMatrix = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, CS_PIN, 1);
RTC_DS1307 realClock;
void setup() {
ledMatrix.begin();
Serial.begin(9600);
if(realClock.begin()) {
Serial.println("Clock Started!");
} else {
Serial.println("Clock Malfunctioned!");
Serial.flush();
abort();
}
pinMode(loveButton, INPUT_PULLUP);
pinMode(foodButton, INPUT_PULLUP);
pinMode(sleepButton, INPUT_PULLUP);
randomSeed(analogRead(7));
}
void loop() {
DateTime lastTimeCheck = realClock.now();
minSinceWake = ((lastTimeCheck.hour() * 60) + lastTimeCheck.minute()) - (wakeTime * 60);
if((lastTimeCheck.hour() >= sleepTime) || (lastTimeCheck.hour() < wakeTime)) {
allGood();
minSinceWake = 0;
nextNeedsCareTime = 0;
nextNeedsRestTime = 0;
nextNeedsFoodTime = 0;
} else {
allGood();
if(nextNeedsCareTime == 0 ) {
nextNeedsCareTime = random(needsCareInterval);
}
if(nextNeedsRestTime == 0) {
nextNeedsRestTime = random(needsRestInterval);
}
if(nextNeedsFoodTime == 0) {
nextNeedsFoodTime = random(needsFoodInterval);
}
if(minSinceWake >= nextNeedsCareTime) {
needsCare();
nextNeedsCareTime = minSinceWake + needsCareInterval + random(needsCareInterval/4);
}
if(minSinceWake >= nextNeedsRestTime) {
needsRest();
nextNeedsRestTime = minSinceWake + needsRestInterval + random(needsRestInterval/4);
}
if(minSinceWake >= nextNeedsFoodTime) {
needsFood();
nextNeedsFoodTime = minSinceWake + needsFoodInterval + random(needsFoodInterval/4);
}
}
}
void screenDisplay(int screenNum) {
for (int row = 0; row <= 7; row ++) {
for (int col = 0; col <= 7; col ++) {
ledMatrix.setPoint(row, col, screenImage[screenNum][row][7 - col]);
}
}
}
void allGood() {
for (int neutralPhase = 0; neutralPhase <= 3; neutralPhase ++) {
screenDisplay(neutralPhase);
delay(1000);
}
screenDisplay(0);
delay(4000);
}
void needsCare() {
boolean loveButtonState;
do {
screenDisplay(4);
delay(5000);
screenDisplay(5);
delay(500);
loveButtonState = digitalRead(loveButton);
} while (loveButtonState != isPushed);
}
void needsFood() {
boolean foodButtonState;
do {
screenDisplay(0);
delay(1000);
screenDisplay(6);
delay(1000);
screenDisplay(0);
delay(1000);
foodButtonState = digitalRead(foodButton);
} while (foodButtonState != isPushed);
}
void needsRest() {
boolean sleepButtonState;
do {
screenDisplay(0);
delay(4000);
screenDisplay(8);
delay(4000);
screenDisplay(7);
delay(10000);
sleepButtonState = digitalRead(sleepButton);
} while (sleepButtonState != isPushed);
}