From 6d1997c2bb181c5e3e9c818ed54dc16a316bf91d Mon Sep 17 00:00:00 2001 From: pcgaldo Date: Tue, 25 Jun 2024 00:27:58 +0200 Subject: [PATCH] Enviar arquivos para "/" --- SharpDistance_v0.ino | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SharpDistance_v0.ino diff --git a/SharpDistance_v0.ino b/SharpDistance_v0.ino new file mode 100644 index 0000000..c60b6b5 --- /dev/null +++ b/SharpDistance_v0.ino @@ -0,0 +1,33 @@ +#define sensorPin A1 // Sharp 2Y0A21 sensor (10 to 80 cm) + +void setup() { + Serial.begin(9600); + pinMode(sensorPin, INPUT); +} + +void loop() { + int rawValue = analogRead(sensorPin); // Read the raw analog value + float volts = rawValue * 0.0048828125; // Convert the raw value to volts + int distance = 26 * pow(volts, -1); // Calculate distance using the formula + + // Print the raw analog value, voltage, and calculated distance to the Serial Monitor + Serial.print("Raw Value: "); + Serial.print(rawValue); + Serial.print(", Volts: "); + Serial.print(volts); + Serial.print(", Distance: "); + Serial.print(distance); + Serial.println(" cm"); + + delay(700); +} + +float readSensorAverage(int pin, int numSamples) { + long sum = 0; + for (int i = 0; i < numSamples; i++) { + sum += analogRead(pin); + delay(10); // Small pause between readings + } + float average = sum / numSamples; + return average * 0.0048828125; // Convert the average value to volts +}