Question
Hello, I've been having trouble writing a peice of code for an Arduino Uno in my electromech' engineering class. We are tasked with writing a
Hello, I've been having trouble writing a peice of code for an Arduino Uno in my electromech' engineering class. We are tasked with writing a program that operates a servo to move a lever (in this case a toilet lever) by gathering distance information from a HC-SRO4 Ultrasonic Sensor and/or a button. Ive been trying to set it up to run a test that once it detects someone is there it continues to test again and again until the person leaves, presses the button or 10 minutes is over. Its also suppose to test every 3 seconds. This is what I currently have but once it passes TRUE for the first IF statement, nothing happens and I have no clue why.
#include . // Global variables for defining the Trig, Echo and Button pins. const int trigPin = 10; const int echoPin = 11; const int buttonPin = 2; int buttonState = 0; long duration; int distance, temp; int calculateDistance(); Servo myservo; // Creates a servo object named 'myservo' for controlling the servo motor. void setup() { pinMode(buttonPin, INPUT); // Sets the buttonPin as an Input. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output. pinMode(echoPin, INPUT); // Sets the echoPin as an Input. Serial.begin(9600); myservo.attach(12); // Defines on which pin is the servo motor attached. } void loop() { distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor. buttonState = digitalRead(buttonPin); // Reads the current state of the button (HIGH or LOW). if (buttonState == HIGH) // If button is pressed (HIGH), the toilet will flush once and void loop will restart. This is an 'emergency' button. { myservo.write(20); delay(2000); myservo.write(90); } else myservo.write(90); // If button is not pressed (LOW), the servo will be set back to the origin. if (distance < 30) // If someone is sitting down (distance from sensor to person is less than 30cm) the program will continue to test distance < 30 every 3 seconds for 10 minutes. { int i = 0; while (i <= 200); // (10min * 60sec) / 3sec = 200 tests; hence i <= 200. { Serial.print(distance); buttonState = digitalRead(buttonPin); // If e-button is pressed it will flush and break the loop. if (buttonState == HIGH) { myservo.write(20); delay(2000); myservo.write(90); i = 201; } distance = calculateDistance(); // Re-call distance function every test. if (distance > 30) // Test if person left. { myservo.write(20); delay(2000); myservo.write(90); i = 201; } else if (i == 200) // If 10 minutes has gone by flush the toilet. { myservo.write(20); delay(2000); myservo.write(90); i = 201; } else { delay(3000); // Delay for 3 seconds before re-starting test. i = i + 1; // Add 1 to i for every test until person leaves/button is pressed/10 minutes has passed. } } } else myservo.write(90); // If no-one every sits down the servo will remain in the origin postion. Serial.print(distance); // This prints the distance to the Serial Monitor for helping with setup. Serial.println("cm"); delay(10); } int calculateDistance(){ // This function calculates the distance readings from the Ultrasonic sensor. digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds distance = duration*0.034/2; return distance; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started