/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/16d115dd-4728-40fb-8ad2-603997edb0d5
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are
made to the Thing
CloudTemperatureSensor temperature;
CloudRelativeHumidity humidity;
String texto;
CloudLight led;
float ultrasonido;
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.
*/
#include "thingProperties.h"
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT11
//Motor 1 izquierdo
#define PIN_MOTOR_L_1 18 // Pin 14 de L293
#define PIN_MOTOR_L_2 19 // Pin 10 de L293
//Motor 2 derecho
#define PIN_MOTOR_R_3 25 // Pin 7 de L293
#define PIN_MOTOR_R_4 26 // Pin 2 de L293
DHT dht(DHTPIN,DHTTYPE);
const int trigPin = 32;
const int echoPin = 33;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(PIN_MOTOR_L_1, OUTPUT);
pinMode(PIN_MOTOR_L_2, OUTPUT);
pinMode(PIN_MOTOR_R_3, OUTPUT);
pinMode(PIN_MOTOR_R_4, OUTPUT);
digitalWrite(PIN_MOTOR_L_1, LOW);
digitalWrite(PIN_MOTOR_L_2, LOW);
digitalWrite(PIN_MOTOR_R_3, LOW);
digitalWrite(PIN_MOTOR_R_4, LOW);
// Initialize serial and wait for port to open:
// This delay gives the chance to wait for a Serial Monitor without blocking if
none is found
delay(1500);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(4, OUTPUT);
dht.begin();
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
if (millis()-lastMillis > 10000){
DHT_SENSOR_READ();
lastMillis = millis();
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
ultrasonido = duration * SOUND_SPEED/2;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(ultrasonido);
//if (ultrasonido < 10){
//Serial.println("Backward");
//digitalWrite(PIN_MOTOR_R_FWD, LOW);
//digitalWrite(PIN_MOTOR_R_BWD, HIGH);
//digitalWrite(PIN_MOTOR_L_FWD, LOW);
//digitalWrite(PIN_MOTOR_L_BWD, HIGH);
//}
onTextoChange();
}
void DHT_SENSOR_READ(){
float h = dht.readHumidity();
float t = dht.readTemperature();
temperature = t;
humidity = h;
Serial.print("Temperature - "); Serial.println(t);
Serial.print("Humidity - "); Serial.println(h);
void onTextoChange() {
if(texto == "w"){
Serial.println("Forward");
digitalWrite(PIN_MOTOR_L_1, HIGH);// Al activar gira el motor A en sentido
de las agujas del reloj.
digitalWrite(PIN_MOTOR_L_2, LOW);// Al activar gira el motor A en sentido
contra las agujas del reloj.
digitalWrite(PIN_MOTOR_R_3, HIGH);// Al activar gira el motor B en sentido
de las agujas del reloj.
digitalWrite(PIN_MOTOR_R_4, LOW);// Al activar gira el motor B en sentido
contra las agujas del reloj.
}
if(texto == "b"){
Serial.println("Backward");
digitalWrite(PIN_MOTOR_L_1, LOW);
digitalWrite(PIN_MOTOR_L_2, HIGH);
digitalWrite(PIN_MOTOR_R_3, LOW);
digitalWrite(PIN_MOTOR_R_4, HIGH);
if(texto == "r"){
Serial.println("Right");
digitalWrite(PIN_MOTOR_L_1, LOW);
digitalWrite(PIN_MOTOR_L_2, HIGH);
digitalWrite(PIN_MOTOR_R_3, HIGH);
digitalWrite(PIN_MOTOR_R_4, LOW);
}
if(texto == "l"){
Serial.println("Left");
digitalWrite(PIN_MOTOR_L_1, HIGH);
digitalWrite(PIN_MOTOR_L_2, LOW);
digitalWrite(PIN_MOTOR_R_3, LOW);
digitalWrite(PIN_MOTOR_R_4, HIGH);
if(texto == "s"){
Serial.println("Stop ");
digitalWrite(PIN_MOTOR_L_1, LOW);
digitalWrite(PIN_MOTOR_L_2, LOW);
digitalWrite(PIN_MOTOR_R_3, LOW);
digitalWrite(PIN_MOTOR_R_4, LOW);
void onLedChange() {
// Do something
if (led == 1) {
digitalWrite(4, HIGH);
}
else {
digitalWrite(4, LOW);
}
}