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

Mover Simulated Wind Arduino Sketch

Discussion in 'FlyPt Mover' started by pmvcda, Mar 21, 2020.

  1. dureiken

    dureiken Active Member

    Joined:
    Sep 28, 2016
    Messages:
    251
    Location:
    France
    Balance:
    1,248Coins
    Ratings:
    +170 / 2 / -0
    thanks

    must come from my fan, need 170 to go up. But wasn't the case 2 days ago, I don't understand

    Specs :

    upload_2023-3-28_19-24-14.png
  2. dureiken

    dureiken Active Member

    Joined:
    Sep 28, 2016
    Messages:
    251
    Location:
    France
    Balance:
    1,248Coins
    Ratings:
    +170 / 2 / -0
  3. GWiz

    GWiz Active Member

    Joined:
    May 12, 2019
    Messages:
    188
    Occupation:
    Dentist
    Location:
    Aberdeenshire, Scotland
    Balance:
    1,510Coins
    Ratings:
    +121 / 0 / -0
    My Motion Simulator:
    DC motor, Arduino, 6DOF
    Hi dureiken, I'm very much an amateur at this kinda stuff and just bodge things together whilst trying to work it out from reading on the internet. My attempts at coding have the elegance of a hippopotamus doing ballet. I've no idea if this will work, but I've inserted the basic premise from the Wind sketch into the PWM timer control sketch you linked. Try connecting the PWM of the fan to pin 9 and use the slider on the Left Wind channel from the Mover file I attached. You may need to add a REMAP filter if it only responds over certain values.

    But like I say, it may not work at all, in which case greater minds will hopefully shed some light.

    Code:
    const byte OC1A_PIN = 9;
    const byte OC1B_PIN = 10;
    
    const word PWM_FREQ_HZ = 20000; //Adjust this value to adjust the frequency
    const word TCNT1_TOP = 16000000/(2*PWM_FREQ_HZ);
    
    byte bytesBuffer[4] = {0};
    
    void setup() {
     
      // Start serial communication
      Serial.begin(115200, SERIAL_8N1);
     
      pinMode(OC1A_PIN, OUTPUT);
    
      // Clear Timer1 control and count registers
      TCCR1A = 0;
      TCCR1B = 0;
      TCNT1  = 0;
    
      // Set Timer1 configuration
      // COM1A(1:0) = 0b10   (Output A clear rising/set falling)
      // COM1B(1:0) = 0b00   (Output B normal operation)
      // WGM(13:10) = 0b1010 (Phase correct PWM)
      // ICNC1      = 0b0    (Input capture noise canceler disabled)
      // ICES1      = 0b0    (Input capture edge select disabled)
      // CS(12:10)  = 0b001  (Input clock select = clock/1)
     
      TCCR1A |= (1 << COM1A1) | (1 << WGM11);
      TCCR1B |= (1 << WGM13) | (1 << CS10);
      ICR1 = TCNT1_TOP;
    }
    
    void loop() {
      if (Serial.available())
    
      {
    
        // Read new byte and put it at the end of the buffer
    
        bytesBuffer[0]=bytesBuffer[1];
        bytesBuffer[1]=bytesBuffer[2];
        bytesBuffer[2]=bytesBuffer[3];
        bytesBuffer[3] = Serial.read();
    
        // Verify if we have a valid sequence
    
        if(bytesBuffer[0] == 0 && bytesBuffer[1] == 0)
    
        {
    
          // Set the pins values
    
          setPwmDuty(bytesBuffer[2]);
    
    
        }
        }
    
    
    }
    
    void setPwmDuty(byte duty) {
      OCR1A = (word) (duty*TCNT1_TOP)/100;
    }
  4. dureiken

    dureiken Active Member

    Joined:
    Sep 28, 2016
    Messages:
    251
    Location:
    France
    Balance:
    1,248Coins
    Ratings:
    +170 / 2 / -0
    thanks a lot, works like a charm with REMAP(VALUE;0;255;0;100) !!
    • Like Like x 1
    • Winner Winner x 1
  5. michel123

    michel123 Member

    Joined:
    Jan 12, 2024
    Messages:
    72
    Occupation:
    cadre sncf
    Location:
    lezignan-corbieres
    Balance:
    168Coins
    Ratings:
    +31 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Motion platform
    Hello, I used your code:

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

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

    void setup()
    {
    Serial.begin(115200);
    pinMode(statpin, OUTPUT);

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

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

    void loop()
    {
    //TCCR0B = TCCR0B & B11111000 | B00000001; // set timer 0 divisor to 1 for PWM frequency of 62500.00 Hz
    TCCR0B = TCCR0B & B11111000 | B00000101; // set timer 0 divisor to 8 for PWM frequency of 7812.50 Hz

    if (Serial.available())
    {
    uint8_t Speed=Serial.read();
    if(Speed>0)
    {
    motorGo(0, CW, Speed);
    motorGo(1, CW, Speed);
    }
    else if(Speed==0)
    {
    motorOff(0);
    motorOff(1);
    }
    }
    }

    void motorOff(int motor)
    {
    digitalWrite(inApin[motor], LOW);
    digitalWrite(inBpin[motor], LOW);
    analogWrite(pwmpin[motor], 0);
    }

    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);
    }
    }
    }

    Issue :
    I have 2 fans that I would like to control individually, with this code only 1 fan works and I am clearly not good at finding the cause!!!

    I use flypt mover, and I can run a single fan with...

    If someone could explain it to me, here is my code and what I have under flyPT mover (the rempa works very well for the fan which turns...)...

    I also attach the arduino sketch of my assembly to understand....

    Attached Files:

  6. michel123

    michel123 Member

    Joined:
    Jan 12, 2024
    Messages:
    72
    Occupation:
    cadre sncf
    Location:
    lezignan-corbieres
    Balance:
    168Coins
    Ratings:
    +31 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Motion platform
    Another code that works when I send the data to the arduino monitor: 000 255, motor 2 is running, 255 000, the right motor is running and if I put 255 255 both are working.

    Problem I put <L> and <R> with an output which gives me 000 000 under flyPT mover nothing seems to come out, as if arduino did not understand the sending of FlyPT ???? ?
    Help ! I don't see what is wrong, the code works very well under arduino with the monitor, why does it not work with FlyPT???

    // Define pins for L298
    #define ENA 10
    #define IN1 8
    #define IN2 9
    #define ENB 5
    #define IN3 7
    #define IN4 6

    void setup() {
    // Initialize serial communication at 9600 bits per second:
    Serial.begin(115200);


    TCCR2B = TCCR2B & B11111000 | B00000101;

    // Set all the motor control pins to outputs
    pinMode(ENA, OUTPUT);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(ENB, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);

    // Initially stop both fans
    // stopFans();
    }

    void loop() {
    // Check if data is available to read
    if (Serial.available() > 0) {
    // Read the incoming string:
    String command = Serial.readStringUntil('\n');

    // Parse the command
    parseCommand(command);
    }
    }

    // Function to parse and execute the command
    void parseCommand(String command) {
    // Example command format: "000 000"
    if (command.length() == 7) { // Ensure the command is the correct length
    int f1Value = command.substring(0, 3).toInt();
    int f2Value = command.substring(4, 7).toInt();

    // Constrain values to 0-255 range
    // f1Value = constrain(f1Value, 0, 255);
    // f2Value = constrain(f2Value, 0, 255);

    // Control fans
    controlFan1(f1Value);
    controlFan2(f2Value);
    }
    }

    // Function to control fan 1
    void controlFan1(int speed) {
    if (speed > 0) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, speed);
    } else {
    analogWrite(ENA, 0);
    }
    }

    // Function to control fan 2
    void controlFan2(int speed) {
    if (speed > 0) {
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
    analogWrite(ENB, speed);
    } else {
    analogWrite(ENB, 0);
    }
    }

    // Function to stop both fans
    // void stopFans() {
    // analogWrite(ENA, 0);
    // analogWrite(ENB, 0);
    // }

    Attached Files:

  7. michel123

    michel123 Member

    Joined:
    Jan 12, 2024
    Messages:
    72
    Occupation:
    cadre sncf
    Location:
    lezignan-corbieres
    Balance:
    168Coins
    Ratings:
    +31 / 0 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino, Motion platform
    • Winner Winner x 1