Codes

Retrouvez ici mes différents scripts et morceaux de code.

FICHIERS :
				
//
// -- BrOs V3.2 --
// Le BrOs est certainement  l'OS  qui  pèse  le  plus  au monde et qui démontre 
// la guindaille 12.0 en ce jour. Une vision du future défendue avec acharnement
// tout en respectant le folklore et  les  traditions  de  nos  anciens. Le BrOs
// définit  et  modère  avec  rigueur la consommation d'alcool et en particulier
// de bière  jusque  dans les  situations les  plus extrèmes.  L'utiliser, c'est
// affoner.
//
// Author : Nicolas Colsoul, copyright Broférie
// Date : 09/08/2016
// License : tout droit réservé, usage exclusif de la Broférie
//

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

long hour = 3600000;
long minute = 60000;

const int led1 = 7; // vert
const int led2 = 6; // rouge
const int led3 = 5; // bleu
const int button1 = 13; // select
const int button2 = 3; // up
const int button3 = 11; // down
const int vibrator = 4;
const int level3 = 10;
const int level2 = 9;
const long timeNotif = 3000;
const float limit = 0.5;
const boolean infusing = true;

boolean welcome = true;
boolean rempli = false;
boolean notif = false;
boolean isAffoning = false;
unsigned long timeGuindailling = 0;
unsigned long lastRempli = 0;
unsigned long startTime = 0;
unsigned long stopTime = 0;
unsigned long descenteTime = 300000;
float bestTime = 0.0;
float lastTime = 0.0;
int val2 = 0;
int val3 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int currentMenu = 0;
int lengthMenu = 2; // first is 0, so length 1 = 2 menus, hehe !
int intraMenu = 0;
int nbreVerres = 0;
int elapsedMin = 0;
int elapsedTot = 0;
long memoryTime = 0;
boolean avoidVib = true;
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void vibration() 
{
  if(avoidVib)
  {
    digitalWrite(vibrator, HIGH);
    delay(500);
    digitalWrite(vibrator, LOW);  
  }
}

void setup() 
{
  memoryTime = millis();
  // EEPROM MEMORY
  // 0 -> elapsedMin
  // 1 -> nbreVerres
  // 2 -> lastTime (sPart)
  // 3 -> lastTime (mPart)
  // 4 -> bestTime (sPart)
  // 5 -> bestTime (mPart)
  elapsedTot = (int) EEPROM.read(0);
  nbreVerres = (int) EEPROM.read(1);
  lastTime = EEPROM.read(2)*1.0 + EEPROM.read(3)/1000.0;
  bestTime = EEPROM.read(4)*1.0 + EEPROM.read(5)/1000.0;
  timeGuindailling = millis();
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(vibrator, OUTPUT);
  pinMode(level2, INPUT);
  pinMode(level3, INPUT);
  
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();
//  digitalWrite(vibrator, HIGH);
  Serial.begin(9600);
}

