Arduino coding_servo

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
Luiso123
 
Posts: 10
Joined: Sat Dec 09, 2017 3:44 pm

Arduino coding_servo

Post by Luiso123 »

This is a code for a sunlight tracker using a 180° servo motor but i want to change the servo for a 360° continues servo, can anyone edit this code for me so it could rotate 360 and not 180.

Code: Select all

#include <Servo.h> // include Servo library 

// 180 horizontal MAX
Servo horizontal; // horizontal servo
int servoh = 90;   // 90;     // stand horizontal servo

int servohLimitHigh = 180;
int servohLimitLow = 65;

// 65 degrees MAX
Servo vertical;   // vertical servo 
int servov = 90;    //   90;     // stand vertical servo

int servovLimitHigh = 120;
int servovLimitLow = 15;


// LDR pin connections
//  name  = analogpin;
int ldrlt = 2; //LDR top left - BOTTOM LEFT    <--- BDG
int ldrrt = 3; //LDR top rigt - BOTTOM RIGHT 
int ldrld = 0; //LDR down left - TOP LEFT
int ldrrd = 1; //ldr down rigt - TOP RIGHT

void setup()
{
  Serial.begin(9600);
// servo connections
// name.attacht(pin);
  horizontal.attach(9); 
  vertical.attach(10);
  horizontal.write(180);
  vertical.write(45);
  delay(3000);
}

void loop() 
{
  int lt = analogRead(ldrlt); // top left
  int rt = analogRead(ldrrt); // top right
  int ld = analogRead(ldrld); // down left
  int rd = analogRead(ldrrd); // down rigt
  
  // int dtime = analogRead(4)/20; // read potentiometers  
  // int tol = analogRead(5)/4;
  int dtime = 10;
  int tol = 50;
  
  int avt = (lt + rt) / 2; // average value top
  int avd = (ld + rd) / 2; // average value down
  int avl = (lt + ld) / 2; // average value left
  int avr = (rt + rd) / 2; // average value right

  int dvert = avt - avd; // check the diffirence of up and down
  int dhoriz = avl - avr;// check the diffirence og left and rigt
  
  
  Serial.print(avt);
  Serial.print(" ");
  Serial.print(avd);
  Serial.print(" ");
  Serial.print(avl);
  Serial.print(" ");
  Serial.print(avr);
  Serial.print("   ");
  Serial.print(dtime);
  Serial.print("   ");
  Serial.print(tol);
  Serial.println(" ");
  
    
  if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle
  {
  if (avt > avd)
  {
    servov = ++servov;
     if (servov > servovLimitHigh) 
     { 
      servov = servovLimitHigh;
     }
  }
  else if (avt < avd)
  {
    servov= --servov;
    if (servov < servovLimitLow)
  {
    servov = servovLimitLow;
  }
  }
  vertical.write(servov);
  }
  
  if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
  {
  if (avl > avr)
  {
    servoh = --servoh;
    if (servoh < servohLimitLow)
    {
    servoh = servohLimitLow;
    }
  }
  else if (avl < avr)
  {
    servoh = ++servoh;
     if (servoh > servohLimitHigh)
     {
     servoh = servohLimitHigh;
     }
  }
  else if (avl = avr)
  {
    // nothing
  }
  horizontal.write(servoh);
  }
   delay(dtime);

}
Last edited by adafruit_support_bill on Sat Dec 09, 2017 3:53 pm, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino coding_servo

Post by adafruit_support_bill »

Code: Select all

This is a code for a sunlight tracker using a 180° servo motor but i want to change the servo for a 360° continues servo
That is not possible to do with a continuous rotation 'servo' - since they are not really servos. they have no feedback, so you can't control their position.
https://learn.adafruit.com/adafruit-mot ... ion-servos

User avatar
Luiso123
 
Posts: 10
Joined: Sat Dec 09, 2017 3:44 pm

Re: Arduino coding_servo

Post by Luiso123 »

Then how is this guy getting a servo to move in any direction 360 around?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino coding_servo

Post by adafruit_support_bill »

Then how is this guy getting a servo to move in any direction 360 around?
Which guy and which code? The code you posted was for an actual servo - not a continuous rotation "servo" (which is NOT A SERVO!). Sure, you can get a CR servo to rotate 360 degrees. You just can't control exactly where it stops unless you have some external encoder for feedback.

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Arduino coding_servo

Post by kcl1s »

