// include the library code:
#include <Arduino.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
// Create An LCD Object. Signals: [ RS, EN, D4, D5, D6, D7 ]
LiquidCrystal lcd(13, 12, 14, 27, 26, 25);
const int ldr = 33; // LDR (Light Dependent Resistor)
word sensorValue;
word lastValue;
int valeur = 0; //variable où on stock la lecture analogique
int position; //position du potentiomètre en tant que pour cent
int last_pos = 0;
int cp = 0;
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;
const int numSamples = 2; // number of samples to use for slope calculation
int samples[numSamples]; // array to store the samples
const int out23 = 23; // problème !!!
const int out32 = 32;
const int out6 = 19;
const int out5 = 18;
const int out4 = 5;
const int out3 = 4;
const int out2 = 2;
const int out1 = 15;
void setup() {
Serial.begin(9600);
Serial.println("Hello, ESP32!");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("circuitschools.");
pinMode(out23, OUTPUT); // problème !!!
pinMode(out32, OUTPUT);
pinMode(out6, OUTPUT);
pinMode(out5, OUTPUT);
pinMode(out4, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out1, OUTPUT);
}
/*
32
435
838
1241
1644
2047
2450
2853
3256
3659
4062
*/
int calculate_position(word val)
{
int position = 0;
if (val < 435)
position = 0;
else if (val < 838)
position = 1;
else if (val < 1241)
position = 2;
else if (val < 1644)
position = 3;
else if (val < 2447)
position = 4;
else if (val < 2853)
position = 5;
else if (val < 3256)
position = 6;
else if (val < 3659)
position = 7;
else if (val < 4062)
position = 8;
else if (val <5000)
position = 9;
else
position = 10;
return position;
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
lcd.print(" ");
// 32 to 4063
sensorValue = analogRead(ldr); // needs about 100 us
delayMicroseconds(30); // delay about 30 us
// mets à l'échelle de 0 à 10 pour 32 à 4063
position = map(sensorValue, 32, 4063, 0, 100); // convertir en pourcentage
lcd.println(position);
lcd.setCursor(10, 1);
samples[0] = samples[1];
samples[1] = sensorValue; //position;
// calculate the slope of the signal as the difference between the two samples
int slope = samples[1] - samples[0];
// print the slope to the serial monitor
// Serial.print(slope); Serial.print(",");
Serial.print(sensorValue); Serial.print(",");
if (slope == 0) {
digitalWrite(out32, HIGH);
} else {
digitalWrite(out32, LOW);
}
cp = calculate_position(sensorValue);
Serial.print(cp);
Serial.print(":");
switch (cp) {
case 0:
digitalWrite(out6, HIGH);
break;
case 1:
digitalWrite(out5, HIGH);
break;
case 2:
digitalWrite(out4, HIGH);
break;
case 3:
digitalWrite(out3, HIGH);
break;
case 4:
digitalWrite(out2, HIGH);
break;
case 5:
digitalWrite(out1, HIGH);
break;
default:
Serial.print("Sensor error");
}
digitalWrite(out6, LOW);
digitalWrite(out5, LOW);
digitalWrite(out4, LOW);
digitalWrite(out3, LOW);
digitalWrite(out2, LOW);
digitalWrite(out1, LOW);
delay(500); // this speeds up the simulation
// if (position == last_pos) {
// digitalWrite(out23, HIGH);
// // Serial.print(position);
// // Serial.print(" == ");
// // Serial.println(last_pos);
// motionDetected = false;
// } else {
// digitalWrite(out23, LOW);
// // Serial.print(position);
// // Serial.print(" <> ");
// // Serial.println(last_pos);
// motionDetected = true;
// }
// if (clearMotionAlert != motionDetected) {
// Serial.print(motionDetected);
// }
// clearMotionAlert = motionDetected;
// last_pos = position;
// Set The Cursor Position To: [ Col 5, Row 1]
// The Next Message Will Start From The 6th Char Position in The 2nd Row
// Note: 1st Row Has An Index of 0, The 2nd Row Has An Index of 1
}
/*
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
*/