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);
}