#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Rotary.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)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//rotary pins
#define ENCODER_CLK 2
#define ENCODER_DT 3
//variables du rotary
static int pos = 0;
int newPos;
int stade = 0; // stades : 0 - démarrage, 1 - Menu, 2 - on air
int etatrectangle = 0;
int rectangle=0;
int etatinter;
int etape1 = 0;
bool afficherrectangle =false;
int intermediaire;
// essai timer sans delay
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
int timer = 5; // Set the timer to 5 seconds
const int ledPin = LED_BUILTIN; // the number of the LED pin
int ledState = LOW;
int temp=400;
String gobtn="Go!";
typedef enum {
GO,
TIME,
TEMP,
} Item;
Item item = GO;
typedef enum {
IDLE,
MENU,
RUNNING,
} Mode;
Mode mode = IDLE;
//Partie Timer
int hora = 0;
int minu = 4;
int sec = 30;
void setup() {
Serial.begin(9600);
// 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
}
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
// Clear the buffer
display.clearDisplay();
}
int lastClk = HIGH; //dans fonction rotary
long int modeLastChanged = 0;
int prevClk = HIGH;
/* ------------------------------------ */
void loop() {
updateDisplay();
rotaryclic();
rotarymove();
//stade = intermediaire;
}
/* ------------------------------------ */
void updateDisplay(void)
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
displayTemp();
displayTime();
displayGo();
displayRec();
if (mode == RUNNING)
{
countdown();
}
display.display();
}
void displayTemp()
{
display.setTextSize(2);
display.setCursor(4, 2);
display.println(F("TEMP:"));
display.setCursor(65, 2);
display.println(temp);
}
void displayTime()
{
if (mode != RUNNING)
{
display.setTextSize(3);
display.setCursor(25, 22);
display.println(F("4:30"));
}
}
void displayGo()
{
display.setTextSize(2);
display.setCursor(90, 48);
if (mode == RUNNING)
{
gobtn ="---";
display.print(gobtn);
}
else {display.print(gobtn);}
}
void displayRec()
{
if (item == TEMP)
{
printRec(); //afficherlerecangle ?
}
if (item == TIME)
{
printRec(); //afficherlerecangle ?
}
if (item == GO)
{
printRec(); //afficherlerecangle ?
}
}
void nextItem() {
switch (item) {
case GO:
item = TEMP;
break;
case TEMP:
item = TIME;
break;
case TIME:
item = GO;
break;
}
}
void prevItem() {
switch (item) {
case GO:
item = TIME;
break;
case TEMP:
item = GO;
break;
case TIME:
item = TEMP;
break;
}
}
void nextMode()
{
switch (mode)
{
case IDLE:
mode = MENU;
break;
case MENU:
mode = RUNNING;
gobtn = "---";
break;
case RUNNING:
mode = IDLE;
break;
}
Serial.print("On change de mode vers le mode ");
Serial.println(mode);
}
void rotarymove() {
int clk = digitalRead(ENCODER_CLK);
if (clk != prevClk && clk == LOW)
{
int dt = digitalRead(ENCODER_DT);
int delta = dt == HIGH ? 5 : -5;
nextItem();
updateValueItem();
Serial.println(F("Changement de selection"));
updateDisplay();
}
/*if (clk != prevClk && clk == HIGH)
{
Serial.print("clk:");
Serial.println(clk);
int dt = digitalRead(ENCODER_DT);
int delta = dt == HIGH ? 5 : -5;
prevItem();
updateValueItem();
updateDisplay();
}*/
prevClk = clk;
}
void rotaryclic(void) {
if (debounce()) {
if (mode == MENU )
{
switch (item)
{
case GO:
Serial.println("Countdown lancé!");
nextMode();
//countdown();
//volume = constrain(volume + delta, 0, 100);
break;
case TEMP:
//TEMP = constrain(TEMP + delta, 0, 500);
break;
case TIME:
//treble = constrain(treble + delta, 0, 100);
break;
}
}
if (mode == IDLE)
{
Serial.println("Click bouton.");
intermediaire = 1;
afficherrectangle = true;
nextMode();
updateValueMode();
modifrectangle();
printRec();
}
/*if (mode == RUNNING)
{
display.clearDisplay();
countdown();
}
*/
}
}
void modifrectangle(void)
{
rectangle=etatrectangle; //fonction qui servira a déterminer la sortie rectangle
}
void printRec()
{
if (afficherrectangle) //si on demande d'afficher le rectangle
{
if (rectangle==0) {display.drawRect(84, 45, 40, 19, SSD1306_WHITE);}
else if (rectangle==1) {display.drawRect(20, 18, 78, 28, SSD1306_WHITE);}
else if (rectangle==2) {display.drawRect(60, 0, 45, 18, SSD1306_WHITE);}
}
}
void printTemp()
{
// if digitalread(pin thermocouple) = selectedTemp display.println("*");
}
void updateValueItem()
{
switch (item)
{
case GO:
rectangle = 0;
//volume = constrain(volume + delta, 0, 100);
break;
case TEMP:
rectangle = 2;
//bass = constrain(bass + delta, 0, 100);
break;
case TIME:
rectangle = 1;
//treble = constrain(treble + delta, 0, 100);
break;
}
}
bool debounce() {
static uint16_t state = 0;
state = (state<<1) | digitalRead(4) | 0xfe00;
return (state == 0xff00);
}
void updateValueMode()
{
switch (mode)
{
case IDLE:
//volume = constrain(volume + delta, 0, 100);
break;
case MENU:
//bass = constrain(bass + delta, 0, 100);
break;
case RUNNING:
//treble = constrain(treble + delta, 0, 100);
break;
}
Serial.println(mode);
}
/*void countdownworking()
{
// do
//{
//display.clearDisplay(); //Used to clear the display
//Text stuff, Size and Color
display.setTextSize(3);
display.setCursor(25, 22);
//This if(variable < 10) ensures that if the number is below 2 digits, it will remain centered otherwise justs prints the variable
if( minu < 10) {
//display.print("0");
display.setCursor(25,22);
display.print(minu);
}
else {
display.print(minu);
}
display.setCursor(40,22);
display.print(":");
if( sec < 10)
{
display.setCursor(60,22);
display.print("0");}
display.print(sec);
//Delay for 1s = 1000ms
delay(1000);
//The maths behinf this, if sec==0 then minu=minu-1 and if minu==0 then hora=hora-1, just the basics of a clock
sec = sec - 1;
if(sec == 0) {
sec = 59;
minu = minu - 1;
}
if (minu < 0) {
minu = 60;
}
display.display();
//Display the corrent countdown
//} while (hora != 0);
*/
//test timer
void countdown()
{
unsigned long currentMillis = millis();
//display.clearDisplay();
// for (int i = timer; i >= 0; i--) { // Set i to timer; test if timer is greater equal to 0; decrement timer
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
//display.clearDisplay(); //Used to clear the display
//Text stuff, Size and Color
display.setTextSize(3);
display.setCursor(25, 22);
//This if(variable < 10) ensures that if the number is below 2 digits, it will remain centered otherwise justs prints the variable
if( minu < 10) {
//display.print("0");
display.setCursor(25,22);
display.print(minu);
}
else {
display.print(minu);
}
display.setCursor(40,22);
display.print(":");
if( sec < 10)
{
display.setCursor(60,22);
display.print("0");}
display.print(sec);
//Delay for 1s = 1000ms
//delay(1000);
//The maths behinf this, if sec==0 then minu=minu-1 and if minu==0 then hora=hora-1, just the basics of a clock
sec = sec - 1;
if(sec == 0) {
sec = 59;
minu = minu - 1;
}
if (minu < 0) {
minu = 60;
}
display.display();
//Serial.println(i); // print i to the serial monitor
//Serial.println(timer);
//timer--;//decrease timer count
//if (timer == -1)
//{
//timer = 5;
//}
}
}