#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#define LIGHT_SENSOR_PIN 34
#define LED_blink 19
const float GAMMA = 0.7;
const float RL10 = 50;
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
TaskHandle_t myTask1Handle = NULL;
TaskHandle_t myTask2Handle = NULL;
void setup() {
xTaskCreate(task1, "luminosité", 4096, NULL,10, &myTask1Handle);
//xTaskCreate(task2, "Températuure", 4096, NULL, 9, &myTask2Handle);
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void task1(void *arg)
{
while(1) {
int analogValue = analogRead(LIGHT_SENSOR_PIN);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(2, 0);
lcd.print("Room: ");
if (lux > 50) {
lcd.print("Light!");
Serial.println("Light!");
} else {
lcd.print("Dark ");
Serial.println("Dark");
}
vTaskDelay( 100 / portTICK_PERIOD_MS );
}
}
void loop() {}
/*int analogValue = analogRead(LIGHT_SENSOR_PIN);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(2, 0);
lcd.print("Room: ");
if (lux > 50) {
lcd.print("Light!");
} else {
lcd.print("Dark ");
}
lcd.setCursor(0, 1);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(100);
char key;
passcode = "";
int i;
if (key) {
for(i=0;i<=5;i++)
{
key = keypad.getKey();
passcode += key;
Serial.println(passcode);
}
if(passcode == password_1 || passcode == password_2 || passcode == password_3) {
Serial.println("The password is correct, unlocking the door in 20 seconds");
} else {
Serial.println("The password is incorrect, try again");
}
}
/*if(key == '*') {
passcode = ""; // reset the input password
Serial.println("The password is reset");
} else if(key == '#') {
if(passcode == password_1 || passcode == password_2 || passcode == password_3) {
Serial.println("The password is correct, unlocking the door in 20 seconds");
} else {
Serial.println("The password is incorrect, try again");
}
passcode = ""; // reset the input password
} else {
passcode += key;
// append new character to input password string
}*/
// Motor A
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enable1Pin = 14;
// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;
void setup() {
// sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
// configure LED PWM functionalitites
ledcSetup(pwmChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(enable1Pin, pwmChannel);
Serial.begin(115200);
// testing
Serial.print("Testing DC Motor...");
}
// Move the DC motor forward at maximum speed
Serial.println("Moving Forward");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
delay(2000);
// Stop the DC motor
Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);
// Move DC motor backwards at maximum speed
Serial.println("Moving Backwards");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
delay(2000);
// Stop the DC motor
Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);
// Move DC motor forward with increasing speed
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
while (dutyCycle <= 255){
ledcWrite(pwmChannel, dutyCycle);
Serial.print("Forward with duty cycle: ");
Serial.println(dutyCycle);
dutyCycle = dutyCycle + 5;
delay(500);
}
dutyCycle = 200;
}