Continous Rotation Servo help with code

Although I am not new to Arduino. I am new to writing code for Arduino. I have been using an Arduino in my DIY CNC machine for quite a while now. And now I've gone and made a humanoid robotic arm for which I need help writing the code.

My Humanoid Arm was made mostly of aluminum and is the whole arm from the shoulder

with 3 degrees of freedom, 2 DOF in the Elbow, 3 DOF in the Wrist,

each finger is actuated by a servo.the thumb has 2 servos.

except for the fingers everything else is Pneumatically driven.

All of which is mechanically complete and functioning.

Now working on getting it all electronically connected.

at the moment (before going any further)
what I have connected to my Genuine arduino uno
4 (fingers no thumb yet)continuous rotation servos each with corresponding button switches(with 10kohm pull-down resisters) and potentiometers(for directional control)
there is a fifth pushbutton switch to move all 4 servos at the same time.

I am aware of the issues concerning continuous rotation servos and positioning.
I know that the servo.h library does not support CRServos.
that being said I would like to adapt it to do so.
For instance the myservo.write(180); line no longer moves the servo just to
180' position rather it moves the servo CW at full speed
90' stops the servo
0' moves the servo CCW at full speed

consequently this gives me both direction and speed control.
What I haven't figured out is how to stop the servo.
maybe some sort of counter / timer in the code.

I just don't understand how to write this into the code.
Believe me I have been trying for days now
If anyone can show me how to write this into my code
I would greatly appreciate any help.

If it turns out this is just not possible to do or adequately reliable with just code.
then limit switches of some sort will be installed
although this will add another level of complexity the project
I would appreciate any ideas on adding the limit switches to the code.

Please take a look at the code.
Here's what I got so far.

Code: [Select]

