Files
MQ3-Alkoholtester/src/main.cpp
2022-10-31 12:38:30 +01:00

439 lines
15 KiB
C++

#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_SH1106.h>
#include <Adafruit_GFX.h>
#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);
int AlkPin=A1;
int ButtonPin=4; //I2C SDA->A4 SCL->A5
int RedPin=13;
int GreenPin=12;
int BluePin=11;
int BuzzPin=10;
int AlkVal;
int MaxAlkVal;
int AlkSum;
int AlkMean;
int ButtonVal;
int i;
int j;
int Bier;
int Aufwaermzeit = 10;
int Messzeit = 10;
int time;
int time2;
// 'Wolfi128x64', 128x64px
const unsigned char BitmapWolfi [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x9c, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xbc, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x02, 0x3f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x1e, 0x1f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x3f, 0xcf, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x3c, 0x00, 0x3f, 0xe7, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x58, 0x00, 0x7f, 0xb0, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xb8, 0x01, 0xfe, 0x0c, 0x1f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xb0, 0x01, 0xfe, 0x06, 0x07, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x03, 0xfe, 0x20, 0x03, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x07, 0xff, 0xa0, 0x03, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x0f, 0xff, 0xff, 0x03, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0xff, 0xff, 0x97, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x07, 0xff, 0xff, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0f, 0xff, 0xfe, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xcf, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xcf, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xcf, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0x8f, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x8f, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0x8f, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xf1, 0xff, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xef, 0xff, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xef, 0xfe, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xef, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x1f, 0xff, 0xff, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xfe, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xfe, 0xfe, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xfe, 0x7c, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x1f, 0xff, 0xff, 0x39, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x1f, 0xc1, 0xff, 0xc1, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x0b, 0x7f, 0xe1, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0xe3, 0xe1, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x1f, 0xfc, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x3f, 0x1f, 0xff, 0x83, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x9f, 0xff, 0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x9f, 0xf8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x1f, 0xfc, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xbf, 0xfe, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xbf, 0xfe, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0xff, 0xbf, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0xff, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0xff, 0x7f, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0xff, 0x7f, 0xfe, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x3f, 0x7f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0xff, 0xf8, 0x00, 0x3f, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xff, 0xff, 0x00, 0x1f, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1f, 0xff, 0xff, 0x80, 0x0f, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x3f, 0xff, 0xff, 0xc0, 0x07, 0xbf, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x3f, 0xff, 0xff, 0x80, 0x07, 0x9f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xff, 0xff, 0xe0, 0x03, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xff, 0xf0, 0x01, 0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void Startbildschirm(){
display.clearDisplay(); //Display löschen
display.setTextColor(WHITE); //Textfarbe
display.setTextSize(2); //Textgröße
display.setCursor(0,0);
display.setCursor(20,5);
display.print("WOLFI'S");
display.setCursor(18,35);
display.print("Sudmeter");
display.display();
for(j=0; j<3; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(GreenPin,HIGH);
delay(300);
digitalWrite(BuzzPin,LOW);
digitalWrite(GreenPin,LOW);
delay(300);
}
}
void Foto(){
display.clearDisplay();
display.drawBitmap(0,0,BitmapWolfi,128,64,WHITE);
display.display();
}
void Aufwaermen(){
display.clearDisplay();
time = map(time, 0, Aufwaermzeit, 0, 100);
display.setTextColor(WHITE); //Textfarbe
display.setTextSize(1); //Textgröße
display.setCursor(25,20);
display.print("Aufwaermen...");
display.drawRect(10, 50, 110, 10, WHITE); //Empty Bar
display.fillRect(10, 50, time,10,WHITE);
display.display();
}
void Messbildschirm(){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(33,10);
display.print("Start");
display.setTextSize(1);
display.setCursor(23,40);
display.print("Knopf druecken");
display.display();
}
void Messung(){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(35,5);
display.print("3 Mal");
display.setCursor(23,35);
display.print("hauchen");
display.setTextSize(1);
display.setCursor(18,43);
display.print("15 cm Entfernung");
display.display();
}
void Ausgabe(){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(30,5);
display.print("Status: ");
display.setCursor(30,30);
display.print(Bier);
display.print(" Bier");
display.setCursor(40,50);
display.setTextSize(1);
display.print(AlkMean);
display.display();
if(Bier==0){
digitalWrite(BuzzPin,HIGH);
digitalWrite(GreenPin,HIGH);
delay(1000);
digitalWrite(BuzzPin,LOW);
digitalWrite(GreenPin,LOW);
} else if (Bier==1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(GreenPin,HIGH);
delay(1000);
digitalWrite(BuzzPin,LOW);
digitalWrite(GreenPin,LOW);
} else if (Bier==2){
digitalWrite(BuzzPin,HIGH);
digitalWrite(GreenPin,HIGH);
delay(1000);
digitalWrite(BuzzPin,LOW);
digitalWrite(GreenPin,LOW);
} else if (Bier==3){
for(j=0; j<3; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(BluePin,HIGH);
delay(300);
digitalWrite(BuzzPin,LOW);
digitalWrite(BluePin,LOW);
delay(300);
}
} else if (Bier==4){
for(j=0; j<3; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(BluePin,HIGH);
delay(300);
digitalWrite(BuzzPin,LOW);
digitalWrite(BluePin,LOW);
delay(300);
}
} else if (Bier==5){
for(j=0; j<3; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(BluePin,HIGH);
delay(300);
digitalWrite(BuzzPin,LOW);
digitalWrite(BluePin,LOW);
delay(300);
}
} else if (Bier==6){
for(j=0; j<6; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(RedPin,HIGH);
delay(100);
digitalWrite(BuzzPin,LOW);
digitalWrite(RedPin,LOW);
delay(100);
}
} else if (Bier==7){
for(j=0; j<6; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(RedPin,HIGH);
delay(100);
digitalWrite(BuzzPin,LOW);
digitalWrite(RedPin,LOW);
delay(100);
}
} else if (Bier==8){
for(j=0; j<6; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(RedPin,HIGH);
delay(100);
digitalWrite(BuzzPin,LOW);
digitalWrite(RedPin,LOW);
delay(100);
}
} else if (Bier==9){
for(j=0; j<6; j=j+1){
digitalWrite(BuzzPin,HIGH);
digitalWrite(RedPin,HIGH);
delay(100);
digitalWrite(BuzzPin,LOW);
digitalWrite(RedPin,LOW);
delay(100);
}
} else if (Bier==10){
digitalWrite(BuzzPin,HIGH);
digitalWrite(RedPin,HIGH);
delay(2000);
digitalWrite(BuzzPin,LOW);
digitalWrite(RedPin,LOW);
}
}
void Berechnung(){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(15,5);
display.print("Berechne");
display.setTextSize(2);
display.setCursor(15,35);
display.print("Sudlevel");
display.display();
}
void Message(){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
if(Bier==0){
display.print("Sauf amal was du");
display.setCursor(0,35);
display.print("Muschi!");
} else if (Bier==1){
display.print("Oans geahtalm");
} else if (Bier==2){
display.print("Nach dem ");
display.setCursor(0,17);
display.print("geah i");
display.setCursor(0,35);
display.print("hoam");
} else if (Bier==3){
display.print("I geah");
display.setCursor(0,17);
display.print("jetzt hoam");
} else if (Bier==4){
display.print("Mei Vater");
display.setCursor(0,17);
display.print("hat ah");
display.setCursor(0,35);
display.print("a Glatzn");
} else if (Bier==5){
display.print("Yeeeaaah, du bisch ");
display.setCursor(0,30);
display.print("so cool,");
display.setCursor(0,45);
display.print("Hangl");
} else if (Bier==6){
display.print("Halt dei");
display.setCursor(0,17);
display.print("scheiss");
display.setCursor(0,35);
display.print("Maul");
} else if (Bier==7){
display.print("Jaeger-");
display.setCursor(0,17);
display.print("Meister");
display.setCursor(0,35);
display.print("Inc.");
} else if (Bier==8){
display.print("Rosenheim");
display.setCursor(0,17);
display.print("Inc.");
} else if (Bier==9){
display.print("Berchtes-");
display.setCursor(0,17);
display.print("gaden");
display.setCursor(0,35);
display.print("Inc.");
} else if (Bier==10){
display.print("Bettl-");
display.setCursor(0,17);
display.print("Ownage");
display.setCursor(0,35);
display.print("Inc.");
}
display.display();
}
void setup() {
Wire.begin(); //Wire-Library für I2C starten
display.begin(SH1106_SWITCHCAPVCC, 0x3C); //Oled-Display mit I2C Adresse initialisieren
pinMode(AlkPin,INPUT);
pinMode(ButtonPin,INPUT);
pinMode(RedPin,OUTPUT);
pinMode(GreenPin,OUTPUT);
pinMode(BluePin,OUTPUT);
pinMode(BuzzPin,OUTPUT);
digitalWrite(ButtonPin,HIGH);
Serial.begin(9600);
Startbildschirm();
delay(3000);
Foto();
delay(3000);
time = millis()/1000;
while(time<=Aufwaermzeit){
Aufwaermen();
time = millis()/1000;
}
}
void loop(){
AlkVal=0;
MaxAlkVal=0;
AlkSum=0;
i=0;
Messbildschirm();
ButtonVal=digitalRead(ButtonPin);
while(ButtonVal==1){
ButtonVal=digitalRead(ButtonPin);
}
digitalWrite(BuzzPin,HIGH);
digitalWrite(GreenPin,HIGH);
delay(300);
digitalWrite(BuzzPin,LOW);
digitalWrite(GreenPin,LOW);
time2 = millis()/1000;
Messzeit = time2+8;
while(time2<=Messzeit){
Messung();
AlkVal=analogRead(AlkPin);
AlkSum=AlkSum+AlkVal;
i=i+1;
time2 = millis()/1000;
Serial.println(AlkVal);
delay(500);
}
AlkMean=AlkSum/i;
if(AlkMean<140){
Bier=0;
}else if (AlkMean<180){
Bier=1;
}else if (AlkMean<220){
Bier=2;
}else if (AlkMean<260){
Bier=3;
}else if (AlkMean<300){
Bier=4;
}else if (AlkMean<340){
Bier=5;
}else if (AlkMean<380){
Bier=6;
}else if (AlkMean<420){
Bier=7;
}else if (AlkMean<460){
Bier=8;
}else if (AlkMean<500){
Bier=9;
}else if (AlkMean<1000){
Bier=10;
}
digitalWrite(BuzzPin,HIGH);
digitalWrite(RedPin,HIGH);
delay(300);
digitalWrite(BuzzPin,LOW);
digitalWrite(RedPin,LOW);
Berechnung();
delay(3000);
Ausgabe();
delay(5000);
Message();
delay(10000);
}