Code isnt working at all

hello my code isnt working
its supposed to detect the object with a sonar detector and light the red or green light up
the closer it is the faster it beeps
the code is

const int trigpin= 8;

const int echopin= 7;

int red=13;

int green=12;

long duration;

int distance;

void setup()

{

pinMode(trigpin,OUTPUT);

pinMode(echopin,INPUT);

pinMode(red,OUTPUT);

pinMode(green,OUTPUT);

Serial.begin(9600);

}

void loop()

{

digitalWrite(trigpin,HIGH);

delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration=pulseIn(echopin,HIGH);
distance = duration*0.034/2;
Serial.println(distance);
if (trigp==HIGH) {
  void loop1();

}
void loop1() {
  if(distance>40) {
  digitalWrite(green,HIGH);
  digitalWrite(red,LOW);
} else if (distance<=30) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(2000);
  digitalWrite(red,LOW);
  delay(2000);
  digitalWrite(red,HIGH);
} else if (distance<=20) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(1000);
  digitalWrite(red,LOW);
  delay(1000);
  digitalWrite(red,HIGH);
} else if (distance<=10){
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
}
}

ive seen multiple forums and tried it but it wont work pls help

Not a good start, I'm afraid.
Where you posted your first question:

There are many subforums here, including one about programming problems. Perhaps a kindly moderator will move your post to that forum for you.

thank you for telling me if you mind which one do i put it in?

Take this little bit of nonsense out:

  if (trigpin == HIGH) {
    void loop1();
  } else {
    void loop1();
  }

Because trigpin is an output, and besides, you're testing the value of the pin number, not the state of the pin. And, you do the same thing either way, so???
Oh, and void... is not how you call a fn, it's how you declare it.

1 Like

alr

as for loop1()

void loop1() {
  if (distance > 40) {
  } else if (distance <= 30) {
  } else if (distance <= 20) {
  } else if (distance <= 10) {
  }
}

Take 5 test values. 45, 35, 25, 15, 5, and trace your logic as it is expressed. What really happens? I stripped out the writes, because until you understand the logic, well...

I moved your topic to an appropriate forum category @taglagban.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I made your project in Wokwi simulation:

When the simulation is running, click on the UltraSonic Sensor to change the distance.

I suggest to move all the code from loop1() function in the loop() function.
Functions are good, to make the software more modular and easier to maintain. But in this case it might be better to move all the code in the loop().

Could have gone to Programming Questions, but I see it's been moved to Project Guidance. That's okay, too.

it was like that before i changed it but the code was delayed by alot

should the code be like this?

const int trigpin= 8;

const int echopin= 7;

int red=13;

int green=12;

long duration;

int distance;

void setup()

{

pinMode(trigpin,OUTPUT);

pinMode(echopin,INPUT);

pinMode(red,OUTPUT);

pinMode(green,OUTPUT);

Serial.begin(9600);

}

void loop()

{

digitalWrite(trigpin,HIGH);

delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration=pulseIn(echopin,HIGH);
distance = duration*0.034/2;
Serial.println(distance);

}
void loop1() {
  if(distance>45) {
  digitalWrite(green,HIGH);
  digitalWrite(red,LOW);
} else if (distance<=35) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(2000);
  digitalWrite(red,LOW);
  delay(2000);
  digitalWrite(red,HIGH);
} else if (distance<=25) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(1000);
  digitalWrite(red,LOW);
  delay(1000);
  digitalWrite(red,HIGH);
} else if (distance<=15){
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(500);
  digitalWrite(red,LOW);
  delay(500);
  digitalWrite(red,HIGH);
} else if (distance<=5){

  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);

}
}

I was not suggesting you change your thresholds. Your logic was flawed. are >= and <= somewhat confusing to you? Think then, about this:

void loop1() {
  if (distance > 40) { //anything greater than 40
  } else if (distance >= 30) {//anything greater than or equal to 30(but not greater than 40)
  } else if (distance >= 20) {//and so on
  } else if (distance >= 10) {//and so on
  } else {//and so on down to 0; you might want to catch 0 separately, by the way, as it has special meaning
  }

ok i did the code but it still wont work
i think i need to start the void loop1 code first then it may work

void loop1() {
  if(distance>45) {
  digitalWrite(green,HIGH);
  digitalWrite(red,LOW);
} else if (distance>=35) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(2000);
  digitalWrite(red,LOW);
  delay(2000);
  digitalWrite(red,HIGH);
} else if (distance>=25) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(1000);
  digitalWrite(red,LOW);
  delay(1000);
  digitalWrite(red,HIGH);
} else if (distance>=15){
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(500);
  digitalWrite(red,LOW);
  delay(500);
  digitalWrite(red,HIGH);
} else{

  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);

}
}

please, post complete code.

sorry

const int trigpin= 8;

const int echopin= 7;

int red=13;

int green=12;

long duration;

int distance;

void setup()

{

pinMode(trigpin,OUTPUT);

pinMode(echopin,INPUT);

pinMode(red,OUTPUT);

pinMode(green,OUTPUT);

Serial.begin(9600);

}

void loop()

{

digitalWrite(trigpin,HIGH);

delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration=pulseIn(echopin,HIGH);
distance = duration*0.034/2;
Serial.println(distance);

}
void loop1() {
  if(distance>45) {
  digitalWrite(green,HIGH);
  digitalWrite(red,LOW);
} else if (distance>=35) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(2000);
  digitalWrite(red,LOW);
  delay(2000);
  digitalWrite(red,HIGH);
} else if (distance>=25) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(1000);
  digitalWrite(red,LOW);
  delay(1000);
  digitalWrite(red,HIGH);
} else if (distance>=15){
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(500);
  digitalWrite(red,LOW);
  delay(500);
  digitalWrite(red,HIGH);
} else{

  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);

}
}

in loop(), you must call loop1(), else how does the code get there?(this was my fault, the code I suggested you remove, above, should have been replaced with a simple call to loop1(), or as @Koepel suggested, you could incorporate it's code directly in loop()).

like this?

const int trigpin= 8;

const int echopin= 7;

int red=13;

int green=12;

long duration;

int distance;

void setup()

{

pinMode(trigpin,OUTPUT);

pinMode(echopin,INPUT);

pinMode(red,OUTPUT);

pinMode(green,OUTPUT);

Serial.begin(9600);

}

void loop()

{

digitalWrite(trigpin,HIGH);

delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration=pulseIn(echopin,HIGH);
distance = duration*0.034/2;
Serial.println(distance);

if(distance>45) {
  digitalWrite(green,HIGH);
  digitalWrite(red,LOW);
} else if (distance>=35) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(2000);
  digitalWrite(red,LOW);
  delay(2000);
  digitalWrite(red,HIGH);
} else if (distance>=25) {
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(1000);
  digitalWrite(red,LOW);
  delay(1000);
  digitalWrite(red,HIGH);
} else if (distance>=15){
  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);
  delay(500);
  digitalWrite(red,LOW);
  delay(500);
  digitalWrite(red,HIGH);
} else{

  digitalWrite(green,LOW);
  digitalWrite(red,HIGH);

}
}

Yes. Now how does it work? Tell us what it doesn't do that you wanted, and what it does that you didn't want. "doesn't work" won't be useful.

it is a lil delayed but it works perfectly fine thank you for your help

Well, you do have delay(nnn) in the code, so you should expect delays. Other than those, I don't see why it would 'delay', so again, we need to understand what you're saying here.