Society of Robots - Robot Forum

Software => Software => Topic started by: offy on April 08, 2009, 05:25:07 PM

Title: Move servo 5 degrees from location
Post by: offy 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);
  }
Title: Re: Move servo 5 degrees from location
Post by: Razor Concepts 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.
Title: Re: Move servo 5 degrees from location
Post by: offy 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();
  }
Title: Re: Move servo 5 degrees from location
Post by: Razor Concepts 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?
Title: Re: Move servo 5 degrees from location
Post by: offy on April 08, 2009, 06:29:27 PM
It wont work, I first want to fix this.
Title: Re: Move servo 5 degrees from location
Post by: Razor Concepts on April 08, 2009, 07:21:05 PM
Can you post the whole code?
Title: Re: Move servo 5 degrees from location
Post by: offy 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);
  }
}
Title: Re: Move servo 5 degrees from location
Post by: Razor Concepts 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.
Title: Re: Move servo 5 degrees from location
Post by: offy 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);
  }
}
Title: Re: Move servo 5 degrees from location
Post by: Razor Concepts 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);
  }
}