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

Arduino Code for Fan Speed from Dashboard

Discussion in 'SimTools compatible interfaces' started by CoachKalk, Sep 30, 2014.

  1. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    I got mine half working and havnt had time to try anymore. It's hard when you can't code but it must be pretty simple for someone who knows how
  2. Mamati

    Mamati Member

    Joined:
    Jan 12, 2011
    Messages:
    52
    Location:
    Istanbul/Turkey
    Balance:
    158Coins
    Ratings:
    +6 / 1 / -0
    I saw your video, it's not perfect but at least it works. You did it with the codes that you share right? What are you using... Arduino Uno and Monster Moto or something else?
  3. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Yes using ARDUINO and MM however I had problems with power constantly out putting through the MM so the fans always trying to spin. Maybe a dodgy MM or something but always a constant voltage did my head in in the end so gave up. May try again to sort it when I get back to my rig.

    Maybe I need to use a different h bridge for it I have no idea. I am useless at coding so don't really understand it.
  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,400Coins
    Ratings:
    +1,683 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    Guys this is the easiest way to drive a fan, with an Ard.
    rfp30n06le-arduino-lightbulb.png

    or even easier , its all on one pcb.
    NDRIVE-angle_large.png

    As you can see it will drive a fan , up to 60v at 20amps.
    And is driven by one PWM pin , higher the value on the pin the faster the fan spins.

    Or an RC driven code like my Model code can be modified to run a RC EC brushless controller to run a little but powerful fan too.
    • Informative Informative x 2
    • Winner Winner x 1
    • Useful Useful x 1
  5. Mamati

    Mamati Member

    Joined:
    Jan 12, 2011
    Messages:
    52
    Location:
    Istanbul/Turkey
    Balance:
    158Coins
    Ratings:
    +6 / 1 / -0
    eaorobbie,
    can you modify your "RC model" code for us. I can not code or modify.

    I have a RC brushless motor and ESC combo pack that I used on my rc buggy, and I am planning to put a rc model propeller on the motor in order to have the wind simulation on my RollerCoaster sim.
    Here is the wiring of the hardware, [​IMG] I need codes for Arduino to work with Game Dash.
    I think that once having the codes this hardware configuration will be the easiest and best way (as you mentioned) for wind (fan) simulation because they are cheap and there are many models for application needed as power, dimensions, long life, high RPM etc...
  6. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Ok i am soon to try again with my fans. I have ordered 2 of the freetronics mosfet boards and I have 4x 545 12v RC motors. I think I have the code which will work without using the MM so I will keep you all posted. Fingers crossed it may work this time :D lol
    • Like Like x 1
  7. Mamati

    Mamati Member

    Joined:
    Jan 12, 2011
    Messages:
    52
    Location:
    Istanbul/Turkey
    Balance:
    158Coins
    Ratings:
    +6 / 1 / -0
    Hope that you will succeed, I will cross my fingers for you as well.
    Waiting for your reply soon.
  8. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Ive done it Ive Done it !!!! Thought I would have 1 last try with the MM and I got it working, well just about lol. Still cant get the bloody thing to stop when the speed is at 0, still slight input causing voltage making the motor whine constantly.

    Does anyone know a shutdown command from the GameDash as the fan carries on trying to spin once I leave the game. @value1 @yobuddy

    Thanks to @eaorobbie for the original code that Ive just added to.

    This just works fine for no limits but other games I have no idea until I try it out.

    @Mamati I know you were interested in this so here it is


    #define BRAKEVCC 0
    #define CW 1
    #define CCW 2
    #define BRAKEGND 3
    #define CS_THRESHOLD 100

    int inApin[2] = {7, 4}; // INA: Clockwise input
    int inBpin[2] = {8, 9}; // INB: Counter-clockwise input
    int pwmpin[2] = {5, 6}; // PWM input
    int cspin[2] = {2, 3}; // CS: Current sense ANALOG input
    int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)
    int statpin = 13;


    // This just works with one variable from the Game Dash.
    int Speed ;
    int SpeedGameDash = 0;

    int bufferArray[4];

    void setup()

    {
    pinMode(11,OUTPUT);

    Serial.begin(9600);
    pinMode(statpin, OUTPUT);

    // Initialize digital pins as outputs
    for (int i=0; i<2; i++)
    {
    pinMode(inApin, OUTPUT);
    pinMode(inBpin, OUTPUT);
    pinMode(pwmpin, OUTPUT);
    }

    // Initialize braked
    for (int i=0; i<2; i++)
    {
    digitalWrite(inApin, LOW);
    digitalWrite(inBpin, LOW);
    }
    }


    void loop(){
    //****************************** READ DATA FROM SERIAL ******************************
    ReadData();
    CalcPWM();



    motorGo(0, CW, Speed); //Motor1
    motorGo(1, CW, Speed); //Motor2


    }

    void motorOff(int motor)
    {
    // Initialize braked
    for (int i=0; i<2; i++)
    {
    digitalWrite(inApin, LOW);
    digitalWrite(inBpin, LOW);
    }
    analogWrite(pwmpin[motor], Speed);
    }

    void motorGo(uint8_t motor, uint8_t direct, uint8_t Speed)
    {
    if (motor <= 1)
    {
    if (direct <=4)
    {
    // Set inA[motor]
    if (direct <=1)
    digitalWrite(inApin[motor], HIGH);
    else
    digitalWrite(inApin[motor], LOW);

    // Set inB[motor]
    if ((direct==0)||(direct==2))
    digitalWrite(inBpin[motor], HIGH);
    else
    digitalWrite(inBpin[motor], LOW);
    analogWrite(pwmpin[motor], Speed);
    }
    }
    }

    void ReadData(){
    if (Serial.available() == 4) { //if 4 bits available in the Serial buffer...

    int i;
    for (i=0; i<4; i=i+1) { // read and label each byte.
    bufferArray = Serial.read();
    }

    if (bufferArray[0] == 'S'){
    SpeedGameDash = ((bufferArray[1]-48)*100) + ((bufferArray[2]-48)*10) + // Take values 1-3.
    ((bufferArray[3]-48)*1);
    //Serial.print("You have set the Speed to a Value of:");
    //Serial.println(SpeedGameDash);
    }
    }
    }
    void CalcPWM() {

    if (SpeedGameDash > 0){
    int oldSpeed = Speed;
    Speed = map(SpeedGameDash, 0 ,50, 0 , 255); //change the "50" value to the speed of your choice
    if (oldSpeed != Speed){
    //Serial.print("Fan Speed set to :");
    //Serial.println(Speed);
    analogWrite(11,Speed);
    }
    }
    }



    If I can iron out these few problems it should work on pretty much any game with speed output. All we have to do is change the 50 value above to a speed that you want the fans to be on full speed. Simples really I just didnt understand it think I figured it out by luck

    Anyways hope some peeps make use of it :)
  9. yobuddy

    yobuddy Well-Known Member Staff Member Moderator SimAxe Beta Tester SimTools Developer Gold Contributor

    Joined:
    Feb 9, 2007
    Messages:
    5,166
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    48,214Coins
    Ratings:
    +5,038 / 16 / -0
    I believe the problem you’re having with the fans not stopping at '0' is because of this line.

    if (SpeedGameDash > 0){

    Since the value is zero, it never gets a chance to send the command to turn off the fans.
    yobuddy
  10. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Thanks @yobuddy for the reply :) I'll have a look see what I can do with it but my coding is pretty basic lol
    • Useful Useful x 1
  11. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Maybe I need to put a 10k resistor on pin 11 to ground perhaps that will work ? I will try later
  12. Mamati

    Mamati Member

    Joined:
    Jan 12, 2011
    Messages:
    52
    Location:
    Istanbul/Turkey
    Balance:
    158Coins
    Ratings:
    +6 / 1 / -0
    Very nice @SilentChill , I am looking forward to trying it but I have to buy first of all an additional Arduino and MM.
    I will let you know about my experiences.
  13. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Hi all got it working perfect now Motors shutoff as they should when the speed is at 0, no more voltage going to the motors YIPPPPEEEEEEEEEEEE.

    Heres the code

    #define BRAKEVCC 0
    #define CW 1
    #define CCW 2
    #define BRAKEGND 3
    #define CS_THRESHOLD 100

    int inApin[2] = {7, 4}; // INA: Clockwise input
    int inBpin[2] = {8, 9}; // INB: Counter-clockwise input
    int pwmpin[2] = {5, 6}; // PWM input
    int cspin[2] = {2, 3}; // CS: Current sense ANALOG input
    int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)
    int statpin = 13;


    // This just works with one variable from the Game Dash.
    int Speed ;
    int SpeedGameDash = 0;

    int bufferArray[4];

    void setup()

    {
    pinMode(11,OUTPUT);

    Serial.begin(9600);
    pinMode(statpin, OUTPUT);

    // Initialize digital pins as outputs
    for (int i=0; i<2; i++)
    {
    pinMode(inApin, OUTPUT);
    pinMode(inBpin, OUTPUT);
    pinMode(pwmpin, OUTPUT);
    }


    }


    void loop(){
    //****************************** READ DATA FROM SERIAL ******************************
    ReadData();
    CalcPWM();



    motorGo(0, CW, Speed); //Motor1
    motorGo(1, CW, Speed); //Motor2
    if (Speed=0 , motorOff);

    }

    void motorOff(int motor)
    {
    // Initialize braked
    for (int i=0; i<2; i++)
    {
    digitalWrite(inApin, LOW);
    digitalWrite(inBpin, LOW);
    }
    analogWrite(pwmpin[motor], Speed);
    }

    void motorGo(uint8_t motor, uint8_t direct, uint8_t Speed)
    {
    if (motor <= 1)
    {
    if (direct <=4)
    {
    // Set inA[motor]
    if (direct <=1)
    digitalWrite(inApin[motor], HIGH);
    else
    digitalWrite(inApin[motor], LOW);

    // Set inB[motor]
    if ((direct==0)||(direct==2))
    digitalWrite(inBpin[motor], HIGH);
    else
    digitalWrite(inBpin[motor], LOW);
    analogWrite(pwmpin[motor], Speed);
    }
    }
    }

    void ReadData(){
    if (Serial.available() == 4) { //if 4 bits available in the Serial buffer...

    int i;
    for (i=0; i<4; i=i+1) { // read and label each byte.
    bufferArray = Serial.read();
    }

    if (bufferArray[0] == 'S'){
    SpeedGameDash = ((bufferArray[1]-48)*100) + ((bufferArray[2]-48)*10) + // Take values 1-3.
    ((bufferArray[3]-48)*1);
    //Serial.print("You have set the Speed to a Value of:");
    //Serial.println(SpeedGameDash);
    }
    }
    }
    void CalcPWM() {

    if (SpeedGameDash > 0){
    int oldSpeed = Speed;
    Speed = map(SpeedGameDash, 0 ,60, 0 , 255);
    if (oldSpeed != Speed){
    //Serial.print("Fan Speed set to :");
    //Serial.println(Speed);
    analogWrite(11,Speed);
    }
    }
    }


    This is the line that I needed to put in so simple yet so frikkin hard when you have no idea what your doing lol

    if (Speed=0 , motorOff);

    Anyways I did it all by myslef im well proud of me, the wife doesnt care but i do :D :D :D



    Woohooo no stupid electrical whining noises anymore !!!!

    Videos to follow soon !!!
    • Winner Winner x 2
  14. yobuddy

    yobuddy Well-Known Member Staff Member Moderator SimAxe Beta Tester SimTools Developer Gold Contributor

    Joined:
    Feb 9, 2007
    Messages:
    5,166
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    48,214Coins
    Ratings:
    +5,038 / 16 / -0
    Nice work buddy!
    • Like Like x 1
    • Agree Agree x 1
  15. Mamati

    Mamati Member

    Joined:
    Jan 12, 2011
    Messages:
    52
    Location:
    Istanbul/Turkey
    Balance:
    158Coins
    Ratings:
    +6 / 1 / -0
    @SilentChill It is awesome but I could't try it because I had problem when loading your codes.
    I checked your code on Arduino after pasted it and Arduino mentioned lots of errors.
    Maybe the error is caused because I didn't wire the MM to Arduino yet...could it be reason?
    If yes what is the wiring between Arduino and MM?
    If no what could be the problem?
  16. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    MM is connected straight onto the top of the Arduino no wiring needed part from the power. The code should work fine ??

    Can you tell me what errors you are getting ?
  17. Mamati

    Mamati Member

    Joined:
    Jan 12, 2011
    Messages:
    52
    Location:
    Istanbul/Turkey
    Balance:
    158Coins
    Ratings:
    +6 / 1 / -0
    I named your code as "Fan" on arduino library
    Here is all error message



    Arduino:1.6.1 (Windows 7), Kart:"Arduino Uno"

    fan.ino: In function 'void setup()':

    fan.ino:34:23: error: invalid conversion from 'int*' to 'uint8_t {aka unsigned char}' [-fpermissive]

    In file included from fan.ino:9:0:

    C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:125:6: error: initializing argument 1 of 'void pinMode(uint8_t, uint8_t)' [-fpermissive]

    void pinMode(uint8_t, uint8_t);

    ^

    fan.ino:35:23: error: invalid conversion from 'int*' to 'uint8_t {aka unsigned char}' [-fpermissive]

    In file included from fan.ino:9:0:

    C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:125:6: error: initializing argument 1 of 'void pinMode(uint8_t, uint8_t)' [-fpermissive]

    void pinMode(uint8_t, uint8_t);

    ^

    fan.ino:36:23: error: invalid conversion from 'int*' to 'uint8_t {aka unsigned char}' [-fpermissive]

    In file included from fan.ino:9:0:

    C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:125:6: error: initializing argument 1 of 'void pinMode(uint8_t, uint8_t)' [-fpermissive]

    void pinMode(uint8_t, uint8_t);

    ^

    fan.ino: In function 'void motorOff(int)':

    fan.ino:61:25: error: invalid conversion from 'int*' to 'uint8_t {aka unsigned char}' [-fpermissive]

    In file included from fan.ino:9:0:

    C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:126:6: error: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)' [-fpermissive]

    void digitalWrite(uint8_t, uint8_t);

    ^

    fan.ino:62:25: error: invalid conversion from 'int*' to 'uint8_t {aka unsigned char}' [-fpermissive]

    In file included from fan.ino:9:0:

    C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:126:6: error: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)' [-fpermissive]

    void digitalWrite(uint8_t, uint8_t);

    ^

    fan.ino: In function 'void ReadData()':

    fan.ino:94:13: error: incompatible types in assignment of 'int' to 'int [4]'

    fan.ino: At global scope:

    fan.ino:119:1: error: 'This' does not name a type

    Derleme sırasında hata oluştu.

    Rapor daha fazla veri içermeli
    "Derleme sırasında ayrıntılı çıktı göster"
    Dosya > Tercihler'de etkinleştirilmiş.
  18. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    Ok possibly the formatting has been screwed up by the forum I will upload the file when I get home if I can :)
  19. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    I have changed a couple of things as well . we just use the game dash to control the speed for different games as it should do No need to change the code on the Arduino once it has the code on it. I didn't quite understand it but I do now lol I will create a new thread soon and upload the file to that.
    • Winner Winner x 1
  20. SilentChill

    SilentChill Problem Maker

    Joined:
    Jul 19, 2014
    Messages:
    2,619
    Occupation:
    Railway Maintenance
    Location:
    Morecambe, Lancashire, England
    Balance:
    20,402Coins
    Ratings:
    +3,480 / 34 / -0
    My Motion Simulator:
    DC motor, Arduino, Motion platform, 6DOF
    • Winner Winner x 1