Question: This code is used for my Arduino Uno project. I want the servo to keep activating until' it detects something in front of it that

This code is used for my Arduino Uno project. I want the servo to keep activating until' it detects something in front of it that is at least 2 inches or less. When it is greater than 2 inches then the servo will keep activating again until' the ping sensor detects something less than 2 inches. My project is a food dispenser. Everytime the servo activates, it will constantly dispense food into a bowl until' the ping sensor detects that the food is 2 inches away or less. I do already have a push button to activate the servo but just ignore it.

This is my code as of right now. Please help me adjust my code so it can do what I want it to. Thanks

#include

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);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!