Code problem on arduino cloud ?

Hi, I'm new to the Arduino Cloud world, and I've been experimenting with communication between two Arduino boards using an IR transmitter and receiver. I have two Arduino Nano boards; one of them is an ESP32 connected to the Arduino Cloud, while the other serves as an IR transmitter, continuously sending the same burst pattern (without using a communication protocol).

On the transmitter Arduino, I'm using pins D2, D3, D4, and D5 in parallel to drive an IR LED. However, I'm encountering an issue on the receiver side. After uploading my code, my Arduino connects to the cloud initially, but it disconnects shortly afterward for no apparent reason. I suspect there might be an infinite loop causing this, but I haven't been able to locate it.

I apologize in advance for the quality of my code; I know it's not ideal. Thank you for any assistance you can provide.

  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/d9e3a021-4287-42e3-962b-ed4b066f53fc 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  String BusNumber;

  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"

#define IR_SENSOR_PIN 2 // Définir la broche pour le capteur IR
#define SIGNAL_DURATION 50 // Durée minimale (en millisecondes) pour laquelle un signal doit être présent pour être détecté

unsigned long lastSignalTime = 0; // Variable pour stocker le temps du dernier signal IR détecté
int lettre = 0;
bool CharCompleted = false;
String TempBusName;
bool StringCompleted = false;

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); 

  Serial.println("setup completed");

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

   
  
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
}

void loop() { // Mettre à jour la connexion Arduino IoT Cloud
  ArduinoCloud.update();

   // Vérifier la connexion IoT Cloud avant de continuer
  if (!ArduinoCloud.connected()) {
    Serial.println("Connexion Arduino IoT Cloud perdue !");
    return; // Sortir de la boucle loop() pour éviter de rester bloqué
  }

  // Lire l'état de la broche IR_SENSOR_PIN
  int irState = 0;
  irState = digitalRead(IR_SENSOR_PIN);

  // Si un signal infrarouge est détecté
  if (irState == 1)
  {
    // Enregistrer le temps du début du signal
    unsigned long startTime = millis();

    // Attendre la fin du signal ou jusqu'à ce que le temps écoulé dépasse SIGNAL_DURATION
    while (digitalRead(IR_SENSOR_PIN)) {
      // Attendre la fin du signal
    }

    // Mesurer la durée du signal
    unsigned long signalDuration = millis() - startTime;

    if (signalDuration >= SIGNAL_DURATION)
    {
      StringCompleted = false;
      lastSignalTime = millis();

      while (!StringCompleted)
      {
        if (digitalRead(IR_SENSOR_PIN))
        {
          lettre++;
          lastSignalTime = millis();
        }

        // Vérifier si plus de 25 ms se sont écoulées
        else if (millis() - lastSignalTime > 25)
        {
          if(lettre != 0)
          {
            TempBusName = String(char(lettre)); // Convertir lettre en char puis en String
            lettre = 0;
          }
          
          else
          {
            StringCompleted = true;
            lastSignalTime = 0;
          }
        }
      }
    }
  }
    // Afficher le numéro de bus via la communication série
  Serial.println(TempBusName);
}

void onBusNumberChange()  {

  BusNumber = TempBusName;
      TempBusName.clear();
  // Add your code here to act upon BusNumber change
}```

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.