I am using an Arduino MKR WIFI 1010. I have the code below in the IOT Cloud so it can be controlled by Alexa. When turning it on via Alexa it works fine, it stays on. When turning it off it will turn off but then Alexa will think its turned back on when it hasten, so Alexa thinks its on when its not. So to turn it on again i have to turn it off for a second time even though its already off and then turn it on quickly before Alexa automatically thinks its turned on.
When Alexa turns it on it doesn't do anything but when I click the button for Alexa to turn it on it does turn on.
Here is my code, I have it setup as Read and Write and as a smart Light in the IOT Cloud.
#include "thingProperties.h"
#include <Arduino_MKRENV.h>
#define ledr 2
#define ledb 3
#define trig 5
#define echo 6
void setup() {
Serial.begin(9600);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
pinMode(ledr, OUTPUT);
pinMode(ledb, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, OUTPUT);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
digitalWrite(ledr, HIGH);
digitalWrite(ledb, HIGH);
}
void loop() {
ArduinoCloud.update();
float duration, distance;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) * 0.0344;
if (distance > 3 && distance < 5)
digitalWrite(ledb, HIGH);
if (distance > 30 && distance < 35)
digitalWrite(ledr, HIGH);
}
void onBlindChange(){
if(Blind==1)
{
Blind = "HIGH";
digitalWrite(ledb, HIGH);
digitalWrite(ledr, LOW);
}
if(Blind==0)
{
Blind="LOW";
digitalWrite(ledr, HIGH);
digitalWrite(ledb, LOW);
}
Serial.println(Blind);
}