Atualizar SharpDistance_v0.ino

This commit is contained in:
Pablo César Galdo Regueiro 2024-06-27 12:10:27 +02:00
parent 995e374609
commit 027adae3e0

View file

@ -1,52 +1,73 @@
#define sensorPin A1 // Sharp 2Y0A21 sensor (10 to 80 cm)
#define leftSensorPin A1 // Sharp 2Y0A21 sensor (10 to 80 cm) - Left
#define rightSensorPin A2 // Sharp 2Y0A21 sensor (10 to 80 cm) - Right
#define relayPin 2 // Digital pin for relay
#define confirmationPin 3 // Digital pin for confirmation input
#define numReadings 10 // Number of readings to average for the moving average
int readings[numReadings]; // Array to store the readings
int readIndex = 0; // Index of the current reading
int total = 0; // Sum of the readings
int average = 0; // Average of the readings
int leftReadings[numReadings]; // Array to store the left sensor readings
int rightReadings[numReadings]; // Array to store the right sensor readings
int leftReadIndex = 0; // Index of the current left sensor reading
int rightReadIndex = 0; // Index of the current right sensor reading
int leftTotal = 0; // Sum of the left sensor readings
int rightTotal = 0; // Sum of the right sensor readings
int leftAverage = 0; // Average of the left sensor readings
int rightAverage = 0; // Average of the right sensor readings
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
pinMode(leftSensorPin, INPUT);
pinMode(rightSensorPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(confirmationPin, INPUT);
for (int i = 0; i < numReadings; i++) {
readings[i] = 0; // Initialize the readings array
leftReadings[i] = 0; // Initialize the left sensor readings array
rightReadings[i] = 0; // Initialize the right sensor readings array
}
}
void loop() {
// Subtract the last reading
total = total - readings[readIndex];
if (digitalRead(confirmationPin) == HIGH) {
// Process left sensor readings
leftTotal = leftTotal - leftReadings[leftReadIndex];
int leftRawValue = analogRead(leftSensorPin);
int leftDistance = 123.221814 * exp(-0.005895 * leftRawValue);
leftTotal = leftTotal + leftDistance;
leftReadings[leftReadIndex] = leftDistance;
leftReadIndex = (leftReadIndex + 1) % numReadings;
leftAverage = leftTotal / numReadings;
// Read the raw analog value
int rawValue = analogRead(sensorPin);
// Process right sensor readings
rightTotal = rightTotal - rightReadings[rightReadIndex];
int rightRawValue = analogRead(rightSensorPin);
int rightDistance = 123.221814 * exp(-0.005895 * rightRawValue);
rightTotal = rightTotal + rightDistance;
rightReadings[rightReadIndex] = rightDistance;
rightReadIndex = (rightReadIndex + 1) % numReadings;
rightAverage = rightTotal / numReadings;
// Calculate distance using the formula
int distance = 123.221814 * exp(-0.005895 * rawValue);
// Print the raw values and the averaged distances to the Serial Monitor
Serial.print("Left Raw Value: ");
Serial.print(leftRawValue);
Serial.print(", Left Distance: ");
Serial.print(leftAverage);
Serial.println(" cm");
// Add the current reading to the total
total = total + distance;
Serial.print("Right Raw Value: ");
Serial.print(rightRawValue);
Serial.print(", Right Distance: ");
Serial.print(rightAverage);
Serial.println(" cm");
// Store the reading in the array
readings[readIndex] = distance;
// Activate relay if left distance is greater than or equal to right distance
if (leftAverage >= rightAverage) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
// Advance to the next position in the array
readIndex = readIndex + 1;
// If we're at the end of the array, wrap around to the beginning
if (readIndex >= numReadings) {
readIndex = 0;
delay(250); // Approximately 250 ms delay
} else {
digitalWrite(relayPin, LOW); // Ensure the relay is off when not in measurement mode
}
// Calculate the average
average = total / numReadings;
// Print the raw analog value and the averaged distance to the Serial Monitor
Serial.print("Raw Value: ");
Serial.print(rawValue);
Serial.print(", Distance: ");
Serial.print(average);
Serial.println(" cm");
delay(250); // Approximately 250 ms delay
}