/**************************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/category/63_98
This example is for a 128x64 pixel display using I2C to communicate
3 pins are required to interface (two I2C and one reset).
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source
hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries,
with contributions from the open source community.
BSD license, check license.txt for more information
All text above, and the splash screen below must be
included in any redistribution.
**************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ezButton.h>
#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)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
ezButton button(7);
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 6
int lastClk = HIGH;
int Delay = 10;
int Count = 4;
int Min = 5;
int Max = 10;
int Proto = 0;
float Progress; //progress umgerechnet in balken pixel
float StartTime;
float RunningTime;
float CountDown;
bool Running = false;
int CountRunning;
int Random;
int SelectionMode = 0;
int Selection = 0;
void setup() {
Serial.begin(9600);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
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();
delay(20); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
//UpdateIdle();
IdleScreen();
//RunningScreen();
}
void loop() {}
void IdleScreen(void) {
UpdateIdle();
delay(100);
while(!Running){
button.loop();
if(button.isPressed()){
Running = true;
delay(100);
RunningScreen();
}
if (digitalRead(ENCODER_SW) == LOW) {
delay(150);
SelectionMode=1;
Selection++;
Serial.println("Button Pressed");Serial.println(Selection);
UpdateIdle();
}
if(Selection>4){
Selection = 0;
}
int newClk = digitalRead(ENCODER_CLK); // RotEncoder ABFRAGE und Aktion
if (newClk != lastClk) {
lastClk = newClk;
SelectionMode=1;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
if(SelectionMode==1){
if(Selection==1){
Delay++;
}
if(Selection==2){
Count++;
}
if(Selection==3){
Min++;
}
if(Selection==4){
Max++;
}
if(Selection==5){
//Enter the Protocols
}
}
delay(50);
UpdateIdle();
}
if (newClk == LOW && dtValue == LOW) {
if(SelectionMode==1){
if(Selection==1){
Delay--;
}
if(Selection==2){
Count--;
}
if(Selection==3){
Min--;
}
if(Selection==4){
Max--;
}
if(Selection==5){
//Enter the Protocols
}
}
delay(50);
UpdateIdle();
}
}
}
}
void UpdateIdle(void){
display.clearDisplay();
display.fillRect(0, 0, 128, 12, SSD1306_WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(54, 2);
display.print("IDLE");
display.setTextColor(SSD1306_WHITE);
display.setCursor(8, 20); display.print("delay:");
display.setCursor(46, 20); display.print(Delay);
display.setCursor(70, 20); display.print("count:");
display.setCursor(108, 20); display.print(Count);
display.setCursor(8, 35); display.print("t. range:");
display.setCursor(70, 35); display.print(Min);
display.setCursor(88, 35); display.print("-");
display.setCursor(100, 35); display.print(Max);
display.setCursor(28, 50); display.print("protocols:");
display.setCursor(89, 50); display.print(Proto);
if(SelectionMode==1){
if(Selection==1){
display.fillRect(8, 29, 50, 2, SSD1306_WHITE);
}
if(Selection==2){
display.fillRect(70, 29, 45, 2, SSD1306_WHITE);
}
if(Selection==3){
display.fillRect(66, 44, 15, 2, SSD1306_WHITE);
}
if(Selection==4){
display.fillRect(98, 44, 15, 2, SSD1306_WHITE);
}
if(Selection==5){
display.fillRect(24, 59, 75, 2, SSD1306_WHITE);
}
}
display.display();
}
void Cancel(void){
display.clearDisplay();
display.fillRect(0, 20, 128, 22, SSD1306_WHITE);
display.setTextSize(2);
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(34, 24);
display.print("CANCEL");
display.display();
Running=false;
delay(3000);
IdleScreen();
}
void RunningScreen(void) {
display.clearDisplay();
StartTime = millis();
CountRunning = Count;
Random = random(0,Max-Min);
while(Running){
button.loop();
if(button.isPressed()){ // ABBRUCH !!!
Cancel();
}
CountDown = Random + Delay + Min - (millis()-StartTime)/1000;
Progress = map(CountDown*10,(Random+Min+Delay)*10,0,0,108);
if(CountRunning < Count){
Delay = 0;
}
if(CountDown <= 0){ // PeekingPEEKING!!! Sound Detection
display.fillRect(0, 0, 128, 12, SSD1306_WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_BLACK);
display.setCursor(44, 2);
display.print("PEEKING");
display.display();
delay(4000);
CountRunning --;
StartTime = millis();
Random = random(0,Max-Min+Delay);
}
if(CountRunning == 0){
Running = false;
Proto ++;
IdleScreen();
}
display.clearDisplay();
display.fillRect(0, 0, 128, 12, SSD1306_WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(44, 2);
display.print("RUNNING");
display.setTextColor(SSD1306_WHITE);
display.setCursor(8, 14); display.print("next peek in");
display.setCursor(90, 14); display.print(CountDown);
display.drawRect(8,25,112,10,SSD1306_WHITE);
display.fillRect(10,27,Progress,6,SSD1306_WHITE);
display.setCursor(8, 42); display.print("delay:");
display.setCursor(46, 42); display.print(Random+Min+Delay);
display.setCursor(67, 42); display.print("count:");
display.setCursor(105, 42); display.print("(");display.print(CountRunning);display.print(")");
display.setCursor(8, 53); display.print("t. range:");
display.setCursor(70, 53); display.print(Min);
display.setCursor(88, 53); display.print("-");
display.setCursor(100, 53); display.print(Max);
display.display();
}
}
#define XPOS 0 // Indexes into the 'icons' array in function below
#define YPOS 1
#define DELTAY 2