Arduino ideas, and help

Affiliate Disclosure: We may receive a commision from some of the links and ads shown on this website (Learn More Here)

tankist

Active Member
#61
Checking into this thread.

...
Look, if people have the ability, the knowledge to use them for whatever purpose they choose and they are beneficial to them then that is great. I just don't see the point in making "some things" more complex than they need to be.
Hi Tony, things sure can be made simplier.
But with programmable controllers the benefit is flexibility and ability to add functionality as you go. Think of the above switch maxhine servo control scenario. Surely there are simplier controllers available that will energize the motor. But you just can't beat arduino's price of sub 5$ per controller (economy of scale at work). Then one day you wake up and decide you want to add a signal protecting the turnout, then you want to add another button to control it from other side of a layout, then you want to pair it with another turnout to better control routes , And at the end you want the entire shabeng with block detection , signalling and control. So instead of rewiring at each step you just modify and add lines of code when need arises.

Now, all of this might not be of interest to you as you find other aspects of hobby more rewarding, and there is nothing wrong with that. But you probably have at least some interest, enough to post in this topic. If the Lack of skill is what's stopping you I just want to say - it is very learnable and doable. I wouldn't call it easy , but it is certainly not a rocket science and for our hobby it's a very worthy skill to invest time into - return on investment is amazing. Many excellent guides available to those far from anything programming related.

Best regards
 

Rabman

Active Member
#63
Ok for my first lesson: Is as sketch the code, or program that make the controller do the outputs on the layout? how is it installed, and do you need a computer by the layout for this? Sorry, but I have a million questions!
I have been researching this for about a month now. There is an excellent step by step tutorial on YouTube. You can run JMRI on a raspberry Pi to control the layout through an apple app or android app. You can also use a computer. The tutorial below shows you how to do it on the Pi. It is an excellent video.

 

Rabman

Active Member
#64
A computer is only necessary to load the sketch into the Arduino. After that, the controller is stand alone, taking inputs from sensors, and issuing commands to the outputs... servos, lighting, locomotives.
This is not the case unless you full integrate all the controls into the Arduino. You need JMRI to select locos, interface with the DCC decoders on the Locomotives. The Arduino DCC++ does not have all the CV coding in it directly, it just passes through what JMRI tells it. To run JMRI you need a computer or Raspberry Pi connected to the USB port of the Arduino.

For Servo controlled turnouts, you do not need any computer. You can drive the servos through a motor shield, with input push buttons feeding in to the digital inputs of the Arduino. Completely stand alone once the sketch is uploaded to it through the USB port at setup. You can push the button, flip the turnouts with the servo, then turn on/off LEDs on a yard board, etc. All possible without a computer.

I have not gotten to the point of whether I can run DCC++ sketch and my turnouts servos and LEDs on the same Arduino or not. I bought 3 in case. 2 Mega 2560s and one UNO.
 

Rabman

Active Member
#65
Thanks for the link but I've tried that one. I think the Uno doesn't like VarSpeedServo as the .h header. I can do servo.h but I always get an error with VarSpeedServo.h
You may wish to wish to increment the position the Servo moves too in a control loop, with a delays. Increase the delay to slow it down.

For example:

9E4A3763-76B3-43F1-A5B7-DC46769B249D.jpeg
 
#66
This is not the case unless you full integrate all the controls into the Arduino. You need JMRI to select locos, interface with the DCC decoders on the Locomotives. ...


I have not gotten to the point of whether I can run DCC++ sketch and my turnouts servos and LEDs on the same Arduino or not. I bought 3 in case. 2 Mega 2560s and one UNO.
True. All the Arduino is doing is supplying the track with the DCC signal and power thru PWM.
Mind you, I am still not even a novice, or even novice trainee concerning, but I seem to have read that the servos/leds/signals can be controlled thru the I2C via 'breakout boards' controlling up to 16 slaves per, AND they can be daisy chained up to 16 boards, limited by addresses.
 

Rabman

Active Member
#67
True. All the Arduino is doing is supplying the track with the DCC signal and power thru PWM.
Mind you, I am still not even a novice, or even novice trainee concerning, but I seem to have read that the servos/leds/signals can be controlled thru the I2C via 'breakout boards' controlling up to 16 slaves per, AND they can be daisy chained up to 16 boards, limited by addresses.
I am not aware of the capabilities of the 12C serial port. Being a serial port, as long as what is on the other end, it should be able to talk to each other similar to USB.

I bought a motor shield to drive to servos through the 12C port.

https://www.amazon.ca/gp/aw/d/B0797JK4RW/ref=ya_st_dp_summary?ie=UTF8&psc=1
 
#70
You guys are way ahead of me. You can bet I will be tapping your knowledge!
Not really. I have acquired the hardware, but have not yet applied any of this. I'm just a long study I guess. We will get through this together.
Soon as I can clear some of my other sh** up, I'm gonna attempt 'Blink', just to make sure I have a decent IDE.
 
#71
Thanks Olie,

That's good stuff there! This has the possibility to be another hobby within the hobby. I can see my wife rolling her eyes now. Looks like Amazon carries a bunch of Arduino stuff. This will be fun to tinker with while stuck inside during the harsh Midwest winters.
Ahh, yes. These brutal winters can be "fun". My buddy from Toledo came for a visit between Christmas & New Years. MOH told him to pack warm clothes. When he got here late Christmas morning, he said "you said it was going to be cold, you didn't tell me it was going to be THIS cold!!". I think the "warmest" we got that week was about 5-10 below.
 
Last edited:

Rabman

Active Member
#72
Tony,

I'm not trying to start a "which way is better" or a "my way is better" argument, but - you cannot use a servo without some form of driver. So it comes down to: Do I want to do it myself (or insert "can" for "want") or do I want to pay someone else to do it for me? Almost everything we (electronics knowledgeable model railroaders) are doing ourselves with Arduinos can be bought off the shelf. Here's what it comes down to for me. I can buy an Arduino Mini Pro (just one of the Arduino variations) for $1.40. I can get a servo for $1.07. Or you can buy the equivalent from TAM Valley for $30 to $40. NEITHER WAY IS "BETTER". For me, I love playing with electronics. I've been doing it personally for 52 years and professionally for 25 years. But that's me. It just becomes one more enjoyable aspect of model railroading. Plus it allows me to configure "things" the way I want, not the way some designer things it should be. Okay, call me a control freak, it won't be the first time.

Here's an offer: let me know something you would like to have an Arduino do. I'll put the hardware together, write the sketch, and send it to you with hookup instructions. I currently have about 100 various Arduinos on hand, and maybe 50 to 60 SG90 servos, plus a deep stock of other electronic parts. One of these days AG (After Garden), I'll take a picture of my test bench and post it. Just keep in mind that you (meaning anyone) don't need all the equipment I have.
I wish I had a setup like that!
 

Rabman

Active Member
#73
My starter kit showed up today. Probably won’t get time to mess with it for a couple of weeks, but am super excited to learn something new.

View attachment 29309
I just got my stash of electronics in the mail a few days ago. I think I bought a little larger starter set. Plus I bought two Arduino Mega 2560s, 20 SG-90 servos, 16 channel servo shield and a motor shield. All in for less than the price of a Digitrax Zephyr. All this should power my 12 turnouts and do my DCC++.
 

Rabman

Active Member
#75
Here is the code that will operate 16 turnouts. I tried operating 3 servos with it so far and it works.

Enhancements I need to make are:

1 - put the last servo position in eeprom memory so it doesn’t get lost when you reset or power down.

2 - put a programming mode for controlling the limits of each turnout servo throw to not over travel the servo and put too much pressure on the turnouts points.

3 - figure out what I am going to do with my 3-Way and Double Slip turnouts servo logic.

4 - figure out what I am going to do with the crossover turnouts. For instance push one button to throw both turnouts in the crossover.

5 - do I want to put LEDs on the Mimic board where my push buttons and track schematic is going to be.

6 - do I want the push buttons to just flip the turnouts or also set multiple turnouts for a route.



/*
Use of 16 channel I2C PWM driver board with 4 servo motors
Uses Adafruit PWM library
Uses 16 momentary switches to toggle servos

Coded by Ron B. - 2018
*/

// Include Wire Library for I2C Communications
#include <Wire.h>

// Include libraries to write to the non-volitile memory in the eeprom. Used to store the Servo Positions outside of ram.
// Used address bytes 0 through 15 for turnouts 0 - 16. 0 value byte for turnout in straight direction, 1 byte value for turnout in diverging direction.
#include <EEPROM.h>

