Help with Ultrasonic Sensors

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Hay Tracker"
  https://create.arduino.cc/cloud/things/bcff5801-10f1-454e-b9e5-50de650b40f6 

  Arduino IoT Cloud Variables description

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

  int HayVal;
  bool status;

  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.
  ___________________________________________________________________________________________
  Hay Tracker V 1.0 (STABLE)
*/

#include "thingProperties.h"
const int toptrig = 13;
const int topecho = 12;
const int medtrig = 11;
const int medecho = 10;
const int bottrig = 9;
const int botecho = 8;
long topduration;
long botduration;
long medduration;
int top;
int med; 
int bot;
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); 

  // 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();
  pinMode(toptrig, OUTPUT);
  pinMode(medtrig, OUTPUT);
  pinMode(bottrig, OUTPUT);
  pinMode(topecho, INPUT);
  pinMode(medecho, INPUT);
  pinMode(botecho, INPUT);
  Serial.println("Hay Tracker V 1.0 (STABLE)");
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  digitalWrite(toptrig, LOW);
  digitalWrite(medtrig, LOW);
  digitalWrite(bottrig, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(toptrig, HIGH);
  digitalWrite(medtrig, HIGH);
  digitalWrite(bottrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(toptrig, LOW);
  digitalWrite(medtrig, LOW);
  digitalWrite(bottrig, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  topduration = pulseIn(topecho, HIGH);
  medduration = pulseIn(medecho, HIGH);
  botduration = pulseIn(botecho, HIGH);
  // Calculating the distance
  top = topduration * 0.034 / 2;
  med = medduration * 0.034 / 2;
  bot = botduration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Top: ");
  Serial.println(top);
  Serial.print("Med: ");
  Serial.println(med);
  Serial.print("bot: ");
  Serial.println(bot);
  if(HayVal <= 1) {
    status = false;
  }
  else {
    status = true;
  }
 //__________________________________________
 if(bot > 30 && med > 30 && top > 30){
   HayVal = 0;
 }
 //________________________________________
 if(bot <= 30 && med > 30 && top > 30){
   HayVal = 1;
 }
 //____________________________________________
 if(bot <= 30 && med <= 30 && top > 30){
   HayVal = 2;
 }
 //________________________________________________
 if(bot <= 30 && med <= 30 && top <= 30){
   HayVal = 3;
 }
 //_____________________________________________
  Serial.print("HayVal: ");
  Serial.println(HayVal);
}



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






/*
  Since Alerts is READ_WRITE variable, onAlertsChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAlertsChange()  {
  // Add your code here to act upon Alerts change
}
Why am I getting straight 0s on "med" and "bot" but not on the top? I know the sensor is not defective because it works on the top. Could it possibly be because my Arduino Nano 33 Pins are defective, even though pins 13, 12 work? Or is it an error in my code?

[quote="loben, post:1, topic:1154778"]
Why am I getting straight 0s on "med" and "bot" but not on the top? I know the sensor is not defective because it works on the top. Could it possibly be because my Arduino Nano 33 Pins are defective, even though pins 13, 12 work? Or is it an error in my code?

you corrupt timing for each sensor. do each sensor readings one after one

The obvious reason is you are NOT allowing enough time between sensor pulsing for the sound and echo from the first sensor to die out. You cannot expect a sound sensor to work properly when you operate them THAT fast. Try allowing 1/2 second between sensor pulses.

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