Programming - BASICS Question

Dear community,

I am a student and we are studying Arduino through very simple programs and applications.
So basically we have to use an ultrasound detector, calculate distances between the detected object and the detector. No bid deal so far, but I encountered a singular issue with a very simple code and even the professor is having gray haris on it, so I rely on you to help me now :slight_smile:

I use a suite of instructions to trigger the ultrasound wave generation, sense the bounce of these, mesure the wave travel time and calculate the distance. Some put a delay between detection, calculation and the Serial.print in order to better see the results. But I don‚Äôt want my radar to stop working for the convenience of the user, so I set up a ‚Äúinc‚ÄĚ which after 50 iteration of the basic procedure will print the results through a test (if inc >=50 then -Print Procedure).

The program just don’t recognize that test.

I thought it could be that im going too fast and need to put delay for the system to acknowledge the value of inc and maybe acknowledge there is a test for the inc value. It doesnt change anything.

I tried to put the Serial.println(inc) before the ¬ę if ¬Ľ. It works but I am now spammed with stroker value in the Serial monitor.

Any help would be appreciated. I do not ask you to do my work as I am going steps further from what is asked from us. I am only willing to understand.

Thank you !

byte pinLed[3] {9,10,11};
byte pinTrig = 1;
byte pinEcho = 13;
float T = 0.0;
float v=340.*pow(10,2)/pow(10,6);
float distance=0.0;
int inc = 0;

void setup()
{
  Serial.begin(9600);
  for (int i=0;i<sizeof(pinLed);i++)
  {
    pinMode(pinLed[i],OUTPUT);
    digitalWrite(pinLed[i],LOW);
  }
  pinMode(pinTrig, OUTPUT);
  pinMode(pinEcho,INPUT);
  Serial.println(" ** System Initialized ** ");
  Serial.println(" ** RADAR MODE ENGAGED ** "); 
}

void loop()
{
  digitalWrite(pinTrig,LOW);
  delayMicroseconds(2);
  digitalWrite(pinTrig,HIGH);
  digitalWrite(pinLed[0],HIGH);
  delayMicroseconds(10);
  digitalWrite(pinTrig,LOW);
  digitalWrite(pinLed[0],LOW);
  T = pulseIn(pinEcho, HIGH);
  distance = (T*v)/2;
  delay(10); 
  if (inc >= 50)
  {
    delay(10);
    Serial.print("Response Time: ");
    Serial.println(T);
    Serial.print("Mesured distance: ");
    Serial.println(distance);
    delay(10);
    inc = 0;
  }
  inc = inc + 1;
    
}

Are you saying you never see "Response Time:" and so on printed in the Serial Monitor?

I tried to put the Serial.println(inc) before the ¬ę if ¬Ľ. It works but I am now spammed with stroker value in the Serial monitor.

I tried that with your code too although I don't have an ultrasonic detector connected, and the values of inc printed make sense as they go from 1 to 50 then the messages from the code dependant on the test of inc is printed like this, after which the value of inc is reset

47
48
49
50
Response Time: 0.00
Mesured distance: 0.00

Are you sure that you posted the code that you are testing ?

The program works as expected for me also.

The program just don't recognize that test.

does this mean you never see any output, or you see it more frequently than expected?

if it's not slowing things down enough, you might try using millis() to print after some time period instead of loop iterations

Yes, I do not see the Response time and Mesured distance phrases and value written in the Serial Monitor.

The code posted is the code I am testing. Meanwhile I tried putting all the void loop content as commentary aside inc incrementation and the "if code part" and it works as intended. The issue must come from the ultrasonic detector and prevent the program from running.

I'll look more into it with step by step Serial.print...

Thank you for your time and answers !

void loop()
{
  /*
  digitalWrite(pinTrig,LOW);
  delayMicroseconds(2);
  digitalWrite(pinTrig,HIGH);
  digitalWrite(pinLed[0],HIGH);
  delayMicroseconds(10);
  digitalWrite(pinTrig,LOW);
  digitalWrite(pinLed[0],LOW);
  T = pulseIn(pinEcho, HIGH);
  distance = (T*v)/2;
  delay(10); 
  */
  if (inc >= 50)
  {
  delay(10);
  Serial.print("Response Time: ");
  Serial.println(T);
  Serial.print("Mesured distance: ");
  Serial.println(distance);
  delay(10);
  inc = 0;
  }
  inc = inc + 1;
  
  
}

Try disconnecting all wiring/sensors/etc. from your Arduino board and then run the original code. Does the problem still occur?

I tried as you said, the program runs fine.

Serial monitor shows:

 ** System Initialized ** 
 ** RADAR MODE ENGAGED ** 
Response Time: 0.00
Mesured distance: 0.00
Response Time: 0.00
Mesured distance: 0.00

I made some testings by my side and isolated the "pulseIn" command as comment while arduino is fully wired. The program goes in the "IF" section. "pulseIn" might interfere with my program and stops it somehow ?

Which Arduino board are you using?

Try connecting only the ultrasonic sensor and then run the program again. What are your results?