Author Topic: Move servo 5 degrees from location  (Read 1764 times)

0 Members and 1 Guest are viewing this topic.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Move servo 5 degrees from location
« on: April 08, 2009, 05:25:07 PM »
This is some of my code, I want to make it so Servo "head" moves 5 degrees from the location it is at when I press Q.

Code: [Select]
  if( val == 'Q' )
  {
I have no idea what to put here
head.write(someting);
  }

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Move servo 5 degrees from location
« Reply #1 on: April 08, 2009, 06:00:50 PM »
You have to keep track of what value you told the servo to go last.

Code: [Select]
headValue = headValue + 5;
head.write(headValue);

An added value of 5 may not be 5 degrees, depends on the servo.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Move servo 5 degrees from location
« Reply #2 on: April 08, 2009, 06:17:24 PM »
This doesn't work, I am trying to do it with softwareservo

   if( val == 'E' )
  {
   headValue = headValue - 5;
   head.write(headValue);
   SoftwareServo::refresh();
  }

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Move servo 5 degrees from location
« Reply #3 on: April 08, 2009, 06:27:01 PM »
You should add some other things to make sure headValue is in between 0 and 180. Also the headValue is being updated in all the other parts of the code, right?

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Move servo 5 degrees from location
« Reply #4 on: April 08, 2009, 06:29:27 PM »
It wont work, I first want to fix this.

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Move servo 5 degrees from location
« Reply #5 on: April 08, 2009, 07:21:05 PM »
Can you post the whole code?

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Move servo 5 degrees from location
« Reply #6 on: April 08, 2009, 07:26:59 PM »
Code: [Select]
#include <Servo.h>
#include <SoftwareServo.h>
Servo right;
Servo left;
SoftwareServo head;
int pos = 0;
int headValue = 0;
char val;

void setup() {
  right.attach(9);
  left.attach(10);
  head.attach(11);
  Serial.begin(9600);
}

void loop() {

  if( Serial.available() )
  {
    val = Serial.read();
  if( val == 'A' )
  {
    left.write(135);
    right.write(135);
  }
  if( val == 'D' )
  {
    left.write(45);
    right.write(45);
  }
  if( val == 'W' )
  {
    left.write(45);
    right.write(135);
  }
  if( val == 'S' )
  {
    left.write(95);
    right.write(95);
    delay(250);
    left.write(135);
    right.write(45);
  }
  if( val == ' ' )
  {
    left.write(96);
    right.write(96);
  }
  if( val == 'Q' )
  {
   headValue = headValue + 5;
   head.write(headValue);
   SoftwareServo::refresh();
  }
   if( val == 'E' )
  {
   headValue = headValue - 5;
   head.write(headValue);
   SoftwareServo::refresh();
  }
  delay(1);
  }
}

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Move servo 5 degrees from location
« Reply #7 on: April 08, 2009, 07:40:23 PM »
Pressing q should work to increment headValue. So does the code work when you press q?

E will not work since headvalue is at 0 and pressing E will get it down to -5 which is not a valid number for a SoftwareServo.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Move servo 5 degrees from location
« Reply #8 on: April 08, 2009, 08:01:48 PM »
Ok, I fixed the E part, but I now have a problem, it doesn't go the same distance the same time on the edges, instead of 1 move, it takes like 10.

Code: [Select]
#include <Servo.h>
#include <SoftwareServo.h>
Servo right;
Servo left;
SoftwareServo head;
int pos = 0;
int headValue = 0;
int headValue1 = 180;
char val;

void setup() {
  right.attach(9);
  left.attach(10);
  head.attach(11);
  Serial.begin(9600);
}

void loop() {

  if( Serial.available() )
  {
    val = Serial.read();
  if( val == 'A' )
  {
    left.write(135);
    right.write(135);
  }
  if( val == 'D' )
  {
    left.write(45);
    right.write(45);
  }
  if( val == 'W' )
  {
    left.write(45);
    right.write(135);
  }
  if( val == 'S' )
  {
    left.write(95);
    right.write(95);
    delay(250);
    left.write(135);
    right.write(45);
  }
  if( val == ' ' )
  {
    left.write(96);
    right.write(96);
  }
  if( val == 'Q' )
  {
   headValue = headValue + 50;
   head.write(headValue);
   SoftwareServo::refresh();
  }
   if( val == 'E' )
  {
   headValue1 = headValue1 - 50;
   head.write(headValue1);
   SoftwareServo::refresh();
  }
  delay(1);
  }
}

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Move servo 5 degrees from location
« Reply #9 on: April 08, 2009, 08:07:49 PM »
Not sure what you were doing in that... here is code that should work:
Code: [Select]
#include <Servo.h>
#include <SoftwareServo.h>
Servo right;
Servo left;
SoftwareServo head;
int pos = 0;
int headValue = 0;
char val;

void setup() {
  right.attach(9);
  left.attach(10);
  head.attach(11);
  Serial.begin(9600);
}

void loop() {

  if( Serial.available() )
  {
    val = Serial.read();
  if( val == 'A' )
  {
    left.write(135);
    right.write(135);
  }
  if( val == 'D' )
  {
    left.write(45);
    right.write(45);
  }
  if( val == 'W' )
  {
    left.write(45);
    right.write(135);
  }
  if( val == 'S' )
  {
    left.write(95);
    right.write(95);
    delay(250);
    left.write(135);
    right.write(45);
  }
  if( val == ' ' )
  {
    left.write(96);
    right.write(96);
  }
  if( val == 'Q' )
  {
   headValue = headValue + 5;
if(headValue > 180)
headValue = 180;
   head.write(headValue);
   SoftwareServo::refresh();
  }
   if( val == 'E' )
  {
   headValue = headValue - 5;
if(headValue < 0)
headValue = 0;
   head.write(headValue);
   SoftwareServo::refresh();
  }
  delay(1);
  }
}

 


Get Your Ad Here