Controlling Servo Speed's

I wanted to build a product which is a Posture Corrector. It suppose to have push button connected to servo motors, that move when the push button is pressed. The buttons will be places where the back should not be in so the servo motor can be activated. (There is also one button that moves two servo motors.) So I also built a app on MIT app inventor to control the speed of the servo motor. How would I do that with this code? I also wanted to see how would I stop the servo motor after a bit because they were just being turned off and on with the push button. I put a delay for around a second and a half, then just but the servo write to 0.

Here The code:

#include <Servo.h>

// constants won't change

//First Circuit
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin

//Second Circuit
const int BUTTON1_PIN = 6; // Arduino pin connected to button's pin
const int SERVO1_PIN = 10; // Arduino pin connected to servo motor's pin

//Third Circuit
const int BUTTON2_PIN = 5; // Arduino pin connected to button's pin
const int SERVO2_PIN = 11; // Arduino pin connected to servo motor's pin
const int SERVO3_PIN = 12; // Arduino pin connected to servo motor's pin

Servo servo; // create servo object to control a servo
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
Servo servo3; // create servo object to control a servo

// variables will change:
int angle = 0; // the current angle of servo motor

//First Circuit
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button

//Second Circuit
int lastButtonState1; // the previous state of button
int currentButtonState1; // the current state of button

//Thrid Circuit
int lastButtonState2; // the previous state of button
int currentButtonState2; // the current state of button

void setup() {
Serial.begin(9600); // initialize serial

pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);

servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);
servo3.attach(SERVO3_PIN);

servo.write(angle);
servo1.write(angle);
servo2.write(angle);
servo3.write(angle);

currentButtonState = digitalRead(BUTTON_PIN);
currentButtonState1 = digitalRead(BUTTON1_PIN);
currentButtonState2 = digitalRead(BUTTON1_PIN);

}

void loop() {

lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state

if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");

// change angle of servo motor
if(angle == 0)
  angle = 90;
else
if(angle == 90)
  angle = 0;

// control servo motor arccoding to the angle
servo.write(angle);
delay(1500);
servo.write(0);

}

lastButtonState1 = currentButtonState1; // save the last state
currentButtonState1 = digitalRead(BUTTON1_PIN); // read new state

if(lastButtonState1 == HIGH && currentButtonState1 == LOW) {
Serial.println("The button is pressed");

// change angle of servo motor
if(angle == 0)
  angle = 90;
else
if(angle == 90)
  angle = 0;

// control servo motor arccoding to the angle
servo1.write(angle);
delay(1500);
servo1.write(0);

}

lastButtonState2 = currentButtonState2; // save the last state
currentButtonState2 = digitalRead(BUTTON2_PIN); // read new state

if(lastButtonState2 == HIGH && currentButtonState2 == LOW) {
Serial.println("The button is pressed");

// change angle of servo motor
if(angle == 0)
  angle = 90;
else
if(angle == 90)
  angle = 0;

// control servo motor arccoding to the angle
servo2.write(angle);
servo3.write(angle);
delay(1500);
servo2.write(0);
servo3.write(0);

}

}

Please edit your post to add code tags. It would be helpful to press ^T to autoformat the code in the IDE, before copying.

Instructions are in the "How to get the best out of this forum" post, linked at the head of every forum category.

To control servo speed, command incremental target positions, each command followed by a short delay.

Can you give me an example of the command incremental target positions

for (int i = 0; i<91; i++) {
   servo.write(i);
   delay(10);
   }

i suggest structuring to independently recognize button presses, setting servo target positions and effect servo movements using a timer.

a table (structure) can represent the state of each servo: including pin #, current position, target position and speed (time between increments)

that table can be scanned each loop() iteration and each servo position updated toward it's target position after the appriate delay controlling it's speed

a separate table describes the buttons: pin #s and possible actions that affect 1+ servo target positions

by managing servo/buttons separately, buttons can be pressed before previous requests are completed

using tables supports an arbitrary # of buttons and servos

can you give me an example

you are ignoring

editing your first posting

to be good readable code by following this short tutorial
please RE-edit your posting to present your code as a code-section

click on the pencil-icon below your posting and then re-edit your posting following this short tutorial

best regards Stefan

Hi, @harsh_1
Please post your complete code in a new posting.
(Never go back and update earlier posts.)

To add code please click this link;

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

see chap6 of The C Programming Language for an explanation of struct, the use of pointers to struct as well as any other questions.

but please ask questions. i don't know what you know

# include <Servo.h>

struct ButServo {
    // constants
    byte PinBut;
    byte PinServo;
    byte PinServo2;
    int  Ang0;
    int  Ang1;
    unsigned long  msecPeriod;

    // variables
    byte butState;
    int  ang;
    int  angTarg;

    unsigned long  msecLast;

    Servo servo;
    Servo servo2;
};

ButServo tbl [] = {
//    but servo servo2 Ang0 Ang1 period
    {   7,    9,    0,  0,  180,   1 },
    {   6,   10,    0,  0,  180,   5 },
    {   5,   11,   12,  0,  180,  10 },
};

const int Ntbl = sizeof(tbl)/sizeof(ButServo);

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    ButServo *p = & tbl [0];
    for (int n = 0; n < Ntbl; n++, p++)  {

        // check for button press
        byte but = digitalRead (p->PinBut);
        if (p->butState != but)  {
            p->butState = but;
            delay (20);             // debounce

            if (LOW == but)  {      // pressed
                if (p->angTarg == p->Ang0)
                    p->angTarg = p->Ang1;
                else
                    p->angTarg = p->Ang0;
                p->msecLast = msec;
            }
        }

        // check if servo needs to be stepped
        if (p->angTarg != p->ang)  {
            if (msec - p->msecLast >= p->msecPeriod)  {
                p->msecLast = msec;

                if (p->angTarg > p->ang)
                    p->ang++;
                else
                    p->ang--;

                p->servo.write (p->ang);
                if (p->PinServo2)
                    p->servo2.write (p->ang);
            }
        }
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    for (int n = 0; n < Ntbl; n++)  {
        pinMode (tbl [n].PinBut, INPUT_PULLUP);
        tbl [n].butState = digitalRead (tbl [n].PinBut);

        tbl [n].angTarg = tbl [n].ang = tbl [n].Ang0;
        tbl [n].servo.attach (tbl [n].PinServo);
        tbl [n].servo.write  (tbl [n].ang);

        if (0 != tbl [n].PinServo2)  {
            tbl [n].servo2.attach (tbl [n].PinServo2);
            tbl [n].servo2.write  (tbl [n].ang);
        }
    }
}
1 Like

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