DCC++ Hardware - Throttles

KE4NYV Jan 25, 2016

  1. mayhaw9999

    mayhaw9999 TrainBoard Member

    27
    28
    5
    Hi Dave,
    Thanks for everything you are doing for this great DCC++ community. My hardware skills are fairly good , but software/coding leaves a LOT to be desired.I know this is old technology, but I'm in the process of building the wired throttle in preparation for going the next step to the HC12 wireless one. Thought I should get the problems worked out with this one first. Anyway, I'm not getting a display on the LCD. Get the back light - no text. Have checked (and swapped) the wiring and tried two or more different Arduino libraries to no avail. My loco moves, stops and reverses as it should. Any ideas?

    David U
     
    Scott Eric Catalano likes this.
  2. Atani

    Atani TrainBoard Member

    1,469
    1,755
    37
    if your LCD is I2C you will need to run a scanner to find its address and update the address for it in the code.
     
    Scott Eric Catalano likes this.
  3. mayhaw9999

    mayhaw9999 TrainBoard Member

    27
    28
    5
    Did that and updated the code in one place.
     
    Scott Eric Catalano likes this.
  4. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    All,

    I've been playing with some new ideas for the Nextion display this afternoon.

    Of interest to me was solving the problem of a full 29 function key page which would be fully status stored and updated as you switched
    between each of the 10 loco's on my throttle design. 290 bits of data !
    Thanks to Gregg's brilliant work on the storage part, all stored in 40 bytes of memory.
    I figured an access and update routine, done in just a few lines of code.
    Now that part is done, <f string> assembly should be a breeze.
    Auto-resends, as mentioned recently should be easy to work in too. I've put some keys to do that manually as in my other designs.
    My display is currently twinkling through those bits, run by a little Uno sketch. Looks quite effective, and nothing dropped as I can see at the moment.
    I'll try get a video up somewhere for you guys.
    CaptureFn.JPG

    Steve
     
    Last edited: Sep 1, 2016
    esfeld, Scott Eric Catalano and Atani like this.
  5. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    David - good to hear you have dived into the project - just a quick thought on the LCD, have you adjusted the contrast potentiometer on the board so that you see a group of boxes?

    dave
     
    Scott Eric Catalano and Atani like this.
  6. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    Nice work on the functions, Steve- What size display are you using? I am using the 3.2" version and getting a particular small button with my finger can be a bit of a challenge! A stylus makes it much easier.

    dave
     
    Scott Eric Catalano likes this.
  7. Steve S

    Steve S TrainBoard Member

    95
    22
    8
    That's a nice looking unit with the display, Dave. I wonder if it would fit into one of these enclosures. It's the same company that makes the T-shaped enclosure someone else used.
    http://www.hammondmfg.com/1553.htm
    There's a PDF file with the dimensions...
    http://www.hammondmfg.com/pdf/1553D.pdf

    Steve S
     
    Scott Eric Catalano likes this.
  8. mayhaw9999

    mayhaw9999 TrainBoard Member

    27
    28
    5
    Dave,
    Thanks Dave. I just did that and got the display. Now having some problems setting loco addresses. May have a problem with the number pad. I need to double check everything and get back to you.

    David U
     
    Scott Eric Catalano likes this.
  9. mayhaw9999

    mayhaw9999 TrainBoard Member

    27
    28
    5
    It seems that NZ Ross and I got keypads from the same factory build. As noted in posts #191 and 193, all keypads are not the same. I changed my code to his, wired counting from the right with the keypad facing forward and viola, it all works! Functions also work with my sound loco, so I'm ready to go to the wireless build. I've already changed the modules to the correct baud rate and adjusted the power level. Doubt any significant problems from here on.

    Thanks Ross and Dave.

    David U
     
    Scott Eric Catalano likes this.
  10. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    Good work, David - keep us posted on your progress!

    dave
     
    Scott Eric Catalano likes this.
  11. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Hi Dave,

    I got the 3.5", so virtually no difference. And yes, I soon found that a cheap rubber tipped stylus was the way to go, highly recommend to anyone
    getting one of these displays.

    Steve.
     
    Scott Eric Catalano likes this.
  12. Steve S

    Steve S TrainBoard Member

    95
    22
    8
    Last night I mocked up the image below to see if the displays would fit on the Hammond enclosures. At left is the 3.2" and the right one is 3.5". Everything is to scale using the dimensions from the Nextion site. The knob is 1" in diameter. It looks like there's enough room. The main concern would be depth. The PDF file I linked to earlier should tell you how much room there is.
    I put the knob in the middle so it doesn't matter whether you're right or left handed. You could put the power switch on the top plate.
    Here's a site that sells the enclosures for $7.27.
    http://www.mouser.com/ProductDetail/Hammond-Manufacturing/1553DBK/?qs=sGAEpiMZZMsrGrAVj6eTvQL/wQ3TetVEyrjLozHh9H8=

    [​IMG]


    Steve S
     
  13. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    All,

    I've now added some print routines for the Function commands.

    I'm totally new to Bitwise operations so this is my first working attempt.
    Perhaps the programming guys amongst you can give this the once over.

    This demo sketch runs on an Uno with your Nextion connected via Software Serial (for me it runs fine at 115200 baud).
    Set your display accordingly !

    The program is looping through the bit data at the top of the sketch (5 loco's worth) printing the <f string> and setting the Nextion keys at the same time.
    You can see the output in the Arduino serial monitor, on the display the keys light accordingly if you set all bco 1(0-28) attributes to a bright color of your choice.
    I'm not providing HMI files as everyone who owns a display should familiarise themselves with using the Editor.
    And besides files are display size specific.

    For the Nextion you will have to build a key page similar to the image below. The "F" buttons are the Dual-State type and pay attention to getting the Object Names (bt0 -bt4)
    of keys F0 - F4 in the correct order as shown, or things will not work as expected.
    The RSD keys are not required.

    CaptureFn2.JPG
    Code:
    #include <SoftwareSerial.h>
    
    SoftwareSerial NexSerial(10, 11); // RX, TX on pins 10 and 11. Nextion TX->10  RX->11.
    
    const char ter [3] = {0xff, 0xff, 0xff};//Terminating characters
    int f;
    String message;
    long lastMillis = 0;
    
    int loco [5] = {
      1024,
      17,
      2820,
      376,
      2016
    };
    
    unsigned long fnbtns [5] = {
      0b00001000100010110010001111110011,
      0b00010001101000100011101000110010,
      0b00000101100100010100101101001010,
      0b00011001110010100001001100011000,
      0b00000101101001001100010001001010
    };
    
    void setup() {
      Serial.begin(115200);
      NexSerial.begin(115200);//Make sure your display is set to this baud, or amend if you need it slower.
    }
    
    void setFnButtons() {//Sends 29 strings to the Nextion to set the Funtion keys accordingly.
      for (int i = 0; i < 29; i ++) {
        int y = (fnbtns[f] >> i) & 1;// i = 0..28.  stores nth(i) bit of fnbtns[] in y.  y becomes 0 or 1.
    
        NexSerial.print("bt");//No library needed here !!!
        NexSerial.print(i);
        NexSerial.print(".val=");
        NexSerial.print(y);
        NexSerial.write(ter, 3);
      }
    }
    
    void sendFn0_4() {
      byte mask = 31;
      byte fn = fnbtns[f] & mask;
      Serial.print("Binary = ");
      Serial.println(fn, BIN);
      Serial.print("<f");
      Serial.print(loco[f]);
      Serial.print(" ");
      Serial.print(fn + 128);
      Serial.println(">");
      Serial.println();
    }
    
    void sendFn5_8() {
      int mask = 15;
      int fn = fnbtns[f] & mask << 5;
      fn = fn >> 5;
      Serial.print("Binary = ");
      Serial.println(fn, BIN);
      Serial.print("<f");
      Serial.print(loco[f]);
      Serial.print(" ");
      Serial.print(fn + 176);
      Serial.println(">");
      Serial.println();
    }
    
    void sendFn9_12() {
      int mask = 15;
      int fn = fnbtns[f] & mask << 9;
      fn = fn >> 9;
      Serial.print("Binary = ");
      Serial.println(fn, BIN);
      Serial.print("<f");
      Serial.print(loco[f]);
      Serial.print(" ");
      Serial.print(fn + 160);
      Serial.println(">");
      Serial.println();
    }
    
    void sendFn13_20() {
      unsigned long mask = 255;
      unsigned long fn = fnbtns[f] & mask << 13;
      fn = fn >> 13;
      Serial.print("Binary = ");
      Serial.println(fn, BIN);
      Serial.print("<f");
      Serial.print(loco[f]);
      Serial.print(" ");
      Serial.print("222 ");
      Serial.print(fn);
      Serial.println(">");
      Serial.println();
    }
    
    void sendFn21_28() {
      unsigned long mask = 255;
      unsigned long fn = fnbtns[f] & mask << 21;
      fn = fn >> 21;
      Serial.print("Binary = ");
      Serial.println(fn, BIN);
      Serial.print("<f");
      Serial.print(loco[f]);
      Serial.print(" ");
      Serial.print("223 ");
      Serial.print(fn);
      Serial.println(">");
      Serial.println();
      Serial.println();
    }
    
    void loop() {
    
      unsigned long currentMillis = millis();
    
      if (currentMillis - lastMillis > 2000) {
        lastMillis = currentMillis;
        setFnButtons();
        sendFn0_4();
        sendFn5_8();
        sendFn9_12();
        sendFn13_20();
        sendFn21_28();
        f++;
        if (f == 5) f = 0;
      }
    }
    Just to get at bit setting routine done for the key presses of the Nextion, and that is about job done.
    Chime in if you have suggestions.

    Steve.
     
    Last edited: Sep 1, 2016
    Scott Eric Catalano likes this.
  14. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    Thanks for your work on this, Steve - I'll need a bit of time to digest what you have done & how.
    dave
     
    Scott Eric Catalano likes this.
  15. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Ask the questions Dave,

    More than happy to explain.

    S.
     
    Scott Eric Catalano likes this.
  16. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    To all C/C++ guys out there.

    I'm trying to add an XOR function to the code above at #253.

    fnbtns [f] = fnbtns [f] ^ (1 << func);

    func is a variable from my Nextion display.

    The code works fine up to bit 16, then nothing. func is definitely there above 16 as it prints to serial just above the XOR.

    The print routines will shift a mask above 16, whats going on with XOR ? I'm new to this stuff :).

    Regards
    Steve.
     
    Scott Eric Catalano likes this.
  17. Travis Farmer

    Travis Farmer TrainBoard Member

    352
    320
    14
    What data type is fnbtns?

    ~Travis
     
    Scott Eric Catalano likes this.
  18. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Hi Travis,

    I've played around changing types, above in the #253 post it is an unsigned long.
    Also tried uint32_t.

    Thanks.

    Edit: There is no corruption to fnbtns as it carries on behaving correctly elsewhere in the sketch.
     
    Last edited: Sep 1, 2016
    Scott Eric Catalano likes this.
  19. Travis Farmer

    Travis Farmer TrainBoard Member

    352
    320
    14
    well that should be fine. the only other culprit may be XOR itself. it could be just a integer operator. I will look in the Arduino core files and see if I can dig it up.

    ~Travis
     
    Scott Eric Catalano likes this.
  20. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Thanks Travis,

    I'm not getting much joy from google.
     
    Scott Eric Catalano likes this.

Share This Page