This is code for a ESP 8266 (WeMos D1 mini).
Problem statement - When turn signal of motorcycle has been switched on for a period of time, remind user to switch it off by sounding a buzzer, while performing other functions.
My approach - Since the turn signal goes high/low multiple times when switched on, it wouldn't be possible to take the time the pin goes high(since every time the pin goes low, it will be reset). Also, there might be instances when the turn signal is turned on, turned off and again turned on.
I decided to use a detect change state for this. ie.) First determine the number of times pin goes high/low when turn signal is switched on for a period of time(1 minute). Then write code such that buzzer goes high, when the number of times the pin is high exceeds the above mentioned value.
Now, there are several example codes for detect state change, however, i need to be able to run other tasks while this is occuring.
This other task is the transmission of an ESP Now signal containing an integer based on pin state of 3 pins(Brake and L and R turn signal)
My understanding is I have to combine my knowledge of the Blink w/o delay and the detect state change code to accomplish this.
However, I am new to coding and this seems to go over my head.
In my code, either the esp now takes place or the change detection state work, but not both and I would like both to work.
Any pointers in the right direction and I would be very thankful.
//this is the transmitter
#include <ESP8266WiFi.h>
#include <espnow.h>
uint8_t mac_peer[] = {0xE8, 0x9F, 0x6D, 0x8F, 0xDF, 0x56}; //receiver MAC
const int leftIN = D5;
const int rightIN = D6;
const int brakeIN = D7;
const int buzzerOUT = D3;
const int inBuiltLED = D4;
uint8_t transmit = 0;
int Counter = 0;
int Lsignalstate = 0;
int Rsignalstate = 0;
int lastsignalstate = 0;
int start;
void setup()
{
WiFi.mode(WIFI_STA);
if(esp_now_init() != 0)
{
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_add_peer (mac_peer, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
pinMode(leftIN,INPUT_PULLUP);
pinMode(rightIN,INPUT_PULLUP);
pinMode(brakeIN,INPUT_PULLUP);
pinMode(inBuiltLED, OUTPUT);
pinMode(buzzerOUT, OUTPUT);
}
void loop()
{
start = millis();
digitalWrite(inBuiltLED, HIGH);
if(digitalRead(brakeIN) == LOW)
{
transmit = 1;
}
else if(digitalRead(leftIN) == LOW && digitalRead(rightIN) == LOW)
{
transmit = 2;
}
else if(digitalRead(leftIN) == LOW)
{
transmit = 3;
}
else if(digitalRead(rightIN) == LOW)
{
transmit = 4;
}
if (transmit == 1 || transmit == 2 || transmit == 3 || transmit ==4)
{
esp_now_send(mac_peer, (uint8_t *) &transmit, sizeof(transmit));
delay(200);
transmit = 0;
esp_now_send(mac_peer, (uint8_t *) &transmit, sizeof(transmit));
}
while (millis() - start <60000)
{
Lsignalstate = digitalread(leftIN);
Rsignalstate = digitalread(rightIN);
if (Lsignalstate != lastsignalstate || Rsignalstate != lastsignalstate )
{
if (Lsignalstate == LOW || Rsignalstate == LOW)
{
Counter ++;
}
delay(50);
}
if (Counter >=150) /*number of times pin goes high in 1 minute. Arrived by previous program running pin state change detection*/
{
digitalWrite(buzzerOUT, LOW);
Counter = 0;
}
if (Counter <150)
{
Counter = 0;
}
}
}
Thank you for the help.