1sheeld gamepad servo control

Good evening all,
I'm new to programming and electronics and I am working on an app controlled car with a grab attachment using the 1sheeld gamepad.
I am trying to work out two separate parts of the code.
The first part is for the steering control, I would like the steering servo to move while the left or right buttons are held and to return to centre when they are released.
The second part is for the grab, I would like this to move until the button is released. I'll be using the coloured buttons for this part.
So far, I have only managed to get the servo to move to pre- determined angles using the code below. Please can you assist with this.

*/

#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_SHIELD

/* include 1Sheeld library */
#include <OneSheeld.h>

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

/* define servo pin */
int servo_pin = 7;

/* make a servo object */
Servo servo;

void setup() {

/* start communication with 1shield */
OneSheeld.begin();

/* attach servo pin */
servo.attach(servo_pin);

/* initially move servo angel to position 0 degree */
servo.write(0);
delay(1000);
}

void loop() {

/* move servo angle when up is pressed*/
if(GamePad.isUpPressed())
{
servo.write(80);
}

/* move servo angle when right button is pressed */
else if(GamePad.isRightPressed())
{
servo.write(0);
}

/* move servo angle when left button is pressed /
else if(GamePad.isLeftPressed())
{
servo.write(180);
}
else
{
/
Do nothing */
}
}

Does that compile without errors? :slight_smile:

Please edit the post and place the code text inside code tags.

How does the behaviour of the program you posted, differ from what you want it to do? It does claim to do something that looks like button controlled steering.

Hi aarg,
It all compiles and works as it is but it is not exactly how I want it.

I'd like the steering part to move left or right then return to centre when the button is released.
The grab part does not need to return to centre.


*/

#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_SHIELD

/* include 1Sheeld library */
#include <OneSheeld.h>

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

/* define servo pin */
int servo_pin = 7;

/* make a servo object */
Servo servo;

void setup() {

/* start communication with 1shield */
OneSheeld.begin();

/* attach servo pin */
servo.attach(servo_pin);

/* initially move servo angel to position 0 degree */
servo.write(0);
delay(1000);
}

void loop() {

/* move servo angle when up is pressed*/
if(GamePad.isUpPressed())
{
servo.write(80);
}

/* move servo angle when right button is pressed */
else if(GamePad.isRightPressed())
{
servo.write(0);
}

/* move servo angle when left button is pressed <em>/
else if(GamePad.isLeftPressed())
{
servo.write(180);
}
else
{
/Do nothing */
}
}

Okay. What is your exact difficulty in making it like that?

if right button active
{
servo right
}
else if left button active
{
servo left
}
else
{
servo centre
}

seems rather easy...

At the moment it just moves directly to 0, 90, or 180 degrees. I want it to stop at any point when I release the button. For example, when the grab has sufficent pressure I'll left go of the button and it will stay there.

For the steering part I would like it to return to centre when released.

You didn't say that in the beginning. I suggest you catch up and post a complete description of all the desired actions.

The grab? Okay. NM.

You can't code directly for that, because there is no feedback to the MCU on the actual servo position. The only way to do that, is to move it in small steps.

while (servo end position has not been reached) and (grab button is active)
{
move servo one small step
delay
}

Code is in post #4. You never did say what your problem was in coding it...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.