#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#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
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int input1 = 2; // Input 1 pin
const int input2 = 3; // Input 2 pin
const int input3 = 4; // Input 3 pin
const int input4 = 5; // Input 4 pin
bool triggerLogic = HIGH; // Set the trigger logic to
const int timeOut = 10000; // Set timeout value if in this time no input trigger set the animation back
bool isIdle = true; // Flag to set if we are in the idle mode.
int lastInput = 0; // Variable to store the last input triggered
int currentInput = -1; // Variable to store the current input triggered
unsigned long lastInputTime = 0; // Variable to store the last input time
String inputMessages[4] = {
"Spirits are Saying No",
"Spirits are Saying Yes",
"Spirits are Saying Maybe",
"Spirits are UNDECIDED, ASK AGAIN"
};
void setup() {
Serial.begin(9600);
pinMode(input1, INPUT); // Set pin as input
pinMode(input2, INPUT); // Set pin as input
pinMode(input3, INPUT); // Set pin as input
pinMode(input4, INPUT); // Set pin as input
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(1000);
}
void loop() {
if(isIdle) // If in idle mode show animation
testfillcircle(); // Draw circles (filled)
// Check if any input is triggered
bool input1State = digitalRead(input1) == triggerLogic;
bool input2State = digitalRead(input2) == triggerLogic;
bool input3State = digitalRead(input3) == triggerLogic;
bool input4State = digitalRead(input4) == triggerLogic;
// Print the state of the inputs to the Serial Monitor
Serial.print("Input 1: ");
Serial.println(input1State ? "Triggered" : "Not Triggered");
Serial.print("Input 2: ");
Serial.println(input2State ? "Triggered" : "Not Triggered");
Serial.print("Input 3: ");
Serial.println(input3State ? "Triggered" : "Not Triggered");
Serial.print("Input 4: ");
Serial.println(input4State ? "Triggered" : "Not Triggered");
// If any input is triggered, display message and wait for a second
if((input1State || input2State || input3State || input4State) && (currentInput != lastInput)) {
// note the time of the last input
lastInputTime = millis();
isIdle = false; // Set idle mode to false
display.clearDisplay(); // Clear the display
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner set cursor
// Display the message based on which input was triggered
if(input1State) {
display.println(inputMessages[0]);
currentInput = 1; // Store current input
}else if(input2State) {
display.println(inputMessages[1]);
currentInput = 2; // Store current input
} else if(input3State) {
display.println(inputMessages[2]);
currentInput = 3; // Store current input
} else if(input4State) {
display.println(inputMessages[3]);
currentInput = 4; // Store current input
}
display.display(); // Show the message on the display
delay(1000); // Wait for a second
lastInput = currentInput; // Store the last input triggered
}
// If no input is triggered for a certain time, set idle mode to true
if(!isIdle && (millis() - lastInputTime > timeOut)) {
Serial.println("Timeout");
isIdle = true; // Set the idle mode
lastInput = -1;
}
}
void testfillcircle(void) {
display.clearDisplay();
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) {
// The INVERSE color is used so circles alternate white/black
display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1306_INVERSE);
display.display(); // Update screen with each newly-drawn circle
delay(1);
}
delay(1000);
}