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

[Solved] Arduino code with seperate Gap

Discussion in 'SimTools compatible interfaces' started by Blekkulf, Sep 29, 2021.

  1. Blekkulf

    Blekkulf Member

    Joined:
    May 18, 2018
    Messages:
    69
    Location:
    Molde
    Balance:
    354Coins
    Ratings:
    +13 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Joyrider, 6DOF
    Hello!

    Im using VFD's on my sim, and I control speed by triggering two digital inputs with 00 - 01 - 10 - 11 to have four preset speeds. I use relays and use Gap in the code to trigger the different speeds. Problem is that i need two different Gaps.

    if (gap1>50) digitalWrite(42, HIGH)
    if (gap1>75) digitalWrite(43, HIGH)
    if (gap1>100) digitalWrite(44, HIGH)
    if (gap2>50) digitalWrite(45, HIGH)
    if (gap2>75) digitalWrite(46, HIGH)
    if (gap2>100)digitalWrite(47, HIGH)

    The orginal code is like this;

    int gap;
    int pwm;
    int brakingDistance=30;

    targetPos=constrain(targetPos,potMini+brakingDistance,potMaxi-brakingDistance);

    gap=abs(targetPos-actualPos);

    if (gap<= Tol) {
    motorOff(numMot); //too near to move
    }
    else {
    // PID : calculate speed proportionaly to distance to go
    pwm=195;
    if (gap>50) pwm=215;
    if (gap>75) pwm=235;
    if (gap>100) pwm=255;

    guess i need targetPos1 and 2, and actualPos 1 and 2 too then.
    any good suggestions ? :)
  2. Zed

    Zed VR Simming w/Reverb Gold Contributor

    Joined:
    Apr 4, 2017
    Messages:
    1,044
    Location:
    USA
    Balance:
    5,828Coins
    Ratings:
    +1,042 / 4 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
    If I understand what you are doing, you do need separate variables to hold the different values. A single variable can only hold one value at a time.

    There are “exceptions” where you encode information in individual bits or in upper or lower nibbles, array elements, etc, but it’s still really just one variable, one value.
  3. Blekkulf

    Blekkulf Member

    Joined:
    May 18, 2018
    Messages:
    69
    Location:
    Molde
    Balance:
    354Coins
    Ratings:
    +13 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Joyrider, 6DOF
    Thanks for the reply! Yeah, instead of Gap, I need Gap1 and Gap2 to get two variables. But I think its easier to just double the arduinos, so I have one arduino on each motor :)
  4. Blekkulf

    Blekkulf Member

    Joined:
    May 18, 2018
    Messages:
    69
    Location:
    Molde
    Balance:
    354Coins
    Ratings:
    +13 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Joyrider, 6DOF
    I cant get this digital outputs to change. I got serial.print on "gap", so i know it changes without triggering the outputs. The outputs change if i change the status of the speedpin 1 and 2 in the void setup.


    if (gap<= Tol) {
    motorOff(numMot); //too near to move
    }
    else {
    // PID : calculates speed according to distance
    if (gap>50) digitalWrite(speedpin1=LOW,speedpin2=LOW);
    if (gap>75) digitalWrite(speedpin1=LOW,speedpin2=HIGH);
    if (gap>100) digitalWrite(speedpin1=HIGH,speedpin2=LOW);
    if (gap>200) digitalWrite(speedpin1=HIGH,speedpin2=HIGH);
    // if motor is outside from the range, send motor back to the limit !
    // go forward (up)
    if ((actualPos<potMini) || (actualPos<targetPos)) motorGo(numMot, FW, pwm);
    // go reverse (down)
    if ((actualPos>potMaxi) || (actualPos>targetPos)) motorGo(numMot, RV, pwm);
    }
  5. Blekkulf

    Blekkulf Member

    Joined:
    May 18, 2018
    Messages:
    69
    Location:
    Molde
    Balance:
    354Coins
    Ratings:
    +13 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Joyrider, 6DOF
    Solved! Changed the digitalWrite to two seperate lines and changed from = to , .
  6. Zed

    Zed VR Simming w/Reverb Gold Contributor

    Joined:
    Apr 4, 2017
    Messages:
    1,044
    Location:
    USA
    Balance:
    5,828Coins
    Ratings:
    +1,042 / 4 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
    Glad you got it working. Just a comment on that code snippet though, if gap is 201 or more, every if statement will get executed because they would all be true. That may not matter to the actual functioning, but those speed pins will flutter in state each time that code executes unless gap is 50 or below where only one if gets executed. I’d recommend looking at a switch/case statement or if/else.
    • Informative Informative x 1
  7. Blekkulf

    Blekkulf Member

    Joined:
    May 18, 2018
    Messages:
    69
    Location:
    Molde
    Balance:
    354Coins
    Ratings:
    +13 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Joyrider, 6DOF
    Yeah I know. I put the 200 on top and 50 on bottom.. seem to work:) thanks anyway :thumbs
  8. Zed

    Zed VR Simming w/Reverb Gold Contributor

    Joined:
    Apr 4, 2017
    Messages:
    1,044
    Location:
    USA
    Balance:
    5,828Coins
    Ratings:
    +1,042 / 4 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
    That arrangement might cause issues though. Executing in the reverse sequence can let the lower gap values take over. Again, if the gap value was 201, they would all still all be true but the last command to execute in that sequence would be the one with gap value greater than 50 and would leave the speedpins both low instead of high.
  9. Blekkulf

    Blekkulf Member

    Joined:
    May 18, 2018
    Messages:
    69
    Location:
    Molde
    Balance:
    354Coins
    Ratings:
    +13 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Joyrider, 6DOF
    Hmm.. seems like it goes down to the one thats True, and then starts from the top again.. i dont get any signals from 150, 75 or 50 when its 201.
  10. Zed

    Zed VR Simming w/Reverb Gold Contributor

    Joined:
    Apr 4, 2017
    Messages:
    1,044
    Location:
    USA
    Balance:
    5,828Coins
    Ratings:
    +1,042 / 4 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
    Just going by the code you posted, with gap equal to 201, since all the statements would be true, the last one to be evaluated would be the assignment you’re left with with either ordering using just if statements. Maybe your code is now different from what I’m commenting on but unless I’m missing something, I believe that’s how it would work. It may not matter to how you use the speed pins but I don’t know.
    Last edited: Oct 5, 2021