Luiso123 wrote:Then how is this guy getting a servo to move in any direction 360 around?
The code uses 2 - 180 degree servos in an altitude/ azimuth configuration and can move to any point in a hemisphere (half sphere).

By the way this is a fun project. It can also be used to follow a flashlight indoors. I had the teens I teach make one but had them clean up the code a bit. Let me know if you have questions on the build.

Keith

User avatar
Luiso123
 
Posts: 10
Joined: Sat Dec 09, 2017 3:44 pm

Re: Arduino coding_servo

Post by Luiso123 »

Do u know codding, could u help me edit that code so the motor could rotate 360 around on the horizontal servo and to eliminate the vertical servo from the code, i have no idea how to code, thats the only thing stoping me.

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Arduino coding_servo

Post by kcl1s »

As bill said regular hobby servos only go 180 deg. Continuous run servos are just gear motors in a servo package and you can not control position. If you need positional control at 360 deg. a small geared stepper would work better. https://www.adafruit.com/product/858 You will also need a control board to drive this. Here is a page that does a good job of explaining them and has example code. http://arduino-info.wikispaces.com/SmallSteppers We are happy to help you with questions about the project but I doubt you will get someone to write the code for you here. There are lots of online resources for learning Arduino coding.

Keith

User avatar
Luiso123
 
Posts: 10
Joined: Sat Dec 09, 2017 3:44 pm

Re: Arduino coding_servo

Post by Luiso123 »

But am reading online that u cold control the servo using code.

Now lets try out the continuous rotation servo. Connect the motor to digital port D9. You control the continuous rotation servo by writing a speed to it instead of a position.

User avatar
kcl1s
 
Posts: 1512
Joined: Tue Aug 30, 2016 12:06 pm

Re: Arduino coding_servo

Post by kcl1s »

Luiso123 wrote:But am reading online that u cold control the servo using code.

Now lets try out the continuous rotation servo. Connect the motor to digital port D9. You control the continuous rotation servo by writing a speed to it instead of a position.
Exactly! You are writing a speed to it so it is going to start rotating. To stop the rotating you write 0 speed. By the time it stops you will have overshot your mark and it will have to move back the other way and the process starts over and over again. This will cause an oscillation or jerkiness in the movement. It is not the best motor for the job.

The code you posted is for a 2 dimensional movement with 2 servos and 4 LDRs. What you describe will only use 2 LDRs and one motor. You can get some guidance from that code but the code you need will be totally different.

In another post you mention this is for a school project and you are asking us to write the code for you. That is not going to happen! We are happy to help with problems in code that you learn to write.

Keith

User avatar
Luiso123
 
Posts: 10
Joined: Sat Dec 09, 2017 3:44 pm

Re: Arduino coding_servo

Post by Luiso123 »

I got two questions, do u think this code would do what i need.
And this code is for a alduino demilanove 2009, would this same code work on a alduino UNO .

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino coding_servo

Post by adafruit_support_bill »

I got two questions, do u think this code would do what i need.
The code is designed for 2 common RC servos with 180 degrees of rotation. It will not work as-is with a single continuous rotation 'servo' for 2 reasons:
1 - The code assumes 2 axis of rotation. One or each servo. A single servo has only one axis of rotation. You would need to re-structure the code accordingly.
2. As explained above, you cannot directly control the position of a continuous rotation 'servo'. You can only control its direction and speed. The code would require a substantial re-write to effectively use the analog sensor feedback to position the servo.. https://learn.adafruit.com/adafruit-mot ... ion-servos

And this code is for a alduino demilanove 2009, would this same code work on a alduino UNO .
Yes. Code written for the Duemilanove will run fine on an UNO.

User avatar
Luiso123
 
Posts: 10
Joined: Sat Dec 09, 2017 3:44 pm

Re: Arduino coding_servo

Post by Luiso123 »

For the code to work on the uno i dont have to add anything to it just as it is it would work on an uno bord?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino coding_servo

Post by adafruit_support_bill »

Yes, it should work without changes on the UNO.

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino coding_servo

Post by Franklin97355 »

Luiso123 wrote:For the code to work on the uno i dont have to add anything to it just as it is it would work on an uno bord?
In most cases, yes, but you will need to try it to be sure.

User avatar
Luiso123
 
Posts: 10
Joined: Sat Dec 09, 2017 3:44 pm

Re: Arduino coding_servo

Post by Luiso123 »

If it didnt work do u have any knowledge of what i would need to add to it to make it compatible?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”