I'm trying to turn ON/OFF a DC motor from Arduino Iot Cloud. I have to turn it ON/OFF physically and through the cloud, physically it works well but from the cloud it does nothing. I'm using an Arduino MKR WiFi 1010, a L298N Dual full bridge,9V DC motor and a push buttom. Mi circuit is like this
My code is this
#include "thingProperties.h"
#include <FTDebouncer.h>
#define MOTOR_PIN 2
#define BUTTON_PIN 5
FTDebouncer buttons;
void setup() {
pinMode(MOTOR_PIN, OUTPUT);
buttons.addPin(BUTTON_PIN, LOW);
buttons.init();
setDebugMessageLevel(2);
Serial.begin(9600);
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
}
void loop() {
buttons.update();
ArduinoCloud.update();
onMotorChange();
}
void onPinActivated(int pinNr) {
// do something according to the _pinNR that is triggered. For instance:
Serial.println(pinNr);
toggle = !toggle;
}
void onPinDeactivated(int pinNr) {
// do something according to the _pinNR that is triggered. For instance:
Serial.println(pinNr);
}
void onMotorChange() {
digitalWrite(MOTOR_PIN, toggle || motor);
Serial.print("The motor is ");
if (toggle) {
Serial.println("ON");
}
}
I tried with a LED and it worked very well, but with the motor doesn't. This is the code that Arduino creates when I add things to the board, the part of
thingProperties.h
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
// x-ed id
const char THING_ID[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or
use as
key for WEP)
void onMotorChange();
bool toggle;
bool motor;
void initProperties(){
ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(toggle, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(motor, READWRITE, ON_CHANGE, onMotorChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
Thanks for the help and suggestions. How should I connect the motor or what do I have to change in the code?
toggle
declared?thingProperties.h
, but that's unclear.toggle
is a very generic name for a variable that is declared in a header file. And e.g.ArduinoCloud.update()
could play a serious role. But I have no access to it. As far as I know the program, it makes no sense to me.