Atualizar SharpDistance_v0.ino
This commit is contained in:
parent
995e374609
commit
027adae3e0
1 changed files with 56 additions and 35 deletions
|
@ -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
|
#define numReadings 10 // Number of readings to average for the moving average
|
||||||
|
|
||||||
int readings[numReadings]; // Array to store the readings
|
int leftReadings[numReadings]; // Array to store the left sensor readings
|
||||||
int readIndex = 0; // Index of the current reading
|
int rightReadings[numReadings]; // Array to store the right sensor readings
|
||||||
int total = 0; // Sum of the readings
|
int leftReadIndex = 0; // Index of the current left sensor reading
|
||||||
int average = 0; // Average of the readings
|
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() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
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++) {
|
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() {
|
void loop() {
|
||||||
// Subtract the last reading
|
if (digitalRead(confirmationPin) == HIGH) {
|
||||||
total = total - readings[readIndex];
|
// 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
|
// Process right sensor readings
|
||||||
int rawValue = analogRead(sensorPin);
|
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
|
// Print the raw values and the averaged distances to the Serial Monitor
|
||||||
int distance = 123.221814 * exp(-0.005895 * rawValue);
|
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
|
Serial.print("Right Raw Value: ");
|
||||||
total = total + distance;
|
Serial.print(rightRawValue);
|
||||||
|
Serial.print(", Right Distance: ");
|
||||||
|
Serial.print(rightAverage);
|
||||||
|
Serial.println(" cm");
|
||||||
|
|
||||||
// Store the reading in the array
|
// Activate relay if left distance is greater than or equal to right distance
|
||||||
readings[readIndex] = distance;
|
if (leftAverage >= rightAverage) {
|
||||||
|
digitalWrite(relayPin, HIGH);
|
||||||
|
} else {
|
||||||
|
digitalWrite(relayPin, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
// Advance to the next position in the array
|
delay(250); // Approximately 250 ms delay
|
||||||
readIndex = readIndex + 1;
|
} else {
|
||||||
|
digitalWrite(relayPin, LOW); // Ensure the relay is off when not in measurement mode
|
||||||
// If we're at the end of the array, wrap around to the beginning
|
|
||||||
if (readIndex >= numReadings) {
|
|
||||||
readIndex = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue