I am using IOT cloud with esp32. The bottom most function is used for used for the current situation.
CloudLight light;
CloudSwitch cls_btn;
CloudSwitch opn_btn;
bool button;
bool button2;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#define L_EN 12
#define R_EN 13
#define L_PWM 14
#define R_PWM 27
#define lock1_relay 26
#define light_relay 18
#define open_lim1 22
#define close_lim1 33
#define L_EN2 5
#define R_EN2 17
#define L_PWM2 16
#define R_PWM2 4
int counter;
#include "thingProperties.h"
void setup()
{
Serial.begin(9600);
pinMode(lock1_relay, OUTPUT);
pinMode(light_relay, OUTPUT);
pinMode(L_EN, OUTPUT);
pinMode(R_EN, OUTPUT);
digitalWrite(L_EN, LOW);
digitalWrite(R_EN, LOW);
ledcAttachPin(L_PWM, 0);
ledcAttachPin(R_PWM, 1);
ledcSetup(0, 12000, 8); // 12 kHz PWM, 8-bit resolution
ledcSetup(1, 12000, 8);
ledcWrite(0, 0);
ledcWrite(1, 0);
pinMode(L_EN2, OUTPUT);
pinMode(R_EN2, OUTPUT);
digitalWrite(L_EN2, LOW);
digitalWrite(R_EN2, LOW);
ledcAttachPin(L_PWM2, 2);
ledcAttachPin(R_PWM2, 3);
ledcSetup(0, 12000, 8); // 12 kHz PWM, 8-bit resolution
ledcSetup(1, 12000, 8);
ledcWrite(2, 0);
ledcWrite(3, 0);
pinMode(open_lim1, INPUT_PULLUP);
pinMode(close_lim1, INPUT_PULLUP);
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void forward(int value)
{
digitalWrite(L_EN, HIGH);
digitalWrite(R_EN, HIGH);
ledcWrite(0, value);
ledcWrite(1, 0);
}
void backward(int value)
{
digitalWrite(L_EN, HIGH);
digitalWrite(R_EN, HIGH);
ledcWrite(0, 0);
ledcWrite(1, value);
}
void stop_1()
{
digitalWrite(L_EN, LOW);
digitalWrite(R_EN, LOW);
ledcWrite(0, 0);
ledcWrite(1, 0);
}
void forward_2(int value)
{
digitalWrite(L_EN2, HIGH);
digitalWrite(R_EN2, HIGH);
ledcWrite(2, value);
ledcWrite(3, 0);
}
void backward_2(int value)
{
digitalWrite(L_EN2, HIGH);
digitalWrite(R_EN2, HIGH);
ledcWrite(2, 0);
ledcWrite(3, value);
}
void stop_2()
{
digitalWrite(L_EN2, LOW);
digitalWrite(R_EN2, LOW);
ledcWrite(2, 0);
ledcWrite(3, 0);
}
void loop()
{
ArduinoCloud.update();
int op_lim1 = digitalRead(open_lim1);
int cl_lim1 = digitalRead(close_lim1);
if(op_lim1 == LOW)
{
stop_1();
delay(750);
digitalWrite(lock1_relay, HIGH);
}
if(cl_lim1 == LOW)
{
stop_1();
delay(750);
digitalWrite(lock1_relay, HIGH);
}
}
/*
Since Button is READ_WRITE variable, onButtonChange() is
executed every time a new value is received from IoT Cloud.
*/
void onButtonChange()
{
if(button == 1)
{
digitalWrite(lock1_relay, LOW);
delay(750);
forward(255);
delay(11750);
for(int i =255; i>=115; i-=10)
{
forward(i);
delay(100);
}
}
}
/*
Since Button2 is READ_WRITE variable, onButton2Change() is
executed every time a new value is received from IoT Cloud.
*/
void onButton2Change()
{
if(button2 == 1)
{
digitalWrite(lock1_relay, LOW);
delay(750);
backward(255);
delay(11500);
for(int i =255; i>=115; i-=10)
{
backward(i);
delay(100);
}
}
}
void onLightChange()
{
if( light == 1)
{
digitalWrite( light_relay, LOW);
}
else if(light == 0)
{
digitalWrite( light_relay, HIGH);
}
}
void onClsBtnChange()
{
}
void onOpnBtnChange()
{
if(opn_btn == 1)
{
counter = 1;
}
if(counter == 1)
{
unsigned long start_time = millis();
digitalWrite(lock1_relay, LOW);
delay(750);
forward_2(255);
forward(255);
if((millis() - start_time) >=5000)
{
stop_2();
}
if((millis() - start_time)>=7000)
{
stop_1();
counter = 0;
}
}
}