//-----------------------
//Stopwatch with Lap Time
//-----------------------
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//---------------------------------
//LiquidCrystal_I2C lcd(0x3F, 16, 2);
LiquidCrystal_I2C lcd(0x27, 20, 4); //LCD
#define I2C_SDA 41
#define I2C_SCL 42
//---------------------------------
String milliSec;
boolean debounce1, debounce2;
unsigned long btn1PressStart = 0, btn2PressStart = 0;
unsigned long Sec, Min, startMillis;
int lapNum = 1;
// Potentiometer is connected to GPIO 33,34,35 (Analog ADC1_CH6)
const int cylinder1 = 34;
const int cylinder2 = 35;
const int cylinder3 = 32;
// variable for storing the potentiometer value
int cylinder1Value = 0;
int cylinder2Value = 0;
int cylinder3Value = 0;
int value = 0;
int countCylinder1 = 0;
int countCylinder2 = 0;
int countCylinder3 = 0;
int valCylinder1;
int valCylinder2;
int valCylinder3;
int val;
int buttonVal;
int buttonCount = 0;
int LED1 = 25;
int LED2 = 26;
int LED3 = 27;
int buttonStart = 33;
//---------------------------
float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//---------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
//Wire.begin(I2C_SDA, I2C_SCL);
//analogSetAttenuation(ADC_11db);
// -- pin modes ---
pinMode(buttonStart, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
cylinder1Value = analogRead(cylinder1); // GPIO33
cylinder2Value = analogRead(cylinder2); // GPIO34
cylinder3Value = analogRead(cylinder3); // GPIO35
lcd.init();
lcd.backlight();
lcd.clear();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RESET CYLINDERS");
lcd.setCursor(0, 1);
lcd.print("Press to start");
}
void loop() {
countCylinder1 = 0;
countCylinder2 = 0;
countCylinder3 = 0;
//---Stop Watch ---
if (millis() - btn1PressStart > 500) debounce1 = true;
if (digitalRead(buttonStart) == HIGH && debounce1 == true)
{
btn1PressStart = millis();
debounce1 = false;
stopWatch();
buttonVal = digitalRead(buttonStart);
buttonCount = 0;
lcd.setCursor(13, 3); lcd.print(buttonCount);
}
// --- cylinder reading and mapping ---
cylinder1Value = analogRead(cylinder1); // GPIO33
cylinder2Value = analogRead(cylinder2); // GPIO34
cylinder3Value = analogRead(cylinder3); // GPIO35
valCylinder1 = map(cylinder1Value, 0, 4095, 0, 100);
valCylinder2 = map(cylinder2Value, 0, 4095, 0, 100);
valCylinder3 = map(cylinder3Value, 0, 4095, 0, 100);
// -- display readout --
lcd.setCursor(15, 1);
lcd.print(valCylinder1);
lcd.setCursor(15, 2);
lcd.print(valCylinder2);
lcd.setCursor(15, 3);
lcd.print(valCylinder3);
// -- cylinder readout function --
if (valCylinder1 > 45 || valCylinder1 < 55) {
digitalWrite(LED1, LOW);
};
if (valCylinder2 > 45 || valCylinder2 < 55) {
digitalWrite(LED2, LOW);
};
if (valCylinder3 > 45 || valCylinder3 < 55) {
digitalWrite(LED3, LOW);
};
}
void stopWatch()
{
lcd.clear();
startMillis = millis();
while (1)
{
if (millis() - btn1PressStart > 500) debounce1 = true;
if (digitalRead(buttonStart) == HIGH && debounce1 == true )
{
btn1PressStart = millis();
debounce1 = false;
lcd.setCursor(0, 0); lcd.print(" ");
lcd.setCursor(13, 0); lcd.print("END");
lapNum = 1;
btn2PressStart = 0;
buttonVal = digitalRead(buttonStart);
lcd.setCursor(13, 3); lcd.print(buttonVal);
break;
}
//-------------------------------------------------
if (countCylinder1 == 1 && countCylinder2 == 1 && countCylinder3 == 1)
{
break;
lcd.clear();
}
//----------------------------------------------
//-------------------------------------------------
milliSec = String((millis() - startMillis) % 1000);
Sec = ((millis() - startMillis) / 1000) % 60;
Min = ((millis() - startMillis) / 60000) % 60;
//-------------------------------------------------
lcd.setCursor(0, 0); lcd.print("BGN ");
if (Min < 10)
{
lcd.print("0"); lcd.print(Min);
}
else lcd.print(Min);
lcd.print(":");
if (Sec < 10)
{
lcd.print("0"); lcd.print(Sec);
}
else lcd.print(Sec);
lcd.print(".");
lcd.print(milliSec[0]); lcd.print(milliSec[1]);
cylinder1Value = analogRead(cylinder1);
cylinder2Value = analogRead(cylinder2);
cylinder3Value = analogRead(cylinder3);
valCylinder1 = map(cylinder1Value, 0, 4095, 0, 100);
valCylinder2 = map(cylinder2Value, 0, 4095, 0, 100);
valCylinder3 = map(cylinder3Value, 0, 4095, 0, 100);
// -- display readout --
lcd.setCursor(16, 1);
lcd.print(valCylinder1);
lcd.setCursor(16, 2);
lcd.print(valCylinder2);
lcd.setCursor(16, 3);
lcd.print(valCylinder3);
// -- cylinder readout function --
if (valCylinder1 < 45 || valCylinder1 > 55) {
//lapTime1();
digitalWrite(LED1, HIGH);
countCylinder1 = 1;
};
if (valCylinder2 < 45 || valCylinder2 > 55) {
//lapTime2();
digitalWrite(LED2, HIGH);
countCylinder2 = 1;
};
if (valCylinder3 < 45 || valCylinder3 > 55) {
//lapTime3();
digitalWrite(LED3, HIGH);
countCylinder3 = 1;
};
}
}
//============================================================
void lapTime1()
{
if (countCylinder1 <= 0) {
String lapMilliSec = milliSec;
int lapSec = Sec, lapMin = Min;
//---------------------------------------------
lcd.setCursor(0, 1);
lcd.print("C1");
lcd.setCursor(4, 1);
if (Min < 10)
{
lcd.print("0"); lcd.print(lapMin);
}
else lcd.print(lapMin);
lcd.print(":");
if (Sec < 10)
{
lcd.print("0"); lcd.print(lapSec);
}
else lcd.print(lapSec);
lcd.print(".");
lcd.print(lapMilliSec[0]); lcd.print(lapMilliSec[1]);
countCylinder1++;
}
}
void lapTime2()
{
if (countCylinder2 <= 0) {
String lapMilliSec = milliSec;
int lapSec = Sec, lapMin = Min;
//---------------------------------------------
lcd.setCursor(0, 2);
lcd.print("C2");
lcd.setCursor(4, 2);
if (Min < 10)
{
lcd.print("0"); lcd.print(lapMin);
}
else lcd.print(lapMin);
lcd.print(":");
if (Sec < 10)
{
lcd.print("0"); lcd.print(lapSec);
}
else lcd.print(lapSec);
lcd.print(".");
lcd.print(lapMilliSec[0]); lcd.print(lapMilliSec[1]);
countCylinder2++;
}
}
void lapTime3()
{
if (countCylinder3 <= 0) {
String lapMilliSec = milliSec;
int lapSec = Sec, lapMin = Min;
//---------------------------------------------
lcd.setCursor(0, 3);
lcd.print("C3");
lcd.setCursor(4, 3);
if (Min < 10)
{
lcd.print("0"); lcd.print(lapMin);
}
else lcd.print(lapMin);
lcd.print(":");
if (Sec < 10)
{
lcd.print("0"); lcd.print(lapSec);
}
else lcd.print(lapSec);
lcd.print(".");
lcd.print(lapMilliSec[0]); lcd.print(lapMilliSec[1]);
countCylinder3++;
}
}