0

I am working in TinkerCAD, I want to design and simulate a circuit in which there are following components: 1.)An Arduino board. 2.)A potentiometer 3.)An HC-SRO4 sensor 4.)An LED. So, the sensor should be able to tell if the range that has been set via the potentiometer has any objects in it or not. And if there are one or more objects in that range then the LED should also light up. Also, the user should be able to change the range using potentiometer during simulation. I have attached the code here, but for some reason, I don't feel satisfied. Here is a list of the possible reasons. 1.) For LED lightening up, I had to use another if else , since it was showing an error else used without a previous if if I used LED in the same if else2.) The theoretical range for the sensor I have used is 2cm-400cm but in my circuit, it was not showing the upper range as 400cm. Please tell the possible solutions. Also, can I improve my code and circuit

Here is the code which I used.

const int echoPin = 6;
const int pingPin = 7;

void setup()
{
 Serial.begin(9600);//Starting serial terminal
}

void loop()
{
  int sensorValue = 0;//Setting variable 
  int mappedValue = 0;
  const int potentiometerRange = 0;//setting the buttons to variables to avoid confusion.
  long distance;//declaring a varible in loop because we can change the range during runtime.
  long actualDuration;//...
  long duration;
  pinMode(A0,INPUT);
  pinMode(11,OUTPUT);
  digitalWrite(11,LOW);//
  sensorValue = analogRead(potentiometerRange);//reading the potentiometer value.
  mappedValue = map(sensorValue,0,1023,2,400);//mapping the value accordingly for ultrasonic sensor.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);//to ensure a clean high pulse later
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);//to trigger the ping.
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  pinMode(echoPin,INPUT);//noe echo which tuns low in the time when waves return back.
  duration =  pulseIn(echoPin,HIGH);//time calculated by sensor
  //calculation of distance from any object
  actualDuration = duration/2;//reducing the time in half for the distance is half only.
  distance = 0.0343*actualDuration;//keeping in mid that speed of sound is 343 m/s and pulseIn returns time in microseonds.
  if(mappedValue>=distance)//checking if the object is in range
    Serial.println("YES");
  else
    Serial.println("NO");
  if(mappedValue>=distance)
    digitalWrite(11,HIGH);
  else
    digitalWrite(11,LOW);
   delay(500); // Delay a little bit to improve simulation performance
}

I'm also attaching the picture of my circuit.

enter image description here

5
  • I don't feel satisfied .. maybe because you didn't have your morning coffee .... lol ...
    – jsotola
    Jun 11 '20 at 15:31
  • What does "not showing the upper range as 400cm" mean?
    – chrisl
    Jun 11 '20 at 18:20
  • @chrisl it means that the green area that is there during simulation which is kind of in front of the sensor, there we can look for the range right, it's showing 334.5 cm and not according to the theory as 400 cm. Jun 12 '20 at 18:13
  • @jsotola lolol, pm for these queries lol Jun 12 '20 at 18:15
  • For me that sounds like a Tinkercad problem. Simulators like Tinkercad are very limited, so you might get strange results, when hitting these limits anywhere. When using a real Arduino, you might not see this behavior
    – chrisl
    Jun 12 '20 at 20:00
0

There's not much code there, so not much to optimize. I will however show you how to combine your two ifs.

You have:

if(mappedValue>=distance)//checking if the object is in range
    Serial.println("YES");
  else
    Serial.println("NO");
  if(mappedValue>=distance)
    digitalWrite(11,HIGH);
  else
    digitalWrite(11,LOW);

and I guess you tried this:

if(mappedValue>=distance)//checking if the object is in range
    Serial.println("YES");
    digitalWrite(11,HIGH);
  else
    Serial.println("NO");
    digitalWrite(11,LOW);

That would be correct in Python where the indenting matters (oh how that brings back the PTSD of having to work with COBOL...) but not in C. In C indenting is ignored completely. It's only there for our benefit in reading the flow of code.

Instead to group statements together within an if or an else (or any other conditional or loop structure) you wrap those statements in a block. That is, place { before it and } after it. Thus:

if(mappedValue>=distance)//checking if the object is in range
{
    Serial.println("YES");
    digitalWrite(11,HIGH);
}
  else
{
    Serial.println("NO");
    digitalWrite(11,LOW);
}

Or with my preferred formatting:

if(mappedValue>=distance) { //checking if the object is in range
    Serial.println("YES");
    digitalWrite(11,HIGH);
} else {
    Serial.println("NO");
    digitalWrite(11,LOW);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.