I created a on/off button a cycle button for a relay. Also sliders for inputting the time on and time off variables for the cycle function. I shared the project which creates a QR code, when I scan the QR on an apple device “voila” it there. When I scan the QR on Android the little balls just twirl for ever. Is my code bad?? Or is this a bug?? Or do I need a new QR for each device?? Sorry total newbie here…
#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
//#include <SimpleTimer.h>
#define EspSerial Serial // Hardware Serial on Mega, Leonardo, Micro...
#define ESP8266_BAUD 115200 // Your ESP8266 baud rate:
char auth[] = "****"; //off blynk project
char ssid[] = "****"; // Your WiFi credentials.
char pass[] = "****"; // Your WiFi credentials.
ESP8266 wifi(&EspSerial);
void fan_Off();
void fan_On();
void fancycle_Off();
void fancycle_On();
void onoffcycle();
boolean FanONState = 0; //initialize variable to hold fan state and set initial state
boolean Fan_cycleState = 0;
boolean SwitchReset = 1;
boolean SwitchReset_cycle = 1;
boolean i = 1;
boolean cycleState = 1;
const int FanONSwitch = 13;
const int RelayPin = 12;
const int Fan_cycleSwitch = 11;
const int systemStatusled = 10;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long interval;
int VsliderState;
int VsliderStateON;
BlynkTimer timer;
WidgetLED VLED(V11);
void setup()
{
EspSerial.begin(ESP8266_BAUD); // Set ESP8266 baud rate
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
delay(10);
pinMode(systemStatusled, OUTPUT);
pinMode(RelayPin, OUTPUT);
pinMode(FanONSwitch, INPUT_PULLUP);
pinMode(Fan_cycleSwitch, INPUT_PULLUP); //added
delay(10);
digitalWrite(RelayPin, HIGH);
digitalWrite(systemStatusled, LOW);
timer.setInterval(100, ButtonCheck);
timer.setInterval(100, Button_cycleCheck);
timer.setInterval(100, updateMillis);
timer.setInterval(500, onoffcycle);
Blynk.syncAll();
}
void loop()
{
Blynk.run();
timer.run();
}
void ButtonCheck() {
boolean SwitchState = (digitalRead(FanONSwitch));
if (!SwitchState && SwitchReset == true) {
if (FanONState) {
fan_On();
} else {
fan_Off();
}
SwitchReset = false;
delay(50);
}
else if (SwitchState) {
SwitchReset = true;
}
}
void ToggleRelay() {
FanONState = !FanONState;
if (FanONState) {
fan_Off();
}
else fan_On();
}
void Button_cycleCheck() {
boolean SwitchState_cycle = (digitalRead(Fan_cycleSwitch));
if (!SwitchState_cycle && SwitchReset_cycle == true) {
if (Fan_cycleState) {
fancycle_On();
} else {
fancycle_Off();
}
SwitchReset_cycle = false;
delay(50);
}
else if (SwitchState_cycle) {
SwitchReset_cycle = true;
}
}
void ToggleRelay_fancycle() {
Fan_cycleState = !Fan_cycleState;
if (Fan_cycleState) {
fancycle_Off();
}
else fancycle_On(); //changed
}
void fan_Off() {
digitalWrite(RelayPin, HIGH);
FanONState = 1;
Blynk.virtualWrite(V5, HIGH);
VLED.off();
fancycle_Off();
digitalWrite(systemStatusled,LOW);
}
void fancycle_Off() {
digitalWrite(RelayPin, HIGH);
Fan_cycleState = 1;
Blynk.virtualWrite(V6, HIGH);
cycleState = 1;
digitalWrite(systemStatusled,LOW);
}
void fan_On() {
digitalWrite(RelayPin, LOW);
FanONState = 0;
Blynk.virtualWrite(V5, LOW);
VLED.on();
digitalWrite(systemStatusled,HIGH);
}
void updateMillis(){
unsigned long currentMillis = millis();
}
void onoffcycle(){
if (cycleState == 0){
unsigned long currentMillis = millis();
if ((unsigned long)currentMillis - previousMillis >= (i ?(VsliderState*60000L):(VsliderStateON*60000L))) { //this makes it run VsliderState one time and VsliderStateOn the next loop
previousMillis = millis();
digitalWrite(RelayPin, i = !i);
}
}else{
digitalWrite(RelayPin, FanONState);
digitalWrite(systemStatusled, !FanONState); //! makes it write the opposite of the state because the StatusLed turns on with write HIGH and Relay turns on with write LOW
}
}
void fancycle_On() {
digitalWrite(RelayPin, HIGH);
Fan_cycleState = 0;
Blynk.virtualWrite(V6, LOW);
digitalWrite(systemStatusled,HIGH);
if (FanONState == 0){
cycleState = 0;
onoffcycle();
}else{
fancycle_Off();
}
}
BLYNK_WRITE(V5) {
int SwitchStatus = param.asInt();
if (SwitchStatus == 2){
ToggleRelay();
}
else if (SwitchStatus){
fan_Off();
}
else fan_On();
}
BLYNK_WRITE(V6) {
int SwitchStatus_fancycle = param.asInt();
if (SwitchStatus_fancycle == 2){
ToggleRelay_fancycle();
}
else if (SwitchStatus_fancycle){
fancycle_Off();
}
else fancycle_On();
}
BLYNK_WRITE(V4) {
VsliderState = param.asInt();
}
BLYNK_WRITE(V3) {
VsliderStateON = param.asInt();
}