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
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.
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.
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().
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
}
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()).
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.