#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include "U8glib.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
// Tempo de aquecimento
int tempo_aquecimento = 300;
int valor_sensor = 0;
unsigned long time;
int status = 1;
String estado;
#define TextoMatrix "OoOoOoOoOoOoOoOoOoOoOoOoOoOoOo";
int posicao1;
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN 13 // Pino de clock
#define DATA_PIN 11 // Pino de dados
#define CS_PIN 10 // Pino de chip select
MD_Parola parola = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
void setup() {
Serial.begin(9600);
/**SENSOR***/
pinMode(A1, INPUT);
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255, 255, 255);
}
/**FIM SENSOR**/
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
/*#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif*/
strip.begin();
strip.show(); // Initialize all pixels to 'off'
parola.begin();
parola.displayText(TextoMatrix, PA_LEFT, 100, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
}
void loop() {
if (parola.displayAnimate()) { parola.displayReset();}
/***INICIO SENSOR**/
// Leitura do valor do sensor
valor_sensor = analogRead(A1);
time = millis() / 1000; //PADRAO ESTAVA 1000 ao inves de 50
u8g.firstPage();
// Tempo de aquecimento
if (time <= tempo_aquecimento) {
time = map(time, 0, tempo_aquecimento, 0, 30);
status = 1;
colorWipe(strip.Color(0, 0, 255), 50); //led azul
}
else {
status = 0;
}
status = 0; //sempre pronto, tirando o tempo de aquecimento
/*
int redValue = map(valor_sensor, 0, 1023, 0, 255);
int greenValue = map(valor_sensor, 0, 1023, 0, 255);
int blueValue = map(valor_sensor, 0, 1023, 0, 255);
*/
// Mensagem rodape
if (valor_sensor >= 0 and valor_sensor <= 50) {
estado = "Voce nao Bebeu";
colorWipe(strip.Color(0, 255, 0), 50); // Green
}
else if (valor_sensor >= 51 and valor_sensor <= 200) {
estado = "Bebeu Pouquinho !";
colorWipe(strip.Color(255, 255, 0), 50); // amarelo
}
else if (valor_sensor >= 201 and valor_sensor <= 400){
estado = "Ja ta alegre!";
colorWipe(strip.Color(255, 165, 0), 50); // laranja
}
else if (valor_sensor >= 401 and valor_sensor <= 600){
estado = "Voce cheira Pinga !";
//colorWipe(strip.Color(0, 0, 255), 50); // Blue
colorWipe(strip.Color(255, 0, 0), 50); // Red
}
else if (valor_sensor >= 601)
{
estado = "Voce esta Bebado !!!";
theaterChase(strip.Color(255, 0, 0), 50); // Red
}
// Chama a rotina de desenho na tela
do
{
draw();
}
while ( u8g.nextPage() );
delay(50);
/***FIM SENSOR**/
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
/******* TUDO DO SENSOR **********/
void draw()
{
// Comandos graficos para o display devem ser colocados aqui
// Seleciona a fonte de texto
u8g.setFont(u8g_font_8x13B);
// Mensagens iniciais
u8g.drawRFrame(0, 18, 128, 46, 4);
u8g.drawStr( 30, 15, "BAFOMETRO");
u8g.drawStr( 10, 37, "Aguarde");
// Animacao caneca
if (status == 1)
{
u8g.drawBox(80, 25, 20, 30);
u8g.drawHLine(77, 24, 26);
u8g.drawRFrame(78, 25 , 24, 32, 0);
u8g.drawRFrame(77, 25 , 26, 32, 0);
u8g.drawHLine(76, 57, 28);
u8g.drawHLine(76, 58, 28);
u8g.drawRFrame(102, 30 , 7, 20, 2);
u8g.drawRFrame(102, 28 , 9, 24, 2);
u8g.setColorIndex(0);
if (time == 0)
{
time = 1;
}
u8g.drawBox(79, 25, 22, time);
u8g.setColorIndex(1);
}
// Apos o aquecimento, exibe dados do sensor
if (status == 0)
{
u8g.setFont(u8g_font_fub20);
u8g.setColorIndex(0);
u8g.drawBox(10, 25, 110, 33);
u8g.setColorIndex(1);
// Centraliza o valor na tela
if (valor_sensor <= 99)
{
posicao1 = 50;
}
else
{
posicao1 = 43;
}
// Mostra valor do sensor
u8g.setPrintPos(posicao1, 45);
u8g.print(valor_sensor);
u8g.setFont(u8g_font_ncenB08);
// Imprime mensagem no rodape
int tamanho = estado.length();
int posicao = (128 / 2 - 3) - ((tamanho * 5) / 2);
u8g.setPrintPos(posicao, 60);
u8g.print(estado);
}
}
/******* FIM TUDO DO SENSOR **********/