#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
int ledPins[] = {23, 19, 18, 5, 4, 15, 25, 32};
// à droite
const int out23 = 23; // led
const int out19 = 19; // led
const int out18 = 18; // led
const int out05 = 5; // led
const int out04 = 4; // led
const int out02 = 2; // ?pwr
const int out15 = 15; // led
//const int out21 = 21; // SDA
// à gauche
const int in34 = 34; // input only
const int in35 = 35; // ""
// 32 output
// 33 analog
const int in25 = 25;
const int in26 = 26;
const int in27 = 27;
const int in14 = 14;
const int in12 = 12;
const int in13 = 13;
const int out32 = 32; // Output socket
const int ldr = 33; // LDR (Light Dependent Resistor)
// LDR (Light Dependent Resistor)
//#define ldr 33
#define LED_1 10 // SD3
#define LED_2 11 // SD2
int ledState = LOW; // current state of the output pin
int buttonState; // current reading from the input pin
int lastButtonState = LOW; // previous reading from the input pin
bool motionDetected = false; // flag variable to send motion alert message
bool clearMotionAlert = true; // clear last motion alert message from web page
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int ledPower = 2;
double dustDensity;
int sensorValue;
double voltage;
// OLED Display
#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)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
Serial.begin(9600);
// set LED pin as output
pinMode(ledPower, OUTPUT); // set the LED pin low
Serial.println("Could not find a valid BME280 sensor, check wiring!");
Serial.println("Impossible de trouver un capteur BME280 valide, vérifiez le câblage !");
ledcSetup(0, 5000, 8); // Configurer le canal 0 pour une fréquence de 5000 Hz et une résolution de 8 bits
ledcAttachPin(14, 0); // Attacher la broche LED au canal 0
// 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
}
display.display();
// Clear the buffer
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 5);
display.println("Dino Game");
display.display();
// initialize the LED pin as an output
// à droite
pinMode(out23, OUTPUT);
pinMode(out19, OUTPUT);
pinMode(out18, OUTPUT);
pinMode(out15, OUTPUT);
pinMode(out05, OUTPUT);
pinMode(out04, OUTPUT);
//pinMode(out02, OUTPUT);
// à gauche
pinMode(out32, OUTPUT);
pinMode(in34, INPUT_PULLUP);
pinMode(in35, INPUT_PULLUP);
pinMode(in25, INPUT_PULLUP);
pinMode(in26, INPUT_PULLUP);
pinMode(in27, INPUT_PULLUP);
pinMode(in14, OUTPUT);
pinMode(in12, OUTPUT);
pinMode(in13, OUTPUT);
// Initialize OLED Display
/* if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}*/
/*
// Display Date and Time on OLED display
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(19, 5);
display.print("hour");
display.print(":");
display.print("minute");
display.display();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(2,2);
display.print(F("Score:"));
display.display();
*/
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
Serial.println("Connecting to SERIAL..");
delay(1000);
}
void loop() {
// Allumer chaque LED l'une après l'autre
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], HIGH); // Allumer la LED
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(12,20);
display.print("Score:");
display.display();
delay(500); // Attendre une seconde
//display.setTextColor(BLACK);
display.setCursor(12,20);
display.print(" ");
display.display();
digitalWrite(ledPins[i], LOW); // Éteindre la LED
delay(500);
}
}
/*
void loop()
{
digitalWrite(ledPower, LOW);
// put your main code here, to run repeatedly:
sensorValue = analogRead(ldr); // needs about 100 us
delayMicroseconds(30); // delay about 30 us
// set the LED pin high
digitalWrite(ledPower, HIGH);
delayMicroseconds(9680); // filling out the rest of the pulse cycle
// Convert analog value to voltage
voltage = sensorValue * (5.0 / 1023.0); // 3.3V
/*
// calculate dust density
if (voltage <= 0.9)
dustDensity = 0.0;
if (voltage < 3.5 && voltage > 0.9)
dustDensity = -0.0127 * pow(voltage, 2.0) + 0.2225 * voltage - 0.1819;
if (voltage >= 3.5)
dustDensity = 40.0 * pow(voltage, 2.0) - 280.8 * voltage + 493.28;
if (dustDensity >= 0.8)
dustDensity = 0.8;
// print the measured and calculated data
Serial.println("Voltage:\t" + String(voltage) + " V\tDust Density:\t" + String(dustDensity) + " mg/m^3");
* /
//digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(out32, HIGH); // G
digitalWrite(out23, HIGH);
digitalWrite(out19, HIGH);
digitalWrite(out18, HIGH);
digitalWrite(out05, HIGH);
digitalWrite(out04, HIGH);
digitalWrite(out02, HIGH);
digitalWrite(out15, HIGH);
digitalWrite(in12, HIGH); // G
digitalWrite(in13, HIGH); // G
digitalWrite(in14, HIGH); // G
delay(500);
//digitalWrite(LED_BUILTIN, LOW);
digitalWrite(out32, LOW);
digitalWrite(out23, LOW);
digitalWrite(out19, LOW);
digitalWrite(out18, LOW);
digitalWrite(out05, LOW);
digitalWrite(out04, LOW);
digitalWrite(out02, LOW);
digitalWrite(out15, LOW);
digitalWrite(in12, LOW); // G
digitalWrite(in13, LOW); // G
digitalWrite(in14, LOW); // G
delay(500);
}
*/
/*
x = digitalRead(in34);
Serial.println("34 " + String(x));
if (x == HIGH) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating bt34...");
} else {
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Nothing...");
}
x = digitalRead(in35);
Serial.println("35 " + String(x));
// 32
// 33
x = digitalRead(in25);
Serial.println("25 " + String(x));
x = digitalRead(in26);
Serial.println("26 " + String(x));
x = digitalRead(in27);
Serial.println("27 " + String(x));
x = digitalRead(in14);
Serial.println("14 " + String(x));
x = digitalRead(in12);
Serial.println("12 " + String(x));
x = digitalRead(in13);
Serial.println("13 " + String(x));
display.print("Temperature: ");*/