1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

Tutorial Adjustable Shift Light with UNO in GameDash

Discussion in 'DIY peripherals' started by eaorobbie, Dec 10, 2013.

  1. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,440Coins
    Ratings:
    +1,684 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    Adjustable Shift Light with Ard UNO.
    Aim: To use the GameDash to capture the Rpm value from the game and send it to the Ard UNO in a form like Rxxxxx where xxxxx is the real value for the Rpm value for example 1000 rpm = R01000.
    With the Uno we can set a shift point by holding the desired rev point and pressing a button to capture the rpm plus use a pot to change the range the Undershift and OverShift lights come on.

    Diagrams :

    ShiftLight Box.png arduino Shift Light - ShiftLight Complete_bb.png arduino Shift Light - Leds_schem.png arduino Shift Light - Shiftpoint Capture Circuit_schem.png arduino Shift Light - Tuner Circuit_schem.png
    Arduino Code:


    Please note to use this code in Serial Monitor of the Arduino IDE , Type R04000 to show shift point and 500 higher and lower to control both leds, Pot will increase the margin between led points and a press of the button will capture the current rpm and set that to the ShiftPoint.
    To use with the GameDash please comment out any of the Serial.pr* lines with a // in front of them and GameDash will now work with it.

    Arduino Code as tested with a UNO and Freetronics Eleven and Nano contained in zip.

    RpmAdjustShiftLedsv2.zip
    Explanation of Code:

    First we must define some variables to use, they are as follows;

    ShiftPoint - Stores the value for the Rpm we wish to use to light the Shiftlight.
    RpmSimTools - Stores the value GameDash is sending (00000 – 99999).
    LimitRange - Stores the value that is read by the pot to change the upper and lower limits(0 – 1023).
    UnderShiftPoint - Stores the value that will be calculated from the ShiftPoint minus Undershift.
    OverShiftPoint -Stores the value that will be calculated from the ShiftPoint plus Overshift.
    Overshift -Stores the value mapped from the LimitRange (0 – 2000).
    UnderShift - Stores the value mapped from the LimitRange (0 – 2000).

    ButtonState - Stores the button state to make sure in a check that once you have pressed the button the rpm has actually changed therefore we need to set it to the new value. (LOW –OFF ,HIGH – ON)
    LastButtonState - As above stores a button state to use in the check to make sure it was pressed.
    lastDebounceTime - Stores the last time of how fast the button bounced.
    debounceDelay - Time measured in ms if you button seems to bounce ie 1 press = 2 button presses leading to an incorrect value , this can be increase to make it steady and not bounce.

    bufferArray - An array to store the single values coming from the Game Dash – (0 – 5). These will be in ascii format so you will need to minus 48 from the value to get the real value sent.

    Setup the pins and Initiate the pins we will use either in Output or Input.

    Pin 3 - UnderShiftPoint Led.
    Pin 4 - ShiftPoint Led.
    Pin 5 - OverShiftPoint Led.

    Open the serial port and set the Baud Rate to 9600.


    Program Loop.

    Here we call four basic functions.

    CheckButtonPress - This checks for a button press everyloop and completes its action.

    CheckShiftLightAdj - This checks the pots reading so it may determine the range that you have set for the Under/OverShiftPoints.

    SetLimits - Could also be done in the CheckShiftLightAdj but thought to keep it separate. Creates the Upper and Lower ShiftPoint values.

    DisplayLeds -Sets the state in which the Leds appear (LOW – Off) (HIGH – On).

    ReadData -Reads the serial port and create a new array with the new data.


    Functions.

    CheckButtonPress()

    Read the switchs state (reading) that’s connected to pin 6.(HIGH/LOW)
    If the state(reading) is not equal to the LastButtonState ,
    Then start counting in ms and assign this to the lastdebouncetime.
    If the counter millis() minus the lastDebounceTime) greater than the debounceDelay (we do this to filter out any noise ie some moment button when press give 2 to 3 changes on a press. So we debounce it to clear it up,
    Then if the button state(reading) not equal to the ButtonState,
    We make ButtonState is equal to the reading to keep the new state.
    If the ButtonState is equal to HIGH as in it was pressed,
    We make the ShiftPoint equal to the read RpmSimTools we got in ReadData.
    Return to the loop.


    CheckShiftLighAdj()

    Read the pot connected to a01 and return this value to LimitRange(0-1023).
    Now calculate the how far to adjust the rpm but mapping (0-1023) to (0-2000) so that 1023 on the pot equals 2000 rpm for a change to occur.
    Now set the UnderShift which is equal to above calculate rpm.
    Now set the OverShift which again is equal to the above, keep them separate so one may have different settings in the future.
    Return to the loop.

    SetLimits()

    The UnderShiftPoint (where the green led comes on) is equal to the ShiftPoint minus the UnderShift.
    The OverShiftPoint (where the Red Led comes on) is equal to the ShiftPoint plus the Overshift.
    Return to loop.

    DisplayLeds()

    Right to gain the right sequence of lights , for example I used this.
    They follow this truth table below. (On = HIGH) (Off = LOW).

    Undershift Green(pin3)On Blue(Pin4)Off Red(Pin5)Off.
    ShiftPoint Green(pin3)On Blue(Pin4)On Red(Pin5)Off.
    OverShift Green(Pin3)On Blue(Pin4)On Red(Pin5)On.

    ReadData()

    Here we read the 6 bits that the GameDash is sending to the Arduino so we can turn it into a real rpm value to work with.
    First we check to make sure that the string coming in is 6 bits , no more or less, if its , ignore it because its bad data.
    If it is 6 bits read each bit into our array (bufferArray) with the first value starting at 0.
    Now check if we received an Rpm Value (R) in the first (o)buffer if it is calculated the real rpm value by
    Taking the second (1) buffer and minus 48 (‘O’) to get the decimal value and times it by 10000 then adding,
    The third (2) buffer and minus 48 (‘O’) to get the decimal value and times it by 1000 then adding,
    The fourth (3) buffer and minus 48 (‘O’) to get the decimal value and times it by 100 then adding,
    The fifth (4) buffer and minus 48 (‘O’) to get the decimal value and times it by 10 then adding,
    The six (5) buffer and minus 48 (‘O’) to get the decimal value,
    Which now be equal to the RpmSimTools.
    Return to Loop.


    So in all its just a big loop, ok for one variable but not the recommended way for many variables.
    To come is 3 variables (Speed,Rpm,Gear) on a TM1638 screen, then add both of these to the one board.
    Hope this helps to clarify the above code and help others to produce something different.

    GameDash Output Settings
    To get to the Output Settings, once Game Dash has loaded, right click on the icon in the task bar and select Settings.
    Depending on the comport number you are using for the Arduino will depend on what you enter here.
    Select Ser from the Output Type.
    Enter like picture below, but with your ComPort inserted.
    [​IMG]
    And remember to press Save.
    And you can like in the Game Engine - Create Preset to save this to a file for sending to some one to use.

    GameDash Profile Settings
    Race 07 - Rpm Script.
    For an example as most are similar, I will run through the script for sending rpm from Race 07.
    Rpm so far can be found in two different formats in the Game Plugins from SimTools, for example Race07,GTR2 is equal to RpmValue times 10 and in Rfactor RpmValue recieved is the actual Rpm. This all does depend on what the game plugin creaters have set it as.

    Now the below script contained in the picture was obtained by following this:

    Ok starting the game and studing the values that appear we notice the Rpm is in the Dash2 area. So taking note of Dash2 ,with game still running in background we then need to click on the pencil on the right hand side of the Dash2(Rpm) boxes.
    This is where we can enter a script to change the way the the data is formated.
    Clicking the Help button will bring up a list of Commands that can be used.

    Ok now looking at the Rpm details we notice that the value is -1, well we cant have a negative value as a rpm value so we need to replace -1 with a 0.
    Replace -1 0

    Now we notice the value needs to be times by 10 to give us the real value.
    Math * 10
    Ok looking better, but don't need a decimal place so we need to round it to 0 decimals
    Round 0
    Now the Ard. code requires a five digit number to be always sent so we can convert the number as quickly as possible. So we end up like a 1000 Rpm needs to be sent as 01000 and 1 Rpm is equal to 00001 so in turn we need to pad our result with zeros.
    Pad 5 0
    Once you have the script as like the picture below you can press Save and close the Command Editor to continue.

    Output Settings - per game.

    This needs to be entered for each game plugins as some may vary the position of certain data. Up to them to where they set it as in Dash1 -20.
    So for Race07 and for the code supplied I setup as per the picture.
    Startup is like a null to clear any leds that may be still on. R00000 at 10ms.
    Interface is set for the code and reads Dash2 - R<Dash2> at 10ms
    ShutDown is the same as the Startup - R00000 at 10ms to clear the leds.

    Now press Save and we can continue to now test the shift light, may need a restart once you have it all set correctly,ie restart the game.


    [​IMG]

    How to Use:
    Once in game, go onto the track and rev the car in neutral to the desired rev that you want to shift at, and hold it. Now press the button and the shift light should appear, now if you want the Undershift light to be a 500 rpm under the Shiftlight, then back of the rpm by the desired amount then turn the pot until only the green(UnderShift) led is light.But if not worried about an Undershift point and you wish to set a Max Rpm (Overshift) point, then hold the rpm at this point , and turn the pot so that all leds come on at this point. All depends on what you want set.

    Any changes let me know , quite happy to change the code for anyone, plus we have combined this with a Tm1638 as well and uses one of the TM1638 buttons to control the ShiftPoint.
    Please enjoy.

    Ard UNO - TM1638 Display - EaoRobbie..png
    Above can be added into the wiring and a display can be used to emulate more of the Dash Board, the script become a lot bigger too. Sorry I need to document this separately.
    • Like Like x 5
    Last edited: Jan 6, 2014
  2. prodigy

    prodigy Burning revs

    Joined:
    Oct 27, 2013
    Messages:
    459
    Location:
    Croatia
    Balance:
    6,698Coins
    Ratings:
    +399 / 4 / -0
    My Motion Simulator:
    2DOF, 3DOF, AC motor, SCN5, JRK
    Can't zoom in on those pics, it says I don't have permission to do that action..
  3. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,864Coins
    Ratings:
    +2,007 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    Have the same permission problem, and i am admin:) Seems as when your pictures are not uploaded, only linked from external post?
  4. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,440Coins
    Ratings:
    +1,684 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    • Like Like x 1
  5. grit

    grit New Member

    Joined:
    Oct 31, 2013
    Messages:
    4
    Balance:
    11Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    Hi,

    This is my first post, so it's only appropriate to firstly say thanks a million for releasing simtools and gamedash.

    I think that perhaps the same original issue with the images above also applies to the file RpmAdjustShiftLedsv2.zip, as it doesn't seem to be possible to download the code. Would you mind having a look?

    I'm planning to play around with an arduino and drive some combination of some TM1638s, an external gear/shift light indicator, and hopefully some alfa 156 speed and rpm instrument gauges.

    I may getting ahead of myself, but I don't know if it'll be possible to run all the devices above from one arduino. If not, is it possible to run multiple instances of gamedash, each pointing to a different serial port and a different arduino?

    Thanks,
    Grit.
  6. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,440Coins
    Ratings:
    +1,684 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    Welcome @grit, ok to answer your questions.

    1. I will check the zip file and even repost it in the original post. Sorry for the inconvenience. My bad, all copied from our closed beta area, where it was tested.

    2. Ok TM1638 boards can be daisy chained together and the code to work them is quiet simple. External gear shifter/Shift Light indicator , well i have a code that was tested by one of beta testers that combines the ShiftLight with the Tm1638 and worked well. Driving the Alfa156 gauges might be a tricky one, all depends on what the motors are that drive the dial, (might not like this suggestion, but its way easier to hack the gauge and replace the motor with a little feather servo, this will only require one more pin from the Ard to drive it.
    The Ard might be limited with pins but if use shift registers we can turn 3 pins into 8 or more again we can daisy chain theses too.
    So depending on the overall design, a circuit could be created and be driven from the Game Dash easily.

    3. No, unless you write a little app to read a udp output that we can send via the gamedash to run multiple devices , Game Dash is still in early days of development, because of a lot of recent question, @yobuddy decided on an early release of what we had so people may start using it and expanding it, Thanks Yobuddy.

    Feel free to start a build post on your dashboard mate, we can drop in and either answer questions or guide you along your journey.

    Again welcome.
  7. grit

    grit New Member

    Joined:
    Oct 31, 2013
    Messages:
    4
    Balance:
    11Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    @eorobbie, Thanks for fixing the link, and also for welcoming me to the community.

    If I've further questions, I'll probably create a new thread as you suggest rather than divert this one too far off topic.

    The TM1638s are great, and it should be straight-forward to daisy-chain them. I had gotten the speedo working previously (using fixed width, variable frequency PWM) - the problem is that I had to use an interrupt on the arduino to avoid timing issues if doing other things in the main loop, IIRC, and this may get more complicated when driving both gauges. Using multiple arduinos (which I have lying idle at the moment) would simplify this. I look forward to playing around with all this, and seeing how it goes.

    UDP is a good idea for scaling up if needs be. I knew the capability was there, but I had somehow forgotten that as an option, so thanks for reminding me.

    @yobuddy, thanks a million for sharing your code.
  8. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,440Coins
    Ratings:
    +1,684 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    @grit
    Shouldnt have to use mutiply Ards, we got 6 steppers and 13 leds running in one dash from one Ard UNO and we could have expanded it further. With a Shift Registor (www.freetronics.com/expand) we can take 3 pins and turn it into mutilplys of 8 per shift resgistor and they can daisy chain easily too. As in 3 pins become 8 with 1x and 16 with 2x and so on.
  9. grit

    grit New Member

    Joined:
    Oct 31, 2013
    Messages:
    4
    Balance:
    11Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    @eaorobbie, thanks for the pointer. I'll look into using a shift register when the time comes. I came across them already in the context of serialising the output to 7-segment leds, so guess you're just doing something along the same lines. Good idea. It definitely saves on the pins.

    I've set aside the alfa clocks for now, but for a different reason. Even with feeding a simple fixed frequency pwm signal (in the absence of any more complex code, which potentially could have timing issues), the needle on the speedo clock is not steady enough, but is wavering between +/- 10%. I don't think that had been the case previously when I had played with it. I had been using an official Arduino Uno board then, and now I'm using a clone of a Leonardo board. I wouldn't have expected a difference, but maybe the clock isn't as accurate on the clone. Getting the time to play around with it all is probably the bigger issue right now.
  10. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,440Coins
    Ratings:
    +1,684 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    I have not had issues with the use of any of the Freetronics Stuff as they are not copies and have been bettered than the Originals and cheaper but not much.
    Be aware Ard clones on ebay and DX as well specially any thing they have cloned from sparkfun, I would recommend spending a little more, for peace of mind, lol this is why I us JRK12V12 to drive sims, never had one fail, going on 5 years, I use Ard for Dashboards only because on high end Windows machines they seem to have a mind of their own sometimes.
    Try pluging it into another com port, or try a different cable, all common issues when using Ards for anything.
    But yes Shift Registers are good way on saving pins, use to use them on my old dashboard cards so that I could drive 10 leds and 3 7-seg displays from 3 pins until someone kindly pointed out the TM1638.
  11. grit

    grit New Member

    Joined:
    Oct 31, 2013
    Messages:
    4
    Balance:
    11Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    Yeah, I agree with regards paying more for peace of mind. The savings just aren't worth the hassle when things do go wrong and you can't even troubleshoot reliably. I've tried the basics (different ports, cables & PSUs), but I might be doing something wrong myself too.

    FWIW, now that I think of it, the problem with the multiple clocks running off the one arduino isn't running out of pins, it's the management of hardware interrupts. Most of the arduino PWM libraries are geared towards fixed frequency PWM, varying the duty cycle, but in my case, the each gauge uses fixed-width variable frequency PWM, which means I have to use a hardware interrupt for each gauge to avoid messing with the timing of the main loop, and vice-versa. Initially, I didn't see a library that could use two interrupts simultaneously (which is why I was considering multiple arduinos), but the library at https://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation, should do the trick.
  12. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,440Coins
    Ratings:
    +1,684 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    Ok I do see your problem, a simple app could be created in VB.net or Freebasic to capture the values and information, that the Game Dash can send via the network, so that the app can then send to mutilply Ards not too hard to do. Keeping the timing all correct for gauge use. Yes the PWM issue with the Ard is what makes them not the best canadiate for sim use but with tweaking of the code some have managed to get a fairly decent ride out of them.
  13. muffkins

    muffkins New Member

    Joined:
    Jan 16, 2017
    Messages:
    5
    Location:
    australia NSW
    Balance:
    189Coins
    Ratings:
    +0 / 0 / -0
    @eaorobbie , howdy, muffkins ere. bit of a neewbie to this site, Im a lil bit confused with somen and was wondering if you could steer me in the right direction.
    Im trying to replicate the adjustable shift light without the tm1638, but using an arduino mega2560. Ive' wired it to your spec though i am using 10k resistors on both leds and the button though i only had a toggle switch for thus but wired it the same.

    Im using simtools 1.3 and the Assetto Corsa Ea plugin, in game dash im getting info when i join a track and play,the RX light shines on the 2560 so i assume Im getting a connection. Nothing happens though, Ive' tried holding a rev flicking the switch on then off but no setting of the rpm range and if i turn the 10K pot yellow is always on but from one end of the pot to the other it just swaps between the green and red but yeh always on never off.

    Replace -1 0
    Math * 10
    Round 0
    Pad 5 0

    is what i put in for Dash2 and the outputs i put R00000,R<Dash2>,R00000. i did the outputs in both the game dash and game engine,in game engine im using 115200 BPS, 8 data bits, none parity,1 stop bit but im unsure of output bit range i have tried a few different setings i figured it should be set 8 but binary or decimal im not sure. Would you have any idea where i am going wrong?

    Manny Thanks
  14. minimus1899

    minimus1899 Member

    Joined:
    Mar 7, 2016
    Messages:
    65
    Occupation:
    retired/broken/falling to bit
    Location:
    lincoln
    Balance:
    163Coins
    Ratings:
    +58 / 1 / -0
    My Motion Simulator:
    Arduino
    Question about this set up im using 220 ohm resistors for the 3 leds 4k7 resistor for the capture button and a 5k pot for the adjustment but in lfs anyway when it goes below 3500 ish revs all 3 leds come on and stay on till you go above the 3500 then they go off till all 3 of them come on at once when reached the max rpm of what was captured, now is it as simple as using a 10k pot or something else

    Kind regards

    Andy