I'm trying to create a function that sweeps a servo, while reading from an analog sensor every step. When the sensor finds something in front of it inside a specified threshold, it pings the object with an ultrasonic rangefinder.
My problem is, when i call the function, nothing happens.
Also, i want to be able to access the variables "servoreadfinal" and "pingreadfinal" which are set in the function.
Thanks for any help!
#include <Servo.h>
Servo myservo;
int value = 0;
int count = 0;
int d = 5;
int sum = 0;
long duration = 0;
int pingPin = 7;
int servopos =0;
int pingread =0;
int servoposfinal =0;
int pingreadfinal =0;void setup()
{
Serial.begin(9600);
}void loop()
{
sweepuser();
}void sweepuser()
{
for(int f=60; f<=120; f++)
{
myservo.write(f);
delay(d);
if(analogRead(0)>170)
{
count++;
for(int i=0; i<5; i++)
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
sum=(sum+duration);
delay(100);
}
Serial.print(count);
Serial.print(" -OBJECT DETECTED- ");
servopos = 0;
servopos = myservo.read();
pingread = 0;
pingread = (sum/5);
Serial.println(sum/5);
sum = 0;
}
else
{
count =0;
}}
for(int f=120; f>=60; f--)
{
myservo.write(f);
delay(d);
if(analogRead(0)>170)
{
count++;for(int i=0; i<5; i++)
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
sum=(sum+duration);
delay(100);
}Serial.print(count);
Serial.print(" -OBJECT DETECTED- ");
servoposfinal = ((servopos + myservo.read())/2);
pingreadfinal = ((pingread + (sum/5))/2);
Serial.println(sum/5);
sum = 0;
}
else
{
count =0;
}}
}