I need help with me project. I\'m on an Arduino Uno. I need my servo to keep act
ID: 3602793 • Letter: I
Question
I need help with me project. I'm on an Arduino Uno. I need my servo to keep activating until the parallax ping sensor senses something in front of it 4cm or less. I'm making a food dispenser and the food will dispense when the servo is activated. Once the food is filled up and the ping sensor senses it about 4 cm's away it will stop. Would be nice if someone could modify add to my code to make this happen. Thanks
#include <Servo.h>
Servo myservo;
const int servoPin = 9;
const int buttonPin = 12;
const int ledPin = 13;
const int pingPin = 7;
void setup() {
Serial.begin(9600);
myservo.attach(servoPin);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
myservo.write(180);
delay(1000);
myservo.detach();
}
void loop() {
int buttonVal = digitalRead(buttonPin);
if(buttonVal == LOW) {
myservo.attach(servoPin);
myservo.write(30);
delay(2000);
myservo.write(180);
delay(1500);
myservo.detach();
delay(1000);
}
delay(13);
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);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
Explanation / Answer
#include <Servo.h>
Servo myservo;
const int servoPin = 9;
const int buttonPin = 12;
const int ledPin = 13;
const int pingPin = 7;
void setup() {
Serial.begin(9600);
}
void loop() {
int buttonVal = digitalRead(buttonPin);
if(buttonVal == LOW) {
myservo.attach(servoPin);
myservo.write(30);
delay(2000);
myservo.write(180);
delay(1500);
myservo.detach();
delay(1000);
}
delay(13);
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);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
if(cm>safezone)
{
digitalWrite(pingPin,HIGH);
delayMicroseconds(2);
digitalWrite(pingPin,LOW);
delayMicroseconds(5);
}
else
{
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
delay(100);
}
long microsecondsTocentimeter(long microseconds)
{
return microseconds/5/2;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.