-3

The code is made to sense were an object is and send out

int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 1;
int const LED1 = 2;
int const LED2 = 3;
int const LED3 = 4;
void setup()
{
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
pinMode(buzzPin, OUTPUT); 
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//                                      Go
distance = (duration/2) / 29.1;
if (distance <= 1000 && distance >=0) { 
digitalWrite(LED1, HIGH);
} else {
digitalWrite(buzzPin, LOW);
digitalWrite(LED1, LOW);
}
delay(60);
}
//                                      SLOW
if (distance <= 50 && distance >=0) { 
digitalWrite(LED2, HIGH);
} else {
digitalWrite(buzzPin, LOW);
digitalWrite(LED2, LOW);
}
delay(60);
}
//                                      STOP
if (distance <= 50 && distance >=0) { 
digitalWrite(buzzPin, HIGH);
digitalWrite(LED3, HIGH);
} else {
digitalWrite(buzzPin, LOW);
digitalWrite(LED3, LOW);
}
delay(60);
}
  • and send out ... send out what to where? – jsotola Jul 31 at 5:56
  • to an LED and or Buzzer sorry I did right it in but for some reason it was cut off – Charles William King Aug 1 at 7:12
1

Try adding in some serial output at important code-parts. If you see that these are getting printed you can be sure that the code gets executed until that point. If it doesn't get executed it could also be a problem with the wiring so be sure to check that everythign is set up properly.

At this part:

...
void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//                                      Go
distance = (duration/2) / 29.1;
...

You are trying to divide an integer (wich can only hold whole numbers) with a real number. Please make sure to use a data type wich can hold more fine-grained results like double or float.

To set up a serial interface you have to add Serial.begin(9600) to you code in the setup() function. Then when you write Serial.println("Your debugging message here") it should be visible in the serial monitor after compiling. (Arduino IDE->Tools->Serial Monitor)

  • 2
    It's not so easy to debug code if you can't compile it at all. Maybe, at some point, he finds out why is everyone using code indentation (and sometimes even C++ Core Guidelines) – KIIV Jul 31 at 7:06
  • I just wanting to know but what would a debugging message be or look like because I am sort of new to Arduino and don't now about too much. – Charles William King Aug 1 at 7:09
  • You just write as message whatever helps you. You could for example write as message: Serial Port Started; Sensor triggered; Entering while loop. It's really just up to you. Whatever helps you figuring out where your arduino is moving arround in your code right now – Cowboy_Patrick Aug 1 at 7:44

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.