#include <WiFi.h>
#include <HTTPClient.h>
#include "WiFi.h"
#include "EEPROM.h"
#include <ESP32Lib.h>
#define LENGTH(x) (strlen(x) + 1) // length of char string
#define EEPROM_SIZE 200 // EEPROM size
#define WiFi_rst 0 //WiFi credential reset pin (Boot button on ESP32)
// VGA pins
#define hsyncPin 27
#define vsyncPin 26
#define redPin 25
#define greenPin 33
#define bluePin 32
String ssid; //string variable to store ssid
String pss; //string variable to store password
unsigned long rst_millis;
int pulse = 0;
int outpulse = 0; // Variable for saving pulses count.
int var = 0;
int var2 = 0;
int input = 32;
int output = 33;
uint32_t resetAfterMillis = 5000;
uint32_t lastResetWas;
uint32_t resetAfterMillis2 = 5000;
uint32_t lastResetWas2;
unsigned long lastTimeGet = 0;
unsigned long timerDelayGet = 1000;
long inpls1 = 0;
bool shdPrint1 = false;
long debounce1 = 100;
long lastPulse1 = 0;
int readpulse;
long inpls2 = 0;
bool shdPrint2 = false;
long debounce2 = 100;
long lastPulse2 = 0;
void IRAM_ATTR inPulse1() {
if (digitalRead(input) == HIGH) {
if (millis() - lastPulse1 > debounce1) {
inpls1++;
lastPulse1 = millis();
shdPrint1 = true;
}
}
}
void IRAM_ATTR inPulse2() {
if (digitalRead(output) == HIGH) {
if (millis() - lastPulse2 > debounce2) {
inpls2++;
lastPulse2 = millis();
shdPrint2 = true;
}
}
}
VGA3Bit vga;
void setup() {
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);
vga.init (vga.MODE320x200,hsyncPin, vsyncPin, redPin, greenPin,bluePin);
lastResetWas = millis();
lastResetWas2 = millis();
pinMode(input, INPUT);
pinMode(output, INPUT);
inpls1 = EEPROM.read(64);
delay(5000);
attachInterrupt(digitalPinToInterrupt(input), inPulse1, FALLING);
attachInterrupt(digitalPinToInterrupt(output), inPulse2, FALLING);
}
void loop() {
uint32_t now = millis();
uint32_t now2 = millis();
if (shdPrint1) {
shdPrint1 = false;
EEPROM.write(64, inpls1);
EEPROM.commit();
Serial.println("State saved in flash memory");
Serial.println("Pulse1Count:" + String(inpls1));
vga.print("Pulse1Count:");
vga.print(inpls1);
vga.print("\n");
lastResetWas = now;
}
if (shdPrint2)
{
shdPrint2 = false;
Serial.println("Pulse2Count:" + String(inpls2));
vga.print("Pulse2Count:");
vga.print(inpls2);
vga.print("\n");
lastResetWas2 = now2;
}
if ((millis() - lastTimeGet) > timerDelayGet)
{
lastTimeGet = millis();
}
}