Help me with my code

complete newbie here, I am trying to make it so an Led will only turn on when it is within 100 centimeters of the ultrasonic sensor and a button is pressed. if the code isn't the problem it must be the wiring? is a 100k ohm resistor too much for the button? also i have both the ultrasonic sensor's and the buttons ground pin connected to one row on the side which goes straight to the GND on the arduino board(i don't think that should be an issue right?)

const int triggerPin = 7;
const int echoPin = 8;
bool valid = false;

void setup() {
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(2, OUTPUT);
  pinMode(4, INPUT);

}

void loop() {
  
  long duration, cm, inches;

  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  if(cm < 100){
    bool valid = true;
  }
  else{
    bool valid = false;
  }

  if(valid == true && digitalRead(4) == HIGH){
    digitalWrite(2, HIGH);
  }
  else{
    digitalWrite(2, LOW);
  }


}

long microsecondsToCentimeters(long microseconds){
  return microseconds / 29 / 2;
}

the program is saying there aren't any errors with the code so i am not sure what mistake i am making

i tried it with a 10k resistor and it still didn't work

edit: the ultrasonic sensor is also acting odd :smiling_face_with_tear: it gives the correct distance but instantly follow it up with a zero. it's also slower than the "PING" example arduino provides...

And how does this compute when the duration is ZERO? Instead of printing after the calculation, temporarily print the duration value so you can debug your program.

You are creating and evaluating new variables called "valid" that are not the same as the "valid" global.

Use this...

    valid = true;
  }
  else{
    valid = false;
  }

Read about Arduino's definition of scope...
https://docs.arduino.cc/language-reference/en/variables/variable-scope-qualifiers/scope/

All it can know is that that aren't any syntax errors or other things that keep a sketch from compiling.

If only finding none of those errors meant there were no logical errors in your code!

a7

I tried copying the Ping example on arduino and don't understand how this works very well :sweat_smile: i don't understand a lot of the terms you used but did you mean something like this?


  cm = microsecondsToCentimeters(duration);
  
  duration = pulseIn(echoPin, HIGH);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

No, that's nuts. Put it back the way it was and then re-read what @Paul_KD7HB suggested.

Respond to @PaulRB

This says: the variable cm is assigned the value returned from the function microsecondsToCentimeters(); using the value of duration in its calculation.

The same flow is with all called functions.

The easiest way to use a button is to connect it to the GND and to set its pinMode as INPUT_PULLUP

Here is a simulation of a simplified version of your sketch:

@8uzztronic - You have a few topics started and unfinished. All of them start with excusing yourself for being new, then saying you guessed at a few random things using random power, and all of them create errors.

It would be nice if you would learn.

Start with; Read the help for which you asked. Act on the help. Show results from acting on the help. Explain your observations on how the help brought your project/topic to a close. Learn from the mistakes and the help. Continue doing this with ONLY ONE project until it is complete.

None of your topics are complete. You are burning up people's time and doing nothing with it. Only one step at a time.

thanks for the warning, i will take it into consideration