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

Question Need help with Arduino code

Discussion in 'Miscellaneous' started by Radioactive_pickle1, May 5, 2025 at 21:52.

  1. Radioactive_pickle1

    Radioactive_pickle1 New Member

    Joined:
    Mar 4, 2025
    Messages:
    6
    Balance:
    52Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    hi, I’m using an Arduino Uno connected to a CNC shield to control my T3DL motor drivers with SIM tools controlling it, but I can’t get SIM tools to control the motors. I have verified that my wiring is correct by running a test script to turn the motors without SIM tools. so I’m pretty sure it’s an issue on the same tools. and through my research, I’m fairly certain it’s an issue with my code. but I also provided an image of the serial connection settings. I’m using.


    simtools.JPG


    // ----------------- Configuration -----------------

    const int stepsPerRev = 1000; // microsteps per revolution



    // Motor pins (X, Y, Z, A)

    const int stepPins[4] = {2, 4, 6, 8}; // STEP pins

    const int dirPins[4] = {3, 5, 7, 9}; // DIR pins



    int lastPosition[4] = {0, 0, 0, 0}; // Remember last position for each motor



    // -------------------- Setup ----------------------

    void setup() {

    Serial.begin(115200);



    for (int i = 0; i < 4; i++) {

    pinMode(stepPins, OUTPUT);

    pinMode(dirPins, OUTPUT);

    digitalWrite(stepPins, LOW);

    digitalWrite(dirPins, LOW);

    }



    Serial.println("System Ready");

    }



    // -------------------- Main Loop ----------------------

    void loop() {

    if (Serial.available()) {

    String input = Serial.readStringUntil('\n'); // Read full line

    input.trim(); // Remove whitespace



    if (input.length() == 0) return;



    // Example input: 127,127,127,127

    int values[4];

    parseInput(input, values);



    for (int i = 0; i < 4; i++) {

    int targetStep = map(values, 0, 255, 0, stepsPerRev); // map to 0–1000

    moveMotor(i, lastPosition, targetStep);

    lastPosition = targetStep;

    }

    }

    }



    // ---------------- Parse Function -----------------

    void parseInput(String input, int* values) {

    int index = 0;

    while (index < 4 && input.length() > 0) {

    int commaIndex = input.indexOf(',');

    if (commaIndex == -1) {

    values[index] = input.toInt();

    break;

    }

    values[index] = input.substring(0, commaIndex).toInt();

    input = input.substring(commaIndex + 1);

    index++;

    }

    }



    // ---------------- Move Function ------------------

    void moveMotor(int motor, int current, int target) {

    int stepsToMove = abs(target - current);

    bool direction = (target > current);



    digitalWrite(dirPins[motor], direction);



    for (int i = 0; i < stepsToMove; i++) {

    digitalWrite(stepPins[motor], HIGH);

    delayMicroseconds(100); // Adjust pulse width

    digitalWrite(stepPins[motor], LOW);

    delayMicroseconds(100); // Adjust frequency

    }

    }
  2. GWiz

    GWiz Active Member

    Joined:
    May 12, 2019
    Messages:
    191
    Occupation:
    Dentist
    Location:
    Aberdeenshire, Scotland
    Balance:
    1,509Coins
    Ratings:
    +124 / 0 / -0
    My Motion Simulator:
    DC motor, Arduino, 6DOF
    It's a little hard to make out, but are those commas or periods in your 'Interface-Output' in the Simtools settings?
  3. Radioactive_pickle1

    Radioactive_pickle1 New Member

    Joined:
    Mar 4, 2025
    Messages:
    6
    Balance:
    52Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    There commas