Society of Robots - Robot Forum
Software => Software => Topic started 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.
if( val == 'Q' )
{
I have no idea what to put here
head.write(someting);
}
-
You have to keep track of what value you told the servo to go last.
headValue = headValue + 5;
head.write(headValue);
An added value of 5 may not be 5 degrees, depends on the servo.
-
This doesn't work, I am trying to do it with softwareservo
if( val == 'E' )
{
headValue = headValue - 5;
head.write(headValue);
SoftwareServo::refresh();
}
-
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?
-
It wont work, I first want to fix this.
-
Can you post the whole code?
-
#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);
}
}
-
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.
-
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.
#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);
}
}
-
Not sure what you were doing in that... here is code that should work:
#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);
}
}