Pages: [1]   Go Down
Author Topic: Sharp IR and Servo  (Read 775 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone, i'm hoping  someone can help me out with this code
and any help will be greatly appreciated.

So basically I want the servo to turn 180 degrees when the sensor comes in contact
at a range below 50 cm and stay there for 2 seconds, then return to 0 degrees.
When I put my hand over the beam it goes 180 degrees but I have to hold it there,
is there any way to have it rotate 180 degrees with out having to hold your hand
over the sensor once it detects the required position?

Code:
#include <Servo.h>

Servo myservo1; //first servo

int SHARP_IR_PIN = 1; //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 adc_to_cm(int adc) {
  return 14251 * pow(adc,-1.17);
}

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

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);
 
  if (distance < 40) {
    myservo1.write(0);
  } else {
    myservo1.write(180);
  }
   
delay(1);

}
Logged

Málaga, Spain
Offline Offline
Edison Member
*
Karma: 40
Posts: 2182
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Add a 2 second delay in the else condition, after moving to the 180 deg position.
Logged

   

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is there any way to have it rotate 180 degrees with out having to hold your hand
over the sensor once it detects the required position?

Put
Code:
while (1);
immediately after the servo write.

Why have you got a 1 millisecond delay at the end of your loop?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

California
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why have you got a 1 millisecond delay at the end of your loop?

by accident I was looking for similar projects to mine and I tweaked the code and missed the delay

So I tried the code and now the servo stays at 180 degrees it doesn't return back to 0 after 2 seconds

Code:
#include <Servo.h>

Servo myservo1; //first servo

int SHARP_IR_PIN = 1; //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 adc_to_cm(int adc) {
  return 14251 * pow(adc,-1.17);
}

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

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);
 
  if (distance < 40) {
    myservo1.write(0);
    while (1);
          }
      else {
    myservo1.write(180);
    delay(1500);
    }
   
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I read "make it stay" but missed the "for two seconds", in which case a "delay (2000);" instead of the "while" would work, but be aware that nothing else will happen in those two seconds.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

California
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sweet it works you guys are awesome!!!!!!!!!!  smiley-grin

So say if I wanted it to detect the required position then wait until it's out of position
then trigger the servo how would that look?
 
Logged

Málaga, Spain
Offline Offline
Edison Member
*
Karma: 40
Posts: 2182
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How do you detect the position? Can you elaborate that a bit?
Logged

   

California
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well at first I followed this but I ended up finding someone that
used a slightly different method for a project very similar to mine so I disregarded this website but it could be useful for you
http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/
Logged

Pages: [1]   Go Up
Jump to: