Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- In this example code, the sonar range finder is connected to analog input pin A0 and digital output pin 2 on the Arduino. The setup() function sets the digital pin to enable the sonar range finder and initializes serial communication. The loop() function reads the analog value from the sonar range finder, converts it to inches, and prints the range to the serial monitor. A short delay is added before taking another reading to prevent the sonar range finder from being read too frequently. Note that the conversion factor from analog value to inches may need to be adjusted depending on the specific model of the sonar range finder being used.
- */
- const int PIN_ANALOG_OUT = A0; // Analog output pin of the sonar range finder
- const int PIN_ENABLE = 2; // Digital pin to enable the sonar range finder
- void setup() {
- pinMode(PIN_ENABLE, OUTPUT); // Set the enable pin as an output
- digitalWrite(PIN_ENABLE, HIGH); // Enable the sonar range finder
- Serial.begin(9600); // Initialize serial communication at 9600 baud
- }
- void loop() {
- int sensorValue = analogRead(PIN_ANALOG_OUT); // Read the analog value from the sonar range finder
- float rangeInches = sensorValue * 0.0098; // Convert the analog value to inches
- Serial.print("Range: "); // Print the range to the serial monitor
- Serial.print(rangeInches);
- Serial.println(" inches");
- delay(100); // Wait for a short time before taking another reading
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement