model railway signalling

This is the story so far...

My thanks to everyone who answered my questions.

work continues...

TJ

My thanks to everyone who answered my questions.

What questions?
Where?

Why isn't this posted along with the questions?

Nice video, the signals look good.

Looks like you have an upper track as well, so expect you have plenty more coding /signals to do.

ricky101:
Nice video, the signals look good.

Looks like you have an upper track as well, so expect you have plenty more coding /signals to do.

Thanks v much

the bulk of the set-up is done now.

It's just a question of duplicating hardware (signal heads) and software (arduino subroutines).

Next step is to attempt semaphores using servos. Expect I'll be back with more dopey questions.

(and finding other uses for the arduino)

This may be of use to You. I developed it to change my points via servo but it would also work for semaphore signals. Summary of function is at the top. :slight_smile:

/*This sketch is designed to operate Model Railway Points that are driven by Servo Motors
 *A SPDT switch connected to + and - 5vDC with the centre tap going to an Input Pin.
 * 
 *This will scan a predefined set if Pins and allocate as INPUT then set them LOW.
 *Dependant on the PIN STATE and whether or not it has changed it will rotate a SERVO on the active pin 
 *to a pre-determined location as set out in the SERVOMIN/SERVOMAX arrays.
 *Millis() function has been used to streamline the operation.
 *Serial.print can be removed, they are just for checking.
 *
 *Machine:- UNO R3 and 16 Servo Controller
 *
 *Created by Doug Reed 10/05/2016
 *
 *My thanks to the Forum Members who pointed me in the right direction.
 */

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

unsigned long previousMillisSWITCH=0;                                         //Button Millis() setup
int intervalSWITCH = 20;                                                      //intervals for millis()

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();                      // called this way, it uses the default address 0x40

unsigned  int SERVOMIN[11] = {172,172,172,246,246,172,246,200,200,150};       //servo lower setting to suit individual servo
unsigned  int SERVOMAX[11] = {565,530,500,492,492,565,492,550,550,525};       //servo upper setting to suit individual servo                                 
int InPin[11] = {2,3,4,5,6,7,8,9,10,11,12};                                   //array of input pins
int PinCount = 11;                                                            //the number of pins used, lenght of array
int Dir = 0;
int OldDir[11];
int NewDir[11];
int thisPin;
int degrees;

void setup() {
  Serial.begin(9600);
    pwm.begin();
    pwm.setPWMFreq(60);                                                       // Analog servos run at ~60 Hz updates
    
   for (int thisPin = 0 ; thisPin < PinCount ; thisPin++) {                   //"thisPin" is array position                                           
      pinMode(InPin[thisPin],INPUT);                                          //sets all pins 2 -12 as INPUT
      }
   for (int thisPin = 0 ; thisPin < PinCount ; thisPin++) {
      digitalWrite(InPin[thisPin],0);                                         //sets all pins 2 -12 to LOW

    Serial.print("   InPin  ");                                               //displays the STATE of the PIN
    Serial.print(InPin[thisPin]);
      int p = digitalRead(InPin[thisPin]);
    Serial.print("   State  ");
    Serial.println(p);
      delay(1000);
      }                                  
}
void loop() {
    unsigned long currentMillis = millis();                                           // get current time stamp, only need one for all if-statements
    if ((unsigned long)(currentMillis - previousMillisSWITCH) >= intervalSWITCH) {    // time between button presses
  
    for (int thisPin = 0 ; thisPin < PinCount ; thisPin++) {                          //assess the direction settings
        Dir = digitalRead(thisPin+2);
        OldDir[thisPin] = NewDir[thisPin];
            Serial.print(InPin[thisPin]);
            Serial.print("    Dir    ");
            Serial.println(Dir);
                        
    if ((Dir == 1) && (OldDir[thisPin] == 0)){
        for (uint16_t pulselen = SERVOMIN[thisPin]; pulselen < SERVOMAX[thisPin]; pulselen++) {
            pwm.setPWM(thisPin, 0, pulselen);
            Serial.print(InPin[thisPin]);
            Serial.print("    HIGH    ");
            Serial.println(pulselen);
      }
         NewDir[thisPin] = 1;
    }
    else
    if ((Dir == 0 ) && (OldDir[thisPin] == 1)) {
    
        for (uint16_t pulselen = SERVOMAX[thisPin]; pulselen > SERVOMIN[thisPin]; pulselen--) {
            pwm.setPWM(thisPin, 0, pulselen);
            Serial.print(InPin[thisPin]);
            Serial.print("    LOW    ");
            Serial.println(pulselen);
      }
         NewDir[thisPin] = 0;
    }
  }
}
previousMillisSWITCH = currentMillis;
}

Very cool!! Good work. I'm working on a controller for my layout too using Arduino, so much fun.

Sorry for the long delay...I have the controller side pretty much worked out, am just waiting on a sound card to add engine sound, at this stage either from within the hand controller or as a type of surround sound, not sure until i can get the sound card working. If you haven't already, have a look at adding a 1602 LCD to your controller, that way you can add/adjust various features as you need to or just see what the controller is doing.

hope your project is going well.

I about to start the learning curve for utilising a ardiuno to operate signal lights. Can you provive the code that you used to drive your lights as shown on utube. It would give me a great start.

Hi Gps65

I see that you have four LEDs for each signal post. Do you have a lot of those?

Just to handle those two, you would use 8 digital pins on your Arduino.

Have you heard about Shift Registers (74HC595)? With just one of those, you can set 8 LEDs on and off using only four Arduino pins. Better yet, you can daisy-chain those babies. How many of them? If powered properly, as many as you want, and still controled by only four Arduino pins.

If you are interested, I made a Library that takes care of exactly that.

find it at : https://github.com/j-bellavance/HC595.

Let me know if this helps.