Arduino IDE con IoT: Coche teledirigido

Aprovechamos el programa que enciende y apaga un led por Arduino Cloud

Variables

Le añadimos tres variables más :

  1. velocidad tipo entero Read&Write
  2. giro tipo entero Read&Write
  3. distancia tipo float Read

2024-07-11 23_56_28-micosa-alvik Thing _ Arduino Cloud.png

Sketch

En thingProperties.h añade automáticamente estas variables y funciones, no tienes que añadirlas :

void onGiroChange();
void onVelocidadChange();
void onRGBverdeChange();

float distancia;
int giro;
int velocidad;
bool rGBverde;

Pero en la función principal, nosotros vamos a poner el siguiente código :

Nota: la instrucción 41 se han colocado dentro de loop() pero también se podría haber colocado dentro de onGiroChange();
onVelocidadChange();

#include "thingProperties.h"
#include "Arduino_Alvik.h"

Arduino_Alvik alvik;

float distances[5];


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  
  alvik.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();

  /// MI CODIGO
  pinMode(D13,OUTPUT);
}

void loop() {
  ArduinoCloud.update();
  // Your code here 

  alvik.drive(velocidad,giro);
  alvik.get_distance(distances[0], distances[1], distances[2], distances[3], distances[4]);
  distancia=distances[2];
  
  
  
}



/*
  Since RGBverde is READ_WRITE variable, onRGBverdeChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRGBverdeChange()  {
  // Add your code here to act upon RGBverde change
  if (rGBverde){
    digitalWrite(D13,HIGH);
    
  }else{
    digitalWrite(D13,LOW);
    
  }
}

/*
  Since RGBrojo is READ_WRITE variable, onRGBrojoChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRGBrojoChange()  {
  // Add your code here to act upon RGBrojo change
}



/*
  Since Velocidad is READ_WRITE variable, onVelocidadChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onVelocidadChange()  {
  // Add your code here to act upon Velocidad change
}
/*
  Since Giro is READ_WRITE variable, onGiroChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onGiroChange()  {
  // Add your code here to act upon Giro change
}
Dashboard

Creamos un panel de control con:

2024-07-12 00_10_53-ENCENDER-LEDS-ARDUINO-ALVIK Dashboard _ Arduino Cloud.png

Resultado


Revision #8
Created 11 July 2024 22:48:05 by Javier Quintana
Updated 27 March 2025 23:07:42 by Javier Quintana