Enviar arquivos para "/"

This commit is contained in:
Pablo César Galdo Regueiro 2024-06-25 00:27:58 +02:00
parent ddf0336c93
commit 6d1997c2bb

33
SharpDistance_v0.ino Normal file
View file

@ -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
}