I wanted to get the boards done as fast as I could so I could get started sooner on the programming since I have little as no experience with programming. My plan was to get the programming of my back as soon as I could and get the servo’s functioning like I wanted, before doing the design of the whole thing.
I started of by modifying Neil’s original code and figure out how it worked. That wasn’t really going too well for me so I moved on to the arduino environment and found a library for servos.
The library is called RBD_Servo and you can find instructions on their side
With great help from Baz I was able to write a code that could demonstrate everything I wanted the servo’s to do.
I did run into some problems and the servo’s were behaving strangely. I first tested the buttons to see if they would connect the pin to the ground and they did. In the code I had to add “INPUT_PULLUP” instead of just “INPUT” to get the buttons functioning properly.
Still they were a little strange and with an logic analyzer I was able to detect the problem.
The servo’s go to a set degrees based on the length of the signal they receive. But they also need a minimum pulse signal to make them move. What I saw in the logic analyzer was that the minimum pulse signal was too long for the servo’s making them think they had to move more than 180 degrees.
Also I had delay in the code that was of no use. I removed the delays and shortened the minimum pulse length to 0,5 ms instead of 1 ms.
Then the servo’s started going where I wanted them to and was able to finish the code.
Since the servo’s were going to be holding gear I had to constantly send updates to them to keep them still in the same place. Here is the code:
// Arduino RBD Servo Library v1.0.1 Example - Move a servo with a 1-2ms pulse and 20ms interval to 90 degrees.
// https://github.com/alextaujenis/RBD_Servo
// Copyright 2015 Alex Taujenis
// MIT License
#include // https://github.com/alextaujenis/RBD_Servo
//telling the program which pins the servos connect to, and minimum pulse lenght
RBD::Servo servoP(6, 500, 2000); //pin 6, 0,5ms - 2ms pulse
RBD::Servo servoT(7, 500, 2000); //pin 7, 0,5ms - 2ms pulse
//giving every button a name, buttonLeft etc.
#define BL 2
#define BR 3
#define BU 0
#define BD 1
void setup() {
servoP.moveToDegrees(90); //servo pan starting point to 90
servoT.moveToDegrees(90); //servo tilt starting point to 90
pinMode(0,INPUT_PULLUP);
pinMode(1,INPUT_PULLUP);
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
}
void loop() {
servoP.update(); //constant updates to keep the servos in place
servoT.update();
//
//
// FOR LEFT
//
//
if (digitalRead(BL) == LOW) {
servoP.moveToDegrees(180);
while (digitalRead(BL) ==LOW){
servoP.update();
servoT.update();
//
// if left and right
//
if (digitalRead(BR) == LOW) {
while ((digitalRead(BL) ==LOW) && (digitalRead(BR) ==LOW)){
servoP.update();
servoT.update();
servoP.moveToDegrees(90);
}
}
}
}
//
//
// FOR RIGHT
//
//
if (digitalRead(BR) == LOW) {
servoP.moveToDegrees(10);
while (digitalRead(BR) ==LOW){
servoP.update();
servoT.update();
//
// if right and left
//
if (digitalRead(BL) == LOW) {
while ((digitalRead(BL) ==LOW) && (digitalRead(BR) ==LOW)){
servoP.update();
servoT.update();
servoP.moveToDegrees(90);
}
}
}
}
//
//
// FOR DOWN
//
//
if (digitalRead(BD) == LOW) {
servoT.moveToDegrees(120);
while (digitalRead(BD) ==LOW){
servoP.update();
servoT.update();
//
// if down and up
//
if (digitalRead(BU) == LOW) {
while ((digitalRead(BU) ==LOW) && (digitalRead(BD) ==LOW)){
servoP.update();
servoT.update();
servoT.moveToDegrees(90);
}
}
}
}
//
//
// FOR UP
//
//
if (digitalRead(BU) == LOW) {
servoT.moveToDegrees(60);
while (digitalRead(BU) ==LOW){
servoP.update();
servoT.update();
//
// if right and left
//
if (digitalRead(BD) == LOW) {
while ((digitalRead(BD) ==LOW) && (digitalRead(BU) ==LOW)){
servoP.update();
servoT.update();
servoT.moveToDegrees(90);
}
}
}
}
}