Pages: [1]   Go Down
Author Topic: Control a servo with a ir sensor  (Read 1511 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi today i decided  to make a new project including a servo, an ir sensor, a webcam and my arduino uno r3.
So after every thing was setup I started writing the code but instead of moving my webcam(that was atached to my servo) until ir sensor(that was atached to my webcam) detectes something and stop moving at that exact position it just stop until reach 0 degrees. Heres the code:
Code:
#include <Servo.h>


const int analogInPin = A0; 
int sensorValue = 0;               
int servoPin = 9;
int i = 0;

Servo servo;

void setup() {
 
 servo.attach(servoPin);
}

void loop() {
 
  sensorValue = analogRead(analogInPin);           
 
  if(sensorValue < 600){
    for (int i = 0; i<=180; i++){
    servo.write(i);
    delay(10);
   }
  }
  servo.write(i); 
                     
}

thans in advance and sorry for my english.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
instead of moving my webcam(that was atached to my servo) until ir sensor(that was atached to my webcam) detectes something and stop moving at that exact position it just stop until reach 0 degrees.
That's what you've told it to do.

You need to move the servo a little, and take a reading. Then move the servo, and take another reading. Stop moving when you get a reading you like. Although, there may be a better one somewhere else...
Logged

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

thanks for the reply.
when i was trying to solve this made this code:
Code:
#include <Servo.h>


const int analogInPin = A0; 
int sensorValue = 0;               
int servoPin = 9;
int i = 0;

Servo servo;

void setup() {
 
 servo.attach(servoPin);
}

void loop() {
 
  sensorValue = analogRead(analogInPin);           
 
  if(sensorValue < 600){
    for (int i = 0; i<=180; i++){
    servo.write(i);
    delay(15);
    if(analogRead(analogInPin) > 600){
      servo.write(i);
      delay(3000);
  }
    }
   
    for (int i = 180; i>=0; i--){
    servo.write(i);
    delay(15);
    if(analogRead(analogInPin) > 600){
      servo.write(i);
      delay(3000);
    }
   }
}

  }
and i was thinking if there is any way to instead of having
 if(analogRead(analogInPin) > 600){
      servo.write(i);
      delay(3000); i could have something that only allow the code to continue if the analogRead(analogInPin) changes to a below or a higher number.
thanks.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and i was thinking if there is any way to instead of having
 if(analogRead(analogInPin) > 600){
      servo.write(i);
      delay(3000); i could have something that only allow the code to continue if the analogRead(analogInPin) changes to a below or a higher number.
Well, of course there is. I just don't understand what you want to do. "Changes to below"? Below what? "or a higher number"? Higher than what?

analogInPin is a lousy name. Surely, you have not attached an analogIn to that pin. So, why doesn't the name reflect what is actually attached to the pin?

sensorValue is a lousy name, too. There is some specific kind of sensor attached. The name of the variable should reflect that.

Putting each { on a new line, and running Tools + Auto Format, will make your code a lot more readable.
Logged

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

sorry if i was not to specific and i hope you can understand it now.
Quote
I just don't understand what you want to do. "Changes to below"? Below what? "or a higher number"? Higher than what?
my ir sensor reads values from 0 to 1023(like any analogpin input) and when this value is lower than 600, the servo starts rotating and while it is rotating, if the ir sensor dectecs something the sensorValue(0 to 1023) should change and if it changes to an number higher than 600 I wanted to stop the servo until it changes again to a number lower than 600
instead of freezing it for 3 seconds.
Code:

#include <Servo.h>


const int irsensor = A0; //ir sensor pin 
int sensorValue = 0; //ir sensor reading values               
int servoPin = 9; //pin where servo is attached to
int i = 0; //servo degrees

Servo servo; //my Servo is called servo

void setup() {

  servo.attach(servoPin); //attach servo to servo pin
}

void loop() {

  sensorValue = analogRead(irsensor); //ir sensor reading values             

  if(sensorValue < 600) //if sensorValue is lower than 600
  {
    for (int i = 0; i<=180; i++) //servo degrees increses from 0 to 180
    {
      servo.write(i); //servo goes to (i) position
      delay(15); //delay of 15 so it as time to change the position gently
      if(analogRead(irsensor) > 600) //if it detecs something at that position
      {
        servo.write(i); //stay in that position
        delay(3000); //and here instead of waiting 3 seconds, can it only continue whith the code if sensorValue gets lower than 600
      }
    }
    //the same just from 180 to 0 (in the opposite direction)
    for (int i = 180; i>=0; i--)
    {
      servo.write(i);
      delay(15);
      if(analogRead(irsensor) > 600)
      {
        servo.write(i);
        delay(3000);
      }
    }
  }

}



thans and i hope you get the idea now but my english isnt the best.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think that the big problem is how you are waving the servo around. I think you need to get rid of the for loops, that are waving the servo around. Create one while loop, instead, that moves the servo to another position, if the sensor reads less than 600. Don't move, and don't increment the position if the sensor reports more than 600.

The amount to increment by should be changed to a negative value when the position value is at or above the upper limit for the servo, and should be changed to a positive value when the position value is at or below the lower limit for the servo.
Logged

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

thanks for everything solve with this code i  made after you tell me about while loops (i dont even no there existence)
heres the code:
Code:
#include <Servo.h>

const int control = A0;
const int irsensor = A2;             
int servoPin = 9;
int i = 0; 
int a = 0;
int onoff = 0;

Servo servo;

void setup() {

  servo.attach(servoPin);
}

void loop() {         

  onoff = analogRead(control);
  if(onoff < 600)
  {

    for (int i = 50; i<=130; i++)
    {
      servo.write(i);
      delay(25);
      if(analogRead(irsensor) > 90)
      {
        servo.write(i);
        a = i;
        while (analogRead(irsensor) > 90){
          servo.write(a);
        }
      }
    }

    for (int i = 130; i>=50; i--)
    {
      servo.write(i);
      delay(25);
      if(analogRead(irsensor) > 90)
      {
        servo.write(i);
        a = i;
        while (analogRead(irsensor) > 90){
          servo.write(a);
        }
      }
    }

  }
  while( onoff < 600)
  {
    servo.write(100);
  }


}



  }
  while( onoff < 600)
  {
    servo.write(100);
  }


}


thanks again for the reply and support and sorry if waste your time with this as i havent read the hole learning page from arduino site.
Logged

Pages: [1]   Go Up
Jump to: