Simple Code Example -- Arduino to DCC++ Arduino ???

Joey Paul Jan 13, 2019

  1. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3
    Hi Robert,

    It's going well right now. Today I just started to use this app https://play.google.com/store/apps/details?id=nextprototypes.BTSerialController
    and just enter the codes for the buttons for use with the HC-06 transceiver. I was using JMRI over the weekend but I grew leery of having a computer connected to the train tracks. I even changed CVs with the RoboRemo app, but it is limited to only 5 buttons. I've been using this https://github.com/DccPlusPlus/BaseStation/wiki/Commands-for-DCCpp-BaseStation for my commands.

    Can you turn track power on and off? <1> for on and <0> for off. If you cam measure voltage on the track, then the base station is working. I was forgetting to turn the power on before I'd enter <t 1 03 70 1> That would be for decoder 3, "70" for approx half speed and the last "1" for forward. Please keep us posted!

    Take care, Joe.
     
  2. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3
    Hi Folks,

    So JMRI stopped working with the DCC++ base station. And when I tried serial into the base-station from the Arduino serial monitor, I couldn't send anything but could receive that initial message from the base-station. (I can do serial in from the Bluetooth transceiver.) When I tried to reinstall the base-station sketch (motor shield removed), the sketch wouldn't upload. So I used a spare Uno and loaded the sketch, reinstalled the motor shield to use Decoder Pro to change the decoder default number from 3 to 4 in my 2nd Digitrax decoder. Then put the original configuration back together. So the "bad" Uno still works well with the motor shield and the Bluetooth transceiver or anything into pin zero, just seems something burned out for the serial in from USB. I had a stray jumper hit the Uno one about a week ago and caused a flicker, so I guess I blew something. I didn't capture the error message from the Arduino IDE, but will when I have some more time.

    Take care, Joe.
     
  3. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3
    Hi Folks,

    So I just realized that I can connect my MacBook wirelessly to the DCC++ base-station via the HC-06 Bluetooth module (for running Decoder Pro or Panel Pro, or the Arduino serial monitor). I was always leery of having my computer physically and electrically connected to train track. I saw that Bluetooth serial port show up in the connections screen of Decoder Pro and it all came together for me. I damaged something when I shorted the ICSP pins somehow on the Arduino with DCC++ installed. So that Arduino board cannot accept serial anymore via the USB connection, but now it's better being completely independent of the computer. (I did change the baud rate of the HC-06 to 115200 and I use the voltage divider on the RX pin of the module. I used this tutorial for the baud rate change. )

    Take care, Joe.
     
  4. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3
    Hi again, Folks,
    Here are a couple photos of the HC-06 with the base-station to connect wirelessly to JMRI on my computer, or using the Robo Remo free remote control app, no USB necessary, can power from the barrel jack:
    [​IMG] [​IMG]
    [​IMG]
    Take care, Joe.
     
    Last edited: Apr 17, 2020
    KC Smith likes this.
  5. KC Smith

    KC Smith TrainBoard Member

    109
    111
    12
    Joey Paul,

    Brilliant and Simplistic idea and code work on DCC Automated Throttle Controller for your Lionel engine post #22.

    Thank you for developing and posting this, I've copied and enhanced it for a Bachmann 2-8-4 Berkshire 1225 Steam Engine with sound and it works splendidly.

    later I plan to edit it again and use it to automate multiple engines in a Steam Engine House and turntable yard.

    Anyway, here is your code edited for a Sound Decoder Steam Engine.
    Thanks so much and be Safe.
    Kevin

    *Note I placed extra spaces in to make it easier to see t and f code and to line up numbers.
    Unfortunately I test copy and pasted it back into the Arduino IDE editor the comments didn't line back up.

    // DCC++ Automated Throttle Control: April-17-2020 KC Smith, enhanced from Joey Paul original
    // Engine #1225 runs at varying speeds forward and backwards while letting off Steam, blowing its Whistle and ringing the Bell
    // You can easily intermix additional Engines to move along adjacent sidings or spurs or roundhouse/turntable by just copy, past change add the Engine ### and go.

    void setup() {
    Serial.begin(115200);
    }

    void loop() {
    Serial.print ("<1>"); // Turns track Power On
    delay(2000); // delays 2 seconds
    Serial.print ("<f 1225 136 >"); // Turn on Steam
    delay(4000); // delays 4 seconds
    Serial.print ("<f 1225 144 >"); // turn on Head Light turn off steam
    Serial.print ("<f 1225 148 >"); // head light + short whistle
    delay(1000); // wait 1 second
    Serial.print ("<f 1225 148 >"); // head light + short whistle
    Serial.print ("<t1 1225 30 1>"); // Eng #1225, 30 speed up (0-126 possible) the last "1" is for forward
    delay(10000); // wait 10 seconds
    Serial.print ("<t1 1225 15 1>"); // speed 15 slow
    delay(10000);
    Serial.print ("<f 1225 146 >"); // head light + whistle
    delay(1000);
    Serial.print ("<f 1225 144 >"); // head light only turn off whistle
    Serial.print ("<t1 1225 70 1>"); // speed 70 medium
    delay(6000); // delays 6 seconds
    Serial.print ("<f 1225 146 >"); // head light + whistle
    delay(2000); // delays 2 second
    Serial.print ("<f 1225 144 >"); // head light only
    delay(2000);
    Serial.print ("<t1 1225 1 1>"); // speed near zero
    delay(2000);
    Serial.print ("<f 1225 145 >"); // Tinder light + bell for reverse
    Serial.print ("<t1 1225 50 0>"); // speed 50 the last "0" for reverse
    delay(8000); // delays 8 seconds
    Serial.print ("<t1 1225 0 0>"); // change speed to zero in reverse
    Serial.print ("<f 1225 144 >"); // Rear tinder light only turn off bell
    delay(1000);
    Serial.print ("<f 1225 148 >"); // lights + short whistle
    delay(1000);
    Serial.print ("<f 1225 148 >"); // lights + short whistle
    Serial.print ("<f 1225 146 >"); // lights + whistle
    delay(1000);
    Serial.print ("<f 1225 144 >"); // light only turn off whistle
    delay(1000);
    Serial.print ("<t1 1225 70 1>"); // speed 70 back to forward
    delay(10000);
    Serial.print ("<f 1225 152 >"); // lights + Steam
    Serial.print ("<t1 1225 0 1>"); // speed zero
    delay(5000);
    Serial.print ("<f 1225 144 >"); // Head light only turn off steam
    delay(2000);
    Serial.print ("<f 1225 128 >"); // All functions keys Off
    Serial.print ("<0>"); // track Power Off
    delay(5000); // delays 5 seconds
    }


    Totally
    Realistic
    Automated
    Interface
    Nerds
    World
    Railroad
    Experimental
    Controller
    Kit
     
    Last edited: Apr 17, 2020
    Joey Paul likes this.
  6. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3
    Hi KC and Everyone,

    So I made a few breakthroughs (for me). Using JMRI completely wireless, is great. I always feared some sort of electrical spike damaging my computer especialyl since the Lionel turnouts have large track-powered solenoids. I am looking into wireless upload of a sketch to the Arduino; found some good stuff, but like with DCC++, it takes me a while to wrap my brain around it. I also discovered that one HC-06's TX can connect to 2 Arduino Unos.

    I'm working on this simple code for the Robo Remo free app:

    Code:
    // remote slider set 0 to 9
    // upper case P for power button, upper case D for direction
    // turnout toggle lowercase e and f
    // serial keyboard window accepts any switch and loco id
    // for loco selection, enter lower case L, then <enter>, then loco number between 1-9
    
    int incomingByte = 0; // for incoming serial data
    int P = 0; //power toggle
    int D = 1; //direction toggle
    int L = 51; //loco number defaults to 3 (ascii of 51)
    
    int outPin1 = 7; // relay 1
    int outPin2 = 8; // relay 2
    
    void setup() {
      pinMode (outPin1, OUTPUT);
      pinMode (outPin2, OUTPUT);
      digitalWrite (outPin1, HIGH); // for relay boards activated when pin is LOW
      digitalWrite (outPin2, HIGH);
    
      Serial.begin(115200);
    }
    
    void loop() {
    
      if (Serial.available() > 0) {
    
        incomingByte = Serial.read();
    
        if (incomingByte == 80) {
          P = P + 1;
          if (P >= 2) P = 0;
          Serial.print("<");
          Serial.print(P);
          Serial.println(">"); // toggle power on/off
        }
        if (incomingByte == 68) {
          D = D + 1;
          if (D >= 2) D = 0; // toggle direction
        }
    
        if (incomingByte == 101) {
          digitalWrite (outPin1, LOW);  //e turnout curve
          delay(250);
          digitalWrite (outPin1, HIGH);
        }
        if (incomingByte == 102) {
          digitalWrite (outPin2, LOW);  //f turnout straight
          delay(250);
          digitalWrite (outPin2, HIGH);
        }
    
        if ((incomingByte > 47) && (incomingByte < 58) ) { // ascii 48 to 57
          Serial.print ("<t 1 0");
          Serial.print(char (L)); // loco numbers 1-9
          Serial.print(" ");
          Serial.print((incomingByte - 48) * 14); //ascii code - 48 x 14,
          Serial.print(" ");                                       //range 0 to 126 in 10 speed steps
          Serial.print(D); // direction
          Serial.print (">");
        }
        if (incomingByte == 108) testMethod(); // receives lowercase L
    
      }
    }
    void testMethod () {
      delay(300);
      while (Serial.available() == 0);
      L = Serial.read(); // accepts loco number
    
    }
    
    [​IMG]


    I'll try to do a little video soon. It works very well, but for only one throttle at a time on the screen; takes a few seconds to change locos (only can do 9 locos). "Switch" is for the relay for the turnout solenoid.

    Take care, Joe.

    [​IMG]
     
    Last edited: Apr 18, 2020
    KC Smith likes this.
  7. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3
    Hi Folks,

    Here's my short video of the RoboRemo app and Panel Pro with the HC-06:



    Take care, Joe.
     
    Last edited: Apr 19, 2020
    KC Smith likes this.
  8. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3
    One last video, testing my simple remote code:

     
  9. Joey Paul

    Joey Paul TrainBoard Member

    34
    11
    3

Share This Page