I have a problem. Pwease help

// C++ code
//
int ultrasonic (7);
int piezo (0);

void setup()
{
  pinMode(piezo,OUTPUT);
  pinMode(ultrasonic,INPUT);
  Serial.begin(9600);
  
}

void loop()
{
  ultrasonic=digitalRead(7);
  
Serial.println(ultrasonic);  
  
  if (ultrasonic < 30){
    
  piezo = (HIGH);
  } 
}

The piezo just dosen't care and keeps on ringing in my ears even though I kept the object far away. And Yes, I didn't keep it too far so that the sensor can still sense it.
Pwease help

Your code shows a lack of basic understanding of arduino and programming. I recommend reading any guide for dummies to begin with

  • for reading result of ultrasonic you can't just a read a pin... you should calculate the time . See examples for ultrasonic sensor

  • what is this?

If you are using the Serial, you can't connect anything else to pins 0 and 1, so you need to select another pin for piezo

digitalWrite(piezo, HIGH); ?

@asdfghjkwpt Installation and Troubleshooting is for Problems with the Arduino IDE itself NOT your project. It says so in the description of the section.

Therefore I have moved your post here. Please be more careful where you post in future.

welcome to the forum. Start with the examples in the IDE and work from there.

There are many problems here.

  1. I'm inferring from your initialization of the following global variables that these are pin numbers:

    int ultrasonic (7);
    int piezo (0);
    

    However, in your code you seem to be conflating the pin number with the value of the input.
    Is ultrasonic a pin number or an input value? In setup() you use it as a pin number and in loop() you use it as an input value.

  2. You shouldn't use inputs 0 or 1 as they are reserved for serial communications

  3. Why would you read a digital input and expect it to be anything but HIGH or LOW?

      ultrasonic=digitalRead(7);
      Serial.println(ultrasonic);  
      if (ultrasonic < 30)
      {
         piezo = (HIGH);
      }
    
  4. Did you mean digitalWrite(piezo, HIGH)?

    piezo = (HIGH);

Please correct your code according to ToddL1962's suggestion and let us know if it works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.