void loop() 
{
  
  val2 = digitalRead(level2);
  val3 = digitalRead(level3);
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);  
  buttonState3 = digitalRead(button3);

  if(val2 == 0)
  {
    rempli = true;
  }
  else
  {
    rempli = false;
  }
  
  if(welcome)
  {
    welcome = false;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Let's           ");
    lcd.setCursor(0,1);
    lcd.print("    guindailling");
    delay(2500);
    lcd.clear();
    Serial.print(EEPROM.read(2));
    Serial.print(" - o - ");
    Serial.println(EEPROM.read(3));
  }
  if(currentMenu == 0 && !notif)
  {
    if(rempli && !isAffoning)
    {
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      lastRempli = millis();
      lcd.setCursor(0,0);
      lcd.print("Verre rempli !");
      lcd.setCursor(0,1);
      if(lastTime != 0.0)
      {
        lcd.setCursor(9,1);
        lcd.print(lastTime, 3);
        lcd.setCursor(15,1);
        lcd.print("s");
      }
      
    }
    else if(!isAffoning)
    {
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
      lcd.setCursor(0,0);
      lcd.print("Verre vide...    ");
      if(lastTime != 0.0)
      {
        lcd.setCursor(9,1);
        lcd.print(lastTime, 3);
        lcd.setCursor(15,1);
        lcd.print("s");
      }
    }
  
    if(rempli && millis() - lastRempli < 1000 && lastRempli != 0 && val3 == 0 && !isAffoning)
    {
      isAffoning = true;
      startTime = millis();
      Serial.println("START");  
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("User is affoning !");
  
    }
    if(isAffoning && val3 == 1)
    {
      isAffoning = false;
      stopTime = millis();
      lcd.clear();
      lcd.setCursor(0,1);
      float elapsed = (stopTime - startTime)/1000.0;
      lcd.setCursor(0,0);
      lcd.print("Finished !");
      lcd.setCursor(9,1);
      lcd.print(elapsed, 3);
      lcd.setCursor(15,1);
      lcd.print("s");
      delay(1000);
      if(elapsed > limit)
      {
        lastTime = elapsed;
        nbreVerres = nbreVerres + 1;
        int sPart = (int) elapsed;
        int mPart = (int) (elapsed*1000.0 - 1000.0*sPart);
        EEPROM.write(1, nbreVerres); 
        EEPROM.write(2, sPart);
        EEPROM.write(3, mPart);
        Serial.print(sPart);
        Serial.print(".");
        Serial.println(mPart);
        if(elapsed < bestTime || bestTime == 255.0)
        {
          bestTime = elapsed;
          EEPROM.write(4, sPart);
          EEPROM.write(5, mPart);
        }
      }

      vibration();  
      lcd.clear();
    }
    else if(isAffoning)
    {
      if(buttonState3 == LOW)
      {
        float elapsed = (millis() - startTime)/1000.0;
        lcd.setCursor(9,1);
        lcd.print(elapsed, 3);
        lcd.setCursor(15,1);
        lcd.print("s");
        
      }
      else
      {
        isAffoning = false;
        lcd.clear();
      }
    }
  }
  else if(currentMenu == 1 && !notif)
  {
    int lengthOptions = 5; 
    switch (intraMenu) {
      case 0:
      {
        lcd.setCursor(0,0);
        lcd.print("--- Stats : ----");
        lcd.setCursor(0,1);
        lcd.print("  < Utilisez >  ");
        break;
      }
      case 1:
      {
        lcd.setCursor(0,0);
        lcd.print("- Meilleur tps -");
        lcd.setCursor(9,1);
        lcd.print(bestTime, 3);
        lcd.setCursor(15,1);
        lcd.print("s");
        break;
      }
      case 2:
      {
        lcd.setCursor(0,0);
        lcd.print("- Consommation -");
        lcd.setCursor(10,1);
        lcd.print(nbreVerres*0.25, 2);
        lcd.setCursor(15,1);
        lcd.print("L");
        break;
      }
      case 3:
      {
        lcd.setCursor(0,0);
        lcd.print("- Nbre verres  -");
        lcd.setCursor(15,1);
        lcd.print(nbreVerres);
        break;
      }
      case 4:
      {
        lcd.setCursor(0,0);
        lcd.print("- Temps total  -");
        int hours = elapsedMin/60;
        int minutes = elapsedMin - hours*60;
        lcd.setCursor(13,1);
        lcd.print("h");
        lcd.setCursor(12,1);
        lcd.print(hours);
        lcd.setCursor(14,1);
        if(minutes < 10)
        {
         lcd.print("0");
         lcd.setCursor(15,1); 
         lcd.print(minutes);
        }
        else
        {
          lcd.print(minutes);
        }
        break;
      }
      case 5:
      {
        lcd.setCursor(0,0);
        lcd.print("-     Debit    -");
        lcd.setCursor(5,1);
        if(elapsedMin > 0) lcd.print(nbreVerres*0.25/elapsedMin, 3); // 5 chars
        else lcd.print(0,3);
        lcd.setCursor(11,1);
        lcd.print("L/min"); // 5 + 1
        break;
      }
      default:
      break;
    }
    
    if(buttonState3 == HIGH && !notif) // down
    {
      lcd.clear();
      if(intraMenu == lengthOptions) intraMenu = 0;
      else intraMenu = intraMenu + 1;
      delay(200);
    }  
    if(buttonState2 == HIGH) // up
    {
      lcd.clear();
      if(intraMenu == 0) intraMenu = lengthOptions;
      else intraMenu = intraMenu - 1;
      delay(200);
    }  
  }
  else if(currentMenu == 2 && !notif)
  {
    int lengthOptions = 1; 
    switch (intraMenu) {
      case 0:
      {
        lcd.setCursor(0,0);
        lcd.print("--- Config : ----");
        lcd.setCursor(0,1);
        lcd.print("  < Utilisez  ");
        break;
      }
      case 1:
      {
        lcd.setCursor(0,0);
        lcd.print("--- Reset ----");
        lcd.setCursor(0,1);
        lcd.print("  Select >  ");
        break;
      }
      default:
      break;
    }
    if(buttonState3 == HIGH && !notif) // down
    {
      lcd.clear();
      if(intraMenu == lengthOptions) intraMenu = 0;
      else intraMenu = intraMenu + 1;
      delay(200);
    }  
    if(buttonState2 == HIGH) // up
    {
      if(intraMenu == 1)
      {
          for (int i = 0 ; i < EEPROM.length() ; i++) 
          {
            EEPROM.write(i, 0);
          }
          lcd.setCursor(0,0);
          lcd.print("RESET EEPROM");
          lcd.setCursor(0,1);
          lcd.print("Done!");
          delay(1000);
          lcd.clear();
      }
      delay(200);
    }  
    
  }

  if(buttonState1 == HIGH) 
  {
    lcd.clear();
    intraMenu = 0;
    if(currentMenu == lengthMenu)
    {
      currentMenu = 0;
    }
    else
    {
      currentMenu = currentMenu + 1;
    }
    delay(200);
  } 
 
  long actual = millis() - timeGuindailling;
  int hours = actual/hour;
  int minutes = (actual % hour) / minute;
  elapsedMin = elapsedTot + hours*60 + minutes;

  if(millis() - stopTime > descenteTime && millis() - stopTime < descenteTime + timeNotif && !rempli && infusing && !isAffoning)
  {
    notif = true;
    digitalWrite(vibrator, HIGH);
    lcd.clear();
    lcd.home();
    lcd.print("J'ai grand soif!");
    delay(timeNotif);
    digitalWrite(vibrator, LOW);
    lcd.clear();
    notif= false;
  }
  delay(200);

  if(millis() - memoryTime > 60000)
  { 
    memoryTime = millis();
    Serial.println("Gestion de la memoire en cours");
    EEPROM.write(0, elapsedMin);   
  }
  Serial.println(val2);
}