Arduino controlling servos help needed

Post all your model railway electronic problems, solutions and discoverys here.
Paul-H
Posts: 146
Joined: Fri Jun 05, 2009 3:00 pm

Arduino controlling servos help needed

Postby Paul-H » Thu Jun 07, 2018 11:12 pm

Hi all

I need some help from someone who understands how to write Arduino Sketches because I don't have a clue :roll:

So I have both UNO and MEGA boards which I use with a number of freely available sketches for things like DCC control DCC++ and for stepper motor control of a Turntable and now I wish to use one to control servos for point control

I have one of these sensor shields which I understand can control 16 servos https://m.ebay.co.uk/itm/2009-Nano-v3-0 ... 3040433870 if that is correct could someone write the sketch for me.

All I will need is servos connected to the shield and each servo switched by a toggle switch with end point adjustment via a simple edit of the sketch , say zero to 180 Deg for the end to end throw which I can adjust in a text editor if I need less than full throw.

If what I want is doable without too much work or time could someone help

I am ok with connections and following instructions but don't have a clue when it comes to writing code, and with the signs of dementia starting I will not be able to learn, so anyone up for the challenge and be willing to help me.

Thanks in advance if anyone can help.

Paul

BuffyMcBuffer
Posts: 68
Joined: Sun Apr 01, 2018 12:59 pm

Re: Arduino controlling servos help needed

Postby BuffyMcBuffer » Fri Jun 08, 2018 7:36 am

Timbersurf and another forum member (forgot his username) are currently helping people in a arduino programming thread

Check it out as the line crossing using servos is currently be covered at the moment so hopefully the coding will be written and an explanation on how is works is up

Paul-H
Posts: 146
Joined: Fri Jun 05, 2009 3:00 pm

Re: Arduino controlling servos help needed

Postby Paul-H » Fri Jun 08, 2018 9:42 am

Thanks

I have added this request to that thread in the hope that someone will see it and take pity on me and help :wink:

paul

User avatar
TimberSurf
Posts: 2163
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: Arduino controlling servos help needed

Postby TimberSurf » Fri Jun 08, 2018 11:23 am

Timbologist is in Australia so his answers tend to be 12 hrs out from UK. :)
We will pick this up in the other thread
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

Buggleskelly
Posts: 350
Joined: Mon Jan 28, 2013 5:50 pm
Location: Basildon, Essex

Re: Arduino controlling servos help needed

Postby Buggleskelly » Sun Jun 10, 2018 11:28 am

Paul-H.
I had the same issue re Arduino sketches. No doubt you have found it is almost impossible to find a pre-written sketch. Even one that you can modify or adjust yourself. I built an N gauge two gate module with two servos.
One suggestion that came up when I asked the same question, was to use one or two of those cheap servo testers. All you need is a simple 5 volt supply.
The only issue may be adding auto detection of a train using infra red or reed switch.

User avatar
TimberSurf
Posts: 2163
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: Arduino controlling servos help needed

Postby TimberSurf » Sun Jun 10, 2018 12:31 pm

Once I have fully built my level crossing and proven the code, i will release it for all.
I have proven it in a simulator, but have yet to find time time to add the sound.

Level Crossing Barrier prelim.jpg


I will also build and program one for swing gates too :)
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

peroni
Posts: 24
Joined: Wed Aug 22, 2007 12:03 am
Location: Australia

Re: Arduino controlling servos help needed

Postby peroni » Mon Jun 11, 2018 5:52 am

Hi Paul,
This should help you out:
This project controls 8 servos with corresponding start and
stop positions using push buttons. The starting positions are
determined by sstart-25,25,25,25,25,25,25,25 and the stop
positions by sstop-160,160,160,160,160,160,160,160
Each push of the corresponding button causes the servo to seek
the opposite position and changes the state of the corresponding
LED, using the pushbutton/LED connections pictured:

Connected pins: servos - spins - 2, 3, 4, 5, 6, 7, 8, 9;
pushbuttons - pins - 11, 12, 14 (A0), 15 (A1), 16 (A2), 17 (A3), 18 (A4), 19 (A5)

Here is the Sketch:

// MRH 8 Switched 8 Servos with Pushbuttons & LED Indicators
// G. Bunza 2016
//
#include <SoftwareServo.h>
#define numpins 8 // Number of Servos
SoftwareServo servo[numpins] ;

int i,k,l;
int pins [ ]= {11,12,14,15,16,17,18,19}; // Pushbutton/LED pins
int spins [ ]= {2,3,4,5,6,7,8,9}; // Servo pins
int sstart [ ]= {25,25,25,25,25,25,25,25}; // Servo Start Positions
int sstop [ ]= {160,160,160,160,160,160,160,160}; // Servo Stop Positions
boolean pinval [ ]= { true,true,true,true,true,true,true,true }; // Servo State
//
void setup() {
for (i=0; i<numpins; i++) {
servo[i].attach (spins[i]); // Set up servo controls
servo[i].write(sstart[i]); // Set each servo to start position
}
pinMode(pins[i],OUTPUT); // Initialize PB/LED pins
digitalWrite(pins[i],HIGH);
}
void loop() {
SoftwareServo::refresh(); // Must Refresh the Internal Servo Control

for (i=0; i<numpins; i++) {
pinMode (pins[i],INPUT_PULLUP);
if (digitalRead(pins[i])==LOW) { // Check if a Pushbutton is pressed
pinval[i]=!pinval[i]; // Assume only one at a time
if (pinval[i]==true) servo[i].write(sstart[i]);
else servo[i].write(sstop[i]);
while (digitalRead(pins[i])==LOW) {SoftwareServo::refresh(); } // Wait until PB is released
}
pinMode(pins[i],OUTPUT); // Now go back to maintaining LEDs
digitalWrite(pins[i],pinval[i]);
}
}
Attachments
Arduino Servos.jpg
Switch with led.jpg
Switch with led.jpg (17.19 KiB) Viewed 371 times

Paul-H
Posts: 146
Joined: Fri Jun 05, 2009 3:00 pm

Re: Arduino controlling servos help needed

Postby Paul-H » Tue Jun 12, 2018 2:19 pm

Thanks for that, will give it a go when all the parts arrive.

Paul

Paul-H
Posts: 146
Joined: Fri Jun 05, 2009 3:00 pm

Re: Arduino controlling servos help needed

Postby Paul-H » Wed Jun 13, 2018 2:09 pm

Getting an error loading that sketch, although its probably me not understanding how to add it

Could you add it as a zip file so I can have a full copy to upload

Thanks

Paul

Paul-H
Posts: 146
Joined: Fri Jun 05, 2009 3:00 pm

Re: Arduino controlling servos help needed

Postby Paul-H » Wed Jun 13, 2018 10:04 pm

So assuming that this is the scetch contents, how do I convert it to something the Arduino IDE will load and compile successfully.

// MRH 8 Switched 8 Servos with Pushbuttons & LED Indicators
// G. Bunza 2016
//
#include <SoftwareServo.h>
#define numpins 8 // Number of Servos
SoftwareServo servo[numpins] ;

int i,k,l;
int pins [ ]= {11,12,14,15,16,17,18,19}; // Pushbutton/LED pins
int spins [ ]= {2,3,4,5,6,7,8,9}; // Servo pins
int sstart [ ]= {25,25,25,25,25,25,25,25}; // Servo Start Positions
int sstop [ ]= {160,160,160,160,160,160,160,160}; // Servo Stop Positions
boolean pinval [ ]= { true,true,true,true,true,true,true,true }; // Servo State
//
void setup() {
for (i=0; i<numpins; i++) {
servo[i].attach (spins[i]); // Set up servo controls
servo[i].write(sstart[i]); // Set each servo to start position
}
pinMode(pins[i],OUTPUT); // Initialize PB/LED pins
digitalWrite(pins[i],HIGH);
}
void loop() {
SoftwareServo::refresh(); // Must Refresh the Internal Servo Control

for (i=0; i<numpins; i++) {
pinMode (pins[i],INPUT_PULLUP);
if (digitalRead(pins[i])==LOW) { // Check if a Pushbutton is pressed
pinval[i]=!pinval[i]; // Assume only one at a time
if (pinval[i]==true) servo[i].write(sstart[i]);
else servo[i].write(sstop[i]);
while (digitalRead(pins[i])==LOW) {SoftwareServo::refresh(); } // Wait until PB is released
}
pinMode(pins[i],OUTPUT); // Now go back to maintaining LEDs
digitalWrite(pins[i],pinval[i]);
}
}