// Include Adafruit PWM Library
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Constants declared
const int ButtonPins[16] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45}; //Provided for 16 Servos, actual number used declared below.
const int ServoPins[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //Provided for 16 Servos, actual number used declared below.
const long int MIN_PULSE_WIDTH[16] = {700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700}; //Provided for 16 Servos, actual number used declared below.
const long int MAX_PULSE_WIDTH[16] = {2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300}; //Provided for 16 Servos, actual number used declared below.
const int FREQUENCY = 50;
const int NumberOfServos = 3; //ENTER THE NUMBER OF SERVOS HERE

// Variables
long int DivergingRouteAngle[16] = {45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45}; //Diverging Route Angle.
long int StraightRouteAngle[16] = {140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140}; //Diverging Route Angle.
int switchState[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Holds the status for each switch, High or 1 means the button is pushed
int ServoState[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Holds the position for each servo, 0 means turnout straight, 1 means turnout diverging
int CurrentSwitchState;
int CurrentServoPosition;

void setup() {
pwm.begin();
pwm.setPWMFreq(FREQUENCY);

for (int i = 0; i < NumberOfServos; i++) {

pinMode(ButtonPins, INPUT); // Define digital input pin with pull up resistor for noise reduction
}
}


void loop() { // runs forever to detect control buttons and change switches accordingly

//Loop through all the servo's

for (int i = 0; i < NumberOfServos; i++) {
//Just check the state of the switches
CurrentSwitchState = digitalRead(ButtonPins);


int Val = digitalRead (ButtonPins);

if (CurrentSwitchState != switchState) {
//Switch State has toggled so toggle the servo to the opposite direction
switchState = CurrentSwitchState; //Set the array switch state to equal the current switch state to determine long pushes. Do move servo only once.


//Toggle the servo

//Current Servo Position
CurrentServoPosition = ServoState;


switch (CurrentServoPosition) {

case 0:
// Toggle the servo position to the diverging turnout route.
ToggleServoPosition(1, i); //Set servo i in the diverging route


ServoState = 1; //Update the array with the diverging route.
break;

case 1:
// Toggle the servo position
ToggleServoPosition(0, i); //Set servo i in the straight route


ServoState = 0; //Update the array with the straight route.
break;

default:
//Do nothing
delay(5);
}


delay(250); //delay the loop to account for a long button push.
switchState = 0; //Set the array switch state to HIGH 1 to reset the switch state.
break;
}
}
}


// Toggles the Servo to opposite position
void ToggleServoPosition(int ServoPosition, int CurrentServoPinNumber) {


long int pulse_wide;
long int pulse_width;


switch (ServoPosition) {

case 0:

// Convert to pulse width by using the Diverging Route Angle value from the Constants
pulse_wide = map(DivergingRouteAngle[CurrentServoPinNumber], 0, 255, MIN_PULSE_WIDTH[CurrentServoPinNumber], MAX_PULSE_WIDTH[CurrentServoPinNumber]);
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);


//Control Motor
pwm.setPWM(ServoPins[CurrentServoPinNumber], 0, pulse_width); // NEED TO VERIFY THIS


ServoState[CurrentServoPinNumber] = 1;
break;

case 1:


// Convert to pulse width by using the Straight Route Angle value from the Constants
pulse_wide = map(StraightRouteAngle[CurrentServoPinNumber], 0, 255, MIN_PULSE_WIDTH[CurrentServoPinNumber], MAX_PULSE_WIDTH[CurrentServoPinNumber]);
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);


//Control Motor
pwm.setPWM(ServoPins[CurrentServoPinNumber], 0, pulse_width); // NEED TO VERIFY THIS


ServoState[CurrentServoPinNumber] = 0;
break;


default:


break;
}
}
 

Rabman

Active Member
#78
So a bit of an update on my programming enhancements I the latest version

1 - put the last servo position in eeprom memory so it doesn’t get lost when you reset or power down.

COMPLETED

2 - put a programming mode for controlling the limits of each turnout servo throw to not over travel the servo and put too much pressure on the turnouts points.

DECIDED TO DO THIS MANUALLY BUT DID PUT A PROGRAM LOOP IN THE SOFTWARE BUT IT DOESN’T DO ANYTHING.

3 - figure out what I am going to do with my 3-Way and Double Slip turnouts servo logic.

DECIDED ON TWO SWITCHES FOR THE DOUBLE SLIP, AND 3 GOR THE 3-WAY.

4 - figure out what I am going to do with the crossover turnouts. For instance push one button to throw both turnouts in the crossover.

ONE SWITCH DOES BOTH TURNOUTS

5 - do I want to put LEDs on the Mimic board where my push buttons and track schematic is going to be.

TO BE COMPLETED

6 - do I want the push buttons to just flip the turnouts or also set multiple turnouts for a route.

UNDECIDED SO FAR. MY PANEL IS QUITE SMALL.
 

Olie

Active Member
#79
Nice Rabman. I've been so busy trying to complete other things on the layout, I put the Arduino sketch to the side. Sounds like you've got a good handle on this. I may hit you up for some guidance, if that's OK.
 



ModelRailroadForums.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com

Top