/*
Controlling servo direction & speed using potentiometers (variable resistor)
modified by Bartolobot on 19 Apr 2016 added 4 continuous rotation servos and
control method with buttons
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;

const int buttonPin = 2;    // the number of the pushbutton pin
const int buttonPin2 = 4;     
const int buttonPin3 = 7;     
const int buttonPin4 = 8;   
const int buttonPin5 = 13;
const byte Relay1 = 6;     // Relay pin for pneumatic valve

int buttonState = 0;       // variable for reading the pushbutton status       
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int potpin = 0;           // analog pin used to connect the potentiometer
int val;                  // variable to read the value from the analog pin
int potpin1 = 1;
int val1;
int potpin2 = 2;
int val2;
int potpin3 = 3;
int val3;   

void setup()
{
 myservo.attach(9);      // attaches the servo on pin 9 to the servo object
 myservo2.attach(10);
 myservo3.attach(11);
 myservo4.attach(12);


 pinMode(buttonPin, INPUT);    // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(buttonPin5, INPUT);
 pinMode(Relay1, OUTPUT);      // initialize the pin as an output:

}

void loop() {

 val = analogRead(potpin);       //reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 180); //scale it to use with the servo(value between 0and180)
 val1 = analogRead(potpin1);
 val1 = map(val1, 0, 1023, 0, 180);
 val2 = analogRead(potpin2);
 val2 = map(val2, 0, 1023, 0, 180);
 val3 = analogRead(potpin3);
 val3 = map(val3, 0, 1023, 0, 180);

 myservo.write(val);          // sets the servo speed according to the scaled value
 myservo2.write(val1);
 myservo3.write(val2);
 myservo4.write(val3);

 buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
 buttonState2 = digitalRead(buttonPin2);
 buttonState3 = digitalRead(buttonPin3);
 buttonState4 = digitalRead(buttonPin4);
 buttonState5 = digitalRead(buttonPin5);

if (buttonState == HIGH) {  //  move myservo
  myservo.write(val);}  // sets the servo speed according to the scaled value
 else {myservo.write(90);}  //  stop the servo.
  
if (buttonState2 == HIGH) {
   myservo2.write(val1);}
 else {myservo2.write(90);}
  
if (buttonState3 == HIGH) {
   myservo3.write(val2);}
 else {myservo3.write(90);}

if (buttonState4 == HIGH) {
  myservo4.write(val3);}
 else {myservo4.write(90);}

 if (buttonState5 == HIGH) {   //move all servos with one button
   myservo.write(val);
    delay(250);               // waits for the servo to get there
   myservo2.write(val);
    delay(500);
   myservo3.write(val);
    delay(820);
   myservo4.write(val);
    delay(1200);}
 else {(val = 90);}
      delay(2500);
      
  digitalWrite(Relay1, HIGH);
    delay(250);
  digitalWrite(Relay1, LOW);
    delay(2000); 
}

You'll need to find the "stop angle" for each servo.
Note that the writeMicroseconds method provides much finer control than the ordinary write method

Please use code tags when posting code

Hi AWOL

The stop angle is here

180' position rather it moves the servo CW at full speed
90' stops the servo
0' moves the servo CCW at full speed

its 90' for all

In my experience, it varies, servo to servo.
YMMV.

Well AWOL,

I have tested this on my Continuous rotation servos and they all do stop at 90'

By the way can u give me an example code of this

Note that the writeMicroseconds method provides much finer control than the ordinary write method

thanks for your help!
AWOL

bartolobot:
Well AWOL,

I have tested this on my Continuous rotation servos and they all do stop at 90'

By the way can u give me an example code of this
thanks for your help!
AWOL

Did you look here ?

I have tested this on my Continuous rotation servos and they all do stop at 90'

So, you KNOW how to stop a not-really-a-servo. What, exactly, is the problem?

Without position feedback, you can tell the servo when to stop but not where.

Posted by AWOL

So, you KNOW how to stop a not-really-a-servo. What, exactly, is the problem?

Does anyone look at my code? the problem is my code lacking full servo control.

I know that 90' stops the servos
Yes I got that part figured out
what I don't understand
is how to implement a timed move in my code then stop the servo after the prescribed amount of time

can someone who knows how to do this please look at my code
and please show me where and how to put this in my code.
please be specific to my code.

Please take a look at the code.
Here's what I got so far.

/*
Controlling servo direction & speed using potentiometers (variable resistor)
modified by Bartolobot on 19 Apr 2016 added 4 continuous rotation servos and
control method with buttons
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;

const int buttonPin = 2;    // the number of the pushbutton pin
const int buttonPin2 = 4;     
const int buttonPin3 = 7;     
const int buttonPin4 = 8;   
const int buttonPin5 = 13;
const byte Relay1 = 6;     // Relay pin for pneumatic valve

int buttonState = 0;       // variable for reading the pushbutton status       
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int potpin = 0;           // analog pin used to connect the potentiometer
int val;                  // variable to read the value from the analog pin
int potpin1 = 1;
int val1;
int potpin2 = 2;
int val2;
int potpin3 = 3;
int val3;   

void setup()
{
 myservo.attach(9);      // attaches the servo on pin 9 to the servo object
 myservo2.attach(10);
 myservo3.attach(11);
 myservo4.attach(12);


 pinMode(buttonPin, INPUT);    // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(buttonPin5, INPUT);
 pinMode(Relay1, OUTPUT);      // initialize the pin as an output:

}

void loop() {

 val = analogRead(potpin);       //reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 180); //scale it to use with the servo(value between 0and180)
 val1 = analogRead(potpin1);
 val1 = map(val1, 0, 1023, 0, 180);
 val2 = analogRead(potpin2);
 val2 = map(val2, 0, 1023, 0, 180);
 val3 = analogRead(potpin3);
 val3 = map(val3, 0, 1023, 0, 180);

 myservo.write(val);          // sets the servo speed according to the scaled value
 myservo2.write(val1);
 myservo3.write(val2);
 myservo4.write(val3);

 buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
 buttonState2 = digitalRead(buttonPin2);
 buttonState3 = digitalRead(buttonPin3);
 buttonState4 = digitalRead(buttonPin4);
 buttonState5 = digitalRead(buttonPin5);

if (buttonState == HIGH) {  //  move myservo
  myservo.write(val);}  // sets the servo speed according to the scaled value
 else {myservo.write(90);}  //  stop the servo.
 
if (buttonState2 == HIGH) {
   myservo2.write(val1);}
 else {myservo2.write(90);}
 
if (buttonState3 == HIGH) {
   myservo3.write(val2);}
 else {myservo3.write(90);}

if (buttonState4 == HIGH) {
  myservo4.write(val3);}
 else {myservo4.write(90);}

 if (buttonState5 == HIGH) {   //move all servos with one button
   myservo.write(val);
    delay(250);               // waits for the servo to get there
   myservo2.write(val);
    delay(500);
   myservo3.write(val);
    delay(820);
   myservo4.write(val);
    delay(1200);}
 else {(val = 90);}
      delay(2500);
     
  digitalWrite(Relay1, HIGH);
    delay(250);
  digitalWrite(Relay1, LOW);
    delay(2000);
}

bartolobot:
Posted by AWOL

Do not attribute to me things I did not write.

Do use code tags when posting code (not quote tags)

what I don't understand
is how to implement a timed move in my code then stop the servo after the prescribed amount of time

Have you looked at the blink without delay example in the IDE, or Robin2's tutorial?

I apolligize for that AWOL,

the quote belongs to

Posted by PaulS

  • Apr 21, 2016, 11:38 am

I have tested this on my Continuous rotation servos and they all do stop at 90'

So, you KNOW how to stop a not-really-a-servo. What, exactly, is the problem?

you said,

Have you looked at the blink without delay example in the IDE, or Robin2's tutorial?

I have looked at the Robin2 tutorial

Demonstration code for several things at the same time

but missed the last part of it, thank you for redirecting me back to it.

void servoSweep() {

     // this is similar to the servo sweep example except that it uses millis() rather than delay()

     // nothing happens unless the interval has expired
     // the value of currentMillis was set in loop()
 
 if (currentMillis - previousServoMillis >= servoInterval) {
       // its time for another move
   previousServoMillis += servoInterval;
   
   servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative

   if (servoPosition <= servoMinDegrees) {
         // when the servo gets to its minimum position change the interval to change the speed
      if (servoInterval == servoSlowInterval) {
        servoInterval = servoFastInterval;
      }
      else {
       servoInterval = servoSlowInterval;
      }
   }

   if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))  {
         // if the servo is at either extreme change the sign of the degrees to make it move the other way
     servoDegrees = - servoDegrees; // reverse direction
         // and update the position to ensure it is within range
     servoPosition = servoPosition + servoDegrees; 
   }
   
       // make the servo move to the next position
   myservo.write(servoPosition);
       // and record the time when the move happened

 }
 
}

I am trying theses Great looking functions in Robin2's
I just have to figure out how to Injected these expressions into my code.
also like the button toggle for the LED's.

Ill let you Know how it go's,
I'm going back to school here thank you.

The last time I did any code programing. Was around 1987 on DOS before windows was around.
so forgive my ignorance.

Code writing update.
I have been working on the code for my Humanoid Arm project.
every chance i get doing the all night'er tonight.
Ive made some progress with my code doing the functions needed for moving the fingers.
and also my comprehension of the language.

I will be adding more to the code.
but if any one's interested here's what I got so far.

any Ideas are welcome.

/*
Controlling servo direction & speed using potentiometers (variable resistor)
modified by Bartolobot on 19 Apr 2016 added 4 continuous rotation servos and
control method with buttons
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;

const int buttonPin = 2;    // the number of the pushbutton pin
const int buttonPin2 = 4;     
const int buttonPin3 = 7;     
const int buttonPin4 = 8;   
const int buttonPin5 = 13;
const byte Relay1 = 6;     // Relay pin for pneumatic valve

int buttonState = 0;// variable for reading the pushbutton status        
int button2State = 0;
int button3State = 0;
int button4State = 0;
int button5State = 0;
int potpin = 0;           // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int val;                  // variable to read the value from the analog pin
int val2;
int val3;
int val4; 
int val5;  

void setup()
{
 myservo.attach(9);      // attaches the servo on pin 9 to the servo object
 myservo2.attach(10);
 myservo3.attach(11);
 myservo4.attach(12);

 pinMode(buttonPin, INPUT);    // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(buttonPin5, INPUT);
 pinMode(Relay1, OUTPUT);      // initialize the pin as an output:
 Serial.begin(9600);
 Serial.println("Bartolobot Servo Control");
}

void loop() {

 val = analogRead(potpin);       //reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 180); //scale it to use with the servo(value between 0and180)
 val2 = analogRead(potpin2);
 val2 = map(val2, 0, 1023, 0, 180);
 val3 = analogRead(potpin3);
 val3 = map(val3, 0, 1023, 0, 180);
 val4 = analogRead(potpin4);
 val4 = map(val4, 0, 1023, 0, 180);

 buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
 button2State = digitalRead(buttonPin2);
 button3State = digitalRead(buttonPin3);
 button4State = digitalRead(buttonPin4);
 button5State = digitalRead(buttonPin5);

if (buttonState == HIGH) {  //  move myservo
  Serial.println ("               Pinky");
  Serial.println (val);
  myservo.write(val);}      // sets the servo speed according to the scaled value}
 else {myservo.write(90);}  //  stop the servo.
 
if (button2State == HIGH) {
   Serial.println ("           Ring");
  Serial.println (val2);
   myservo2.write(val2);}
 else {myservo2.write(90);}
 
if (button3State == HIGH) {
   Serial.println ("      Middle");
  Serial.println (val3);
   myservo3.write(val3);}
 else {myservo3.write(90);}

if (button4State == HIGH) {
   Serial.println ("Index");
  Serial.println (val4);
  myservo4.write(val4);}
 else {myservo4.write(89);}

if (button5State == HIGH) {   //move thumb
  Serial.println ("Thumb");
}

Before you go too much further, it would be a good idea to reflect on your use of arrays.

Hi AWOL,

I wasn't aware that i was using arrays in my code.

can you be more specific so I can answer your comment better?

bartolobot:
Hi AWOL,

I wasn't aware that i was using arrays in my code.

You're not using arrays.
Perhaps you should.

why should I be using Arrays AWOL

not familiar with arrays can you explain why? where? and how?

Why?
Because it'll make your sketch simpler, shorter and easier to maintain and debug.

The first time you notice you're adding numeric suffixes to variable names ("buttonPin2", "potpin2", "myservo2") and writing similar-looking blocks of code, is the time to start asking yourself "Is it time for some arrays?".
In fact, arrays are presently unnecessary for "val2 - 4" and "buttonState2-4", because you don't keep these values beyond a single iteration of "loop()".

Your code is about 100 lines long - I'd expect it to shorten to around 40.
Shorter code means fewer dark corners for the bugs to hide in.

Thank you for the great advice AWOL,

I am now looking at how to accomplish this simplifying of the code with arrays
just need to learn how.

I received my new copy of Arduino Sketches Tools and techniques for programming Wizardry
by James A. Langbridge
in the mail today.

I got a lot of learning to do :o.

I am also considering going to an ArduinoMega , the UNO is just not enough pins
I would like to combine the switches to work on one pin.Just to free up some pins.
But also going to need more room for around 8 Pneumatic Cylinders each with a 12v Pneumatic Solenoid Valves and Relays for each valve.
(some are double solenoid valves)

And then there's sensors that will need to be installed

A lot of complicated work ahead of me
so I do Appreciate any help i can get.

Thanks again AWOL

I just lost the progress i made on my code.
when I closed the arduino IDE I didn't save
so the next time i went to open my sketch it opened up a blank sketch
and its gone!

Good thing I shared a copy of my code here on post #11
Just gotta start from there again.

Having a big issue with my desktop CPU running Win 10 something eating up my hard disk space there's more than 200gigs Gone?

I would like to combine the switches to work on one pin.Just to free up some pins.

Use an analogue pin and a resistor ladder perhaps. Each switch produces a different voltage on the pin which you can test to determine which switch was activated.