Question
I'm on an Arduino Uno and I'm trying to fix a problem I have but I can't seem to figure it out. I had it
I'm on an Arduino Uno and I'm trying to fix a problem I have but I can't seem to figure it out. I had it working before and the code is exactly the same as I had it before. I'm making a pet food dispenser and every time the servo turns, food will come out and into the bowl. There is a ping sensor is pointing into the bowl and whenever the ping sensor detects that there is enough food and is less than 5cm from the sensor it will stop activating the servo. BUT! whenever the servo spins, the ping sensor seems to actually pause or stop working. Once the servo stops spinning the ping sensor will work again but when it starts up again, the servo will spike up to a high cm number and then go back to normal. This spike in the sensor causes the servo to actually spin again and dispense more food even when the bowl is full. Also, as soon as I put my hand in front of the ping sensor to remove some food, the ping sensor will spike up to about 312 cm causing food to be dispense when I don't want it to and then it'll go back to normal. How do I get the ping sensor to still sense while the servo spins? I feel that if I can get the ping sensor to run at all times even when the servo spins.. then maybe all my problems will be fixed. Here is my code below. Much help would be appreciated! It's driving me nuts!
#include
Servo myservo;
const int servo = 9; const int button = 12; const int ledPin = 13; const int pingPin = 8; const int BowlFull = 5; int buttonVal = false;
void setup() { Serial.begin(9600); myservo.attach(servo); }
void loop() { long duration, inches, cm;
pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);
// convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration);
buttonVal = digitalRead(button); if(buttonVal == LOW) { if (cm > BowlFull) { //if (inches > safezone) { myservo.write(30); delay(1000); myservo.write(180); delay(1000); } }
Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();
delay(100); } long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are 73.746 // microseconds per inch (i.e. sound travels at 1130 feet per second). // This gives the distance travelled by the ping, outbound and return, // so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the object we // take half of the distance travelled. return microseconds / 29 / 2; }
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