Skip to main content

Coche teledirigido

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

Variables

Le añadimos tres variables más :

    velocidad tipo entero Read&Write giro tipo entero Read&Write 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 :

      Línea 2 #include "Arduino_Alvik.h" para que incluya la libería de manejo del robot Línea 4 Creamos un objeto alvik Arduino_Alvik alvik; Línea 6 Creamos una variable tipo array de 5 elementos para almacenar las distancias que lee el sensor de distancia float distances[5]; Línea 15 arrancamos el objeto alvik  alvik.begin(); Línea 41 que el alvik se mueva según la velocidad y el giro alvik.drive(velocidad,giro); Línea 42 leemos el array de distancias alvik.get_distance(distances[0], distances[1], distances[2], distances[3], distances[4]); Línea 43 de todas las distancias, sólo nos importa la 2 distancia=distances[2];
      #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
      }