Help with project coding, Servo/ Sensor movements

Heya peeps,

it’s been a while since my previous project, but now i’m returning to it and trying to improve its design.

http://arduino.cc/forum/index.php/topic,61531.0.html

In this light, i am wanting to elaborate a slow/small movement by the servos that’re triggered by the Sharp sensor.

Ideally, the user would approach the sensors proximity limits. this would trigger the servo to move from, lets say, 0deg to 45deg BUT at 1 deg per, lets say, 3 seconds. And as the sensor limits are breached, the servo would halt (not back to 0deg, but in the position it was reached).

Perhaps my approach is totally off, correct me if it is, but i’ve tried to integrate the Sweep command into the code that’s been used by the project.

Here it is :

#include <Servo.h>

Servo myservo1; //first servo


int SHARP_IR_PIN = 0; //analog pin used to connect the sensor
int ledPin = 9;  //select pin for the servo
int val = 0;  //variable to read the value from the analog pin
int pos = 60;
int adc_to_cm(int adc) {
  return 14251 * pow(adc,-1.17);
}

void setup()
{
  myservo1.attach(8); //set up the servo as usual first servo
  pinMode(ledPin, OUTPUT);  //variable to store the value coming from the sensor
  Serial.begin(9600);
  myservo1.write(60);
}

void loop() {
  
  int reading = analogRead(SHARP_IR_PIN); // replace this pin with the analog pin your sensor is connected to
  int distance = adc_to_cm(reading);

  char buf[50];
  sprintf(buf, "Distance: %d, voltage: %.02f, adc: %d\r\n", distance, reading * 5.00 / 1024 , reading);
  Serial.print(buf);
  
  while (distance < 35)
  {
  for(pos = 60; pos < 120; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 120; pos>=60; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits 15ms for the servo to reach the position
  }
}

else {
    myservo1.write(0);
  }


delay(10);

}

[one servo has been temporarily removed, as getting the correct movement of one is to be resolved first]

The problems encountered so far is that the Sweep command does trigger with the appropriate small movements, however it is triggered as a single output. i.e. when the <35 sensor limit is active the servo would move from 60 deg to 120deg and back, and do a complete cycle regardless of if the sensor is still active < 35.

I’ve also looked at projects abouts robotics. Sensors/ sensing / servo movement / stop and their relationships is, fundimentally, what i’m trying to explain ^^.

http://arduino.cc/forum/index.php/topic,62257.0.html

In terms of coding, am i pointed in the right direction or perhaps does it really get into the scary language of robotic ^^
either way any suggestions would be greatly appreciated :slight_smile:

PS i’m still very novice at programming, so please do refrain from too crazy vocabulary tehe

Thanks for the time,
Atomicdog

Your issue is simply that you read the distance at the top of loop and then, if it’s < 35, execute the whole servo cycle. While that’s going on, you have no code to read the sensor again and break out of your loops, so it’ll always do a complete cycle.

for(pos = 120; pos>=60; pos-=1)     // goes from 180 degrees to 0 degrees

Clearly, it does no such thing.

Ahh yes from what i want to achieve and described so far. sorry your absolutely right... ^^