I tried copy and paste, putting in its own directory etc but keep getting a fail on compiling?

Thanks for any help with this

Paul

peroni
Posts: 24
Joined: Wed Aug 22, 2007 12:03 am
Location: Australia

Re: Arduino controlling servos help needed

Postby peroni » Wed Jun 13, 2018 11:14 pm

Hi Paul,

Just copy the sketch file, use you mouse to highlight it, then paste it into the arduino program window, when you compile it, it will ask you to save it and you can call it anything you like.
Attachments
Arduino window.jpg
Arduino window.jpg (103.49 KiB) Viewed 288 times

Paul-H
Posts: 146
Joined: Fri Jun 05, 2009 3:00 pm

Re: Arduino controlling servos help needed

Postby Paul-H » Thu Jun 14, 2018 8:58 am

Hi

I tried that that and got the following error message, can you see anything obvious in that message

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"

D:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware D:\Program Files (x86)\Arduino\hardware -tools D:\Program Files (x86)\Arduino\tools-builder -tools D:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries D:\Program Files (x86)\Arduino\libraries -libraries D:\My Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10805 -build-path C:\Users\pshar\AppData\Local\Temp\arduino_build_252412 -warnings=none -build-cache C:\Users\pshar\AppData\Local\Temp\arduino_cache_564700 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=D:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=D:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=D:\Program Files (x86)\Arduino\hardware\tools\avr -verbose D:\My Documents\Arduino\sketch_jun14c\sketch_jun14c.ino
D:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware D:\Program Files (x86)\Arduino\hardware -tools D:\Program Files (x86)\Arduino\tools-builder -tools D:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries D:\Program Files (x86)\Arduino\libraries -libraries D:\My Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10805 -build-path C:\Users\pshar\AppData\Local\Temp\arduino_build_252412 -warnings=none -build-cache C:\Users\pshar\AppData\Local\Temp\arduino_cache_564700 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=D:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=D:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=D:\Program Files (x86)\Arduino\hardware\tools\avr -verbose D:\My Documents\Arduino\sketch_jun14c\sketch_jun14c.ino
Using board 'uno' from platform in folder: D:\Program Files (x86)\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: D:\Program Files (x86)\Arduino\hardware\arduino\avr
Detecting libraries used...
"D:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-ID:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-ID:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\pshar\AppData\Local\Temp\arduino_build_252412\sketch\sketch_jun14c.ino.cpp" -o "nul"
"D:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-ID:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-ID:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\pshar\AppData\Local\Temp\arduino_build_252412\sketch\sketch_jun14c.ino.cpp" -o "C:\Users\pshar\AppData\Local\Temp\arduino_build_252412\preproc\ctags_target_for_gcc_minus_e.cpp"
D:\My Documents\Arduino\sketch_jun14c\sketch_jun14c.ino:12:28: fatal error: SoftwareServo.h: No such file or directory

#include <SoftwareServo.h>

^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Uno.

peroni
Posts: 24
Joined: Wed Aug 22, 2007 12:03 am
Location: Australia

Re: Arduino controlling servos help needed

Postby peroni » Thu Jun 14, 2018 10:58 am

Going by your printout, it looks like it is not finding the library files, when you install the Arduino program
it puts the arduino files in (windows10) Users\NAME\Documents\or My Documents\arduino
then the libraries are located in (windows10) Users\NAME\Documents\or My Documents\arduino\Libraries

i have attached the library files in a zip file, just unzip it and place the folder in the library folder then start the Arduino program and load
the sketch


Graham
Attachments
SoftwareServo.zip
(4.1 KiB) Downloaded 5 times
SoftwareServo.zip
(4.1 KiB) Downloaded 4 times

Paul-H
Posts: 146
Joined: Fri Jun 05, 2009 3:00 pm

Re: Arduino controlling servos help needed

Postby Paul-H » Thu Jun 14, 2018 1:39 pm

That did the trick, thanks for the help

Paul


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 3 guests