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)

RailroadBookstore.com - An online railroad bookstore featuring a curated selection of new and used railroad books. Railroad pictorials, railroad history, steam locomotives, passenger trains, modern railroading. Hundreds of titles available, most at discount prices! We also have a video and children's book section.

ModelRailroadBookstore.com - An online model railroad bookstore featuring a curated selection of new and used books. Layout design, track plans, scenery and structure building, wiring, DCC, Tinplate, Toy Trains, Price Guides and more.

Olie

Active Member
#21
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!
Once you have loaded the sketch into the Arduino, you no longer need the computer. The "program" is stored on the memory chip on the Arduino board.
 
#27
Olie,

Here's my sketch to control a servo and signals. Pick the parts you can use.

// Sketch: Turnout and Signal Control

int ON = HIGH;
int OFF = LOW;
int RouteNormal = 0;
int RouteDiverging = 1;
int IsMoving = 1;
int SwitchMachinePosition = 0;
int FacingSignalGreen = 5;
int FacingSignalYellow = 6;
int OpposingSignalGreen = 7;
int OpposingSignalRed = 8;
int DivergingSignalYellow = 9;
int DivergingSignalRed = 10;

void setup()
{
// Assign the pin inputs and outputs

pinMode(FacingSignalGreen, OUTPUT);
pinMode(FacingSignalYellow, OUTPUT);
pinMode(OpposingSignalGreen, OUTPUT);
pinMode(OpposingSignalRed, OUTPUT);
pinMode(DivergingSignalYellow, OUTPUT);
pinMode(DivergingSignalRed, OUTPUT);
pinMode(2, INPUT);

}

void loop()
{
// put your main code here, to run repeatedly:
SwitchMachinePosition = digitalRead(2);

if (SwitchMachinePosition == RouteNormal)
{
digitalWrite(FacingSignalGreen, ON);
digitalWrite(FacingSignalYellow, OFF);
digitalWrite(OpposingSignalGreen, ON);
digitalWrite(OpposingSignalRed, OFF);
digitalWrite(DivergingSignalYellow, OFF);
digitalWrite(DivergingSignalRed, ON);
}
else
{
digitalWrite(FacingSignalGreen, OFF);
digitalWrite(FacingSignalYellow, ON);
digitalWrite(OpposingSignalGreen, OFF);
digitalWrite(OpposingSignalRed, ON);
digitalWrite(DivergingSignalYellow, ON);
digitalWrite(DivergingSignalRed, OFF);
}
}
 
#29
OOPS, wrong cut and paste. Try this one:

/*
* Servo Turnout Control
*
* This sketch controls an SG90 servo for use as a model railroad
* turnout (switch) machine. It also controls a relay board to
* route power to the frog and rails.
*
*/

#include <Servo.h>

Servo Turnout_Motor;

int THROUGH = 1;

int Toggle_Switch_Pin = 7;
int Servo_Control_Pin = 8;


int Switch_Position = 0;
int Previous_Switch_Position = 3;
int Delay_Count = 0;

// Define a set of variables for the servo positions
int set_through = 1;
int set_diverge = 170;
int current_servo_position = 0;

void setup()
{
// put your setup code here, to run once:

pinMode(Toggle_Switch_Pin, INPUT);

Turnout_Motor.attach(Servo_Control_Pin);
Turnout_Motor.write(set_through);

}

void loop()
{
// put your main code here, to run repeatedly:

Switch_Position = digitalRead(Toggle_Switch_Pin);

if (Switch_Position == Previous_Switch_Position)
{
Turnout_Motor.detach();
}
else
{
Turnout_Motor.attach(Servo_Control_Pin);
if (Switch_Position == THROUGH)
{
Switch_To_Through();
}
else
{
Switch_To_Diverge();
}
Previous_Switch_Position = Switch_Position;
}
}

void Switch_To_Through()
{
for (Delay_Count = current_servo_position; Delay_Count > set_through; Delay_Count -= 1)
{
Turnout_Motor.write(Delay_Count);
delay(25);
}
current_servo_position = set_through;
}

void Switch_To_Diverge()
{
for (Delay_Count = current_servo_position; Delay_Count < set_diverge; Delay_Count += 1)
{
Turnout_Motor.write(Delay_Count);
delay(25);
}
current_servo_position = set_diverge;
}
 

Olie

Active Member
#32
Mark,

Here's what I'm running now. It works, I just want to slow the servo movement. Maybe you can make sense of it.
#include <Servo.h>

Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo

// Use single on-off-on toggle to control servo position.
// First position moves from X to Y degrees
// Second position moves from Y to X degrees

int pos = 0; // variable to store the servo position and starting position
int buttonApin = 4;
int buttonBpin = 8;

bool buttonAstatus; // stores button state
bool buttonBstatus;

void setup()
{
Serial.begin(9600);
Serial.print("Running v2.0 of Servo controlled by toggle and Nano.");
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);

buttonAstatus = digitalRead(buttonApin);
buttonBstatus = digitalRead(buttonBpin);
Serial.println(buttonAstatus);
Serial.println(buttonBstatus);

servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}

void loop()
{
buttonAstatus = digitalRead(buttonApin);
buttonBstatus = digitalRead(buttonBpin);
Serial.println(buttonAstatus);
Serial.println(buttonBstatus);

if (buttonAstatus == LOW)
{
servoLeft.write((digitalRead(10)) ? 60: 165);
servoRight.write((digitalRead(9)) ? 100: 135);
{
Serial.print(pos);
}
}

if (buttonBstatus == LOW)
{
servoLeft.write((digitalRead(10)) ? 165 : 60);
servoRight.write((digitalRead(9)) ? 135 : 100);
{
Serial.print(pos);
}
}
}
 
#33
WOW.... now we have people interested in Arduino.
I've been working on a few things with a group of model railroaders (about 10-15).
We're online most nights to discuss anything about model railroading, which includes Arduino.

We've even done live shows on YouTube where everyone can follow and do at the same time, such as cross gates with servos and lights, lights inside buildings, arc welding, sound and much more.

I build a speedometer for less than $10 instead of paying $100.
But my pride and joy is having built the only working weigh station that really weighs your cars for less than $12.
You can see them on my Facebook page or you can wait and see it on a video from Chris Lyon (CNLVN) on YouTube as he came over to film my layout.
IMG_0482.JPG


IMG_0444.JPG


As for DCC++, sorry can't help since I already have NCE, but we have a few people who already built it and have it running.
 
#34
The DCC++ aspect doesn't worry me. I'm just deciding if I go with JMRI (not thrilled) or another interface/throttle. Right at the moment, I'm still DC.
Sometime in the future, I will be interested in servo/turnout control, with tactile buttons & bi-color LEDs, Block recognition onto a control board, and perhaps grade crossing control. IR sensors seem to be the ideal choice for the latter two.
 

migalyto

Well-Known Member
#35
Lloyd,

That's awesome. I never even thought of a speedometer, or a working weigh station as possible uses. I will defiantly go check out the videos, and since I'm nowhere near building, or grade crossings, a speedometer is something I could use now. Would this be a beginner project, or would you start with something less?
 
#36
Lloyd,

That's awesome. I never even thought of a speedometer, or a working weigh station as possible uses. I will defiantly go check out the videos, and since I'm nowhere near building, or grade crossings, a speedometer is something I could use now. Would this be a beginner project, or would you start with something less?
Hi Mike,

The complicated part of working with Arduino is the coding, but lots of coding (sketches) have already been done.
I would start with some basic examples first and once you get the hang of it, than you can start combining more than one item.

I learned most of the stuff through a few courses given on the net called Udemy, look up Udemy courses on Arduino.
There's one called step by step for beginners and another called step by step getting serious.
Also being part of a group that like sharing things about model railroading.
 

migalyto

Well-Known Member
#37
Thanks Lloyd,

I hope you will be a regular part of our discussion since you have hands on experience with this. I will defiantly look those courses up. I really want to learn this. The possibilities are almost unlimited.
 
#38
Thanks Lloyd,

I hope you will be a regular part of our discussion since you have hands on experience with this. I will defiantly look those courses up. I really want to learn this. The possibilities are almost unlimited.
Mike, I usually drop by once a month or so, to see if anything new, but I'm usually online live with a bunch of guys every night and we also have shows such as fine scale, arduino, weathering, special guests and lots more. The group is called YouTubeModelBuilders.
 

wombat457

Well-Known Member
Staff member
#40
Yes ... the electronics is are thing, the code stuff is another matter and what would probably dissuade me from using this stuff. I might be wrong BUT, the more I look at Arduino it seems as though using it only makes achieving an end result much more complicated.
 





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