1. 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 Download Package Now!
  2. 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
  3. 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 here. Do not following these rules will lead to permanent exclusion from this website: Read the forum rules.

Question Can't communicate with Arduino...

Discussion in 'DIY Motion Simulator Building Q&A / FAQ' started by Lebois, Dec 12, 2018.

  1. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    Hi all !

    I am trying to retrieve the data sent from Simtools to the Arduino with the simple following code :

    Code:
    /*
    Use this code to check what Simtools send to the Arduino
    */
    
    int incomingByte = 0;
    
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
      incomingByte = Serial.read();
    
      // say what you got:
      Serial.print("I received: ");
      Serial.println(incomingByte, DEC);
    }
    But the only answer that I get is : I received: -1

    I want to know the exact data sent from Simtools because I would like to use stepper motors to build a 2Dof sim rig.

    Here is the configuration :
    Capture.JPG
    Capture2.JPG
  2. noorbeast

    noorbeast VR - The Next Generation Staff Member Moderator

    Joined:
    Jul 13, 2014
    Messages:
    12,487
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    95,839Coins
    Ratings:
    +8,050 / 39 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
  3. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
  4. yobuddy

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

    Joined:
    Feb 9, 2007
    Messages:
    3,263
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    24,736Coins
    Ratings:
    +3,318 / 10 / -0
    Set DOF 1 for Axis 1a to 100% (your only using 4% of the output)
    Your sending the value as a Chr (binary) to the Arduino.(a chr can be a value of 0 to 255)
    (for testing set it to 8 bit output)
    Then you need to convert it back into a value in the ard.
    int_output = atoi(char_input);

    Let me know how it goes.
    yobuddy
  5. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    Thanks for your reply !

    So I will test the following code tonight :
    Code:
    /*
    Use this code to check what Simtools send to the Arduino
    */
    
      int char_input ;
      int int_output ;
     
    void setup() {
      Serial.begin(115200);
    }
    
    void loop() {
      char_input = Serial.read();
      int_output = atoi(char_input);
      // say what you got:
      Serial.print("I received: ");
      Serial.println(int_output);
    }
    • Like Like x 1
  6. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    I tried the following code, but it appears that no serial data goes to the Arduino...

    Code:
    /*
    Use this code to check what Simtools send to the Arduino
    */
    
      int char_input ;
      int int_output ;
     
    void setup() {
      Serial.begin(115200);
    }
    
    void loop() {
      if (Serial.available() > 0) {
      char_input = Serial.read();
      int_output = atoi(char_input);
      //say what you got:
      Serial.print("I received: ");
      Serial.println(int_output);
    }
    delay(500);
    Serial.println("nothing in last 500ms");
    }

    Attached Files:

  7. yobuddy

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

    Joined:
    Feb 9, 2007
    Messages:
    3,263
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    24,736Coins
    Ratings:
    +3,318 / 10 / -0
    Please remove the "P" from the output line and try again buddy.
    Make it simple with just "<Axis1a>" for the output.
    Chat soon,
    yobuddy
    • Like Like x 1
  8. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    I tried to remove the P, but it doesn't changed the problem.

    Now here is what I tried : if Serial.available()>0, light one led. And it works ! The Simtools send datas to arduino, the problem is that it probably doesn't managed to send it back to the computer...

    By the way, this seems to only work with the arduino nano, not with the Leonardo...
    Last edited: Dec 18, 2018
  9. yobuddy

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

    Joined:
    Feb 9, 2007
    Messages:
    3,263
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    24,736Coins
    Ratings:
    +3,318 / 10 / -0
  10. pmvcda

    pmvcda aka FlyPT

    Joined:
    Nov 3, 2010
    Messages:
    323
    Location:
    Portugal
    Balance:
    2,452Coins
    Ratings:
    +368 / 2 / -0
    My Motion Simulator:
    6DOF
    In your last picture, the Arduino serial monitor is open.
    You have to close it.
    You can't use the COM port simultaneously in the Arduino serial monitor and SimTools.
    Might not be this, but it shows it open in your pictures and it must be closed.
  11. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    Yes good advice, I will try this code. I will have to wait for the Uno to arrive... I started to read the code, far more complex than what I am used to...

    That's what I feared... So how can you read the data sent ? Maybe I will try to put an lcd panel on the arduino :)
  12. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    Hi again ! I make a code that allows to display pitch data on a LCD. Now the problem is that numbers are quite strange.. It goes from 0 to 255 but in a strange way.
    0% is 0 or 253 (when the cursor comes back and forth to the same position),
    50% is 0 or 255
    100% is 255
    -100% is 0
    -50% is 255
    Then we fo from 0% to 50% by 2 or by 3 (it change everytime...), the same goes for -100% to -50% etc

    Here is the code. And then the settings. Axe limiting is 100%.

    Code:
    //********************************************************************************************
    // Simtools Data to LCD code
    // Simtools output : P<Axis1a>C. Inspired by Sirnoname
    // By Aurélien Leca
    //********************************************************************************************
    int buffer           =  0 ;    // It takes the value of the serial data
    int buffercount      = -1 ;    // To count where we are in the serial datas
    int commandbuffer[5] = {0};    // To stock the serial datas in the good order.
    
    #include <LiquidCrystal.h> // includes the LiquidCrystal Library
    
    LiquidCrystal lcd(3, 5, 10,11,12,13); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
    
    void setup() {
      lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
      Serial.begin(115200);
    }
    
    void loop() {
    SerialWorker();
    }
    
    void SerialWorker(){
     
        while(Serial.available())
        {
            if(buffercount==-1)
            {
                buffer = Serial.read();
                if(buffer != 'P'){buffercount=-1;}else{buffercount=0;} // "P" is the marquer. If we read P, the next data is pitch
            }
            else   //  if(buffercount>=0)
            {
                buffer = Serial.read();
                commandbuffer[buffercount]=buffer;   // The first value next to "P" is saved in commandbuffer in the place "buffercount"
                buffercount++;
                if(buffercount > 2)
                {
                    if(commandbuffer[2]=='C'){    // If the third data is "C", then commandbuffer is full, we can use it
                    
                      lcd.print( commandbuffer[1]);
                      delay(100);
                      lcd.clear();
                    }
                    buffercount=-1;   // Re-initialize buffercount.
                }
            }
        }
    }

    Attached Files:

  13. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    1,809
    Location:
    Oklahoma City, USA
    Balance:
    13,265Coins
    Ratings:
    +1,554 / 30 / -1
    If you want 0 to 255 output use 8 bit output not 10 as you have it set now which is outputting 0 to 1023.
    • Informative Informative x 1
  14. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    That makes sense, but when I use 8 bit range output, nothing appears on the LCD...
  15. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    1,809
    Location:
    Oklahoma City, USA
    Balance:
    13,265Coins
    Ratings:
    +1,554 / 30 / -1
    The sketch appears to read 10 bit input but you're only printing the second byte to the lcd. That explains why the lcd shows 0 to 255 four times when moving the slider from far left to far right.
    • Informative Informative x 1
    Last edited: Dec 21, 2018
  16. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    Hm... First byte is P, second byte is the pitch value, so it's normal to only print the first one isn't ?
  17. BlazinH

    BlazinH Well-Known Member

    Joined:
    Oct 19, 2013
    Messages:
    1,809
    Location:
    Oklahoma City, USA
    Balance:
    13,265Coins
    Ratings:
    +1,554 / 30 / -1
    With 8 bit output P is first, value is second, and C is third, so with 8 bit yes. But with 10bit output (and up thru 16 bit) P is first, value hi is second, value low is third, and C is forth. To get the 10 bit value you have to multiply value hi by 256 then add value low to that.

    (value hi * 256) + value low
    • Like Like x 1
    Last edited: Dec 21, 2018
  18. Lebois

    Lebois Member Gold Contributor

    Joined:
    Dec 10, 2018
    Messages:
    32
    Balance:
    201Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    2DOF
    Thanks it works ! Here is the final code :

    Code:
    //********************************************************************************************
    // Simtools Data to LCD code
    // Simtools output : P<Axis1a>C. Inspired by Sirnoname
    // By Aurélien Leca
    //********************************************************************************************
    int buffer           =  0 ;    // It takes the value of the serial data
    int buffercount      = -1 ;    // To count where we are in the serial datas
    int commandbuffer[5] = {0};    // To stock the serial datas in the good order.
    
    #include <LiquidCrystal.h> // includes the LiquidCrystal Library
    
    LiquidCrystal lcd(3,5,10,11,12,13); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
    
    void setup() {
      lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
      Serial.begin(115200);
    }
    
    void loop() {
     // commandbuffer[1]=123;
     // lcd.print(commandbuffer[1]);
      //delay(1000);
        //lcd.clear();
    SerialWorker();
     
    }
    
    void SerialWorker(){
     
        while(Serial.available())
        {
            if(buffercount==-1)
            {
                buffer = Serial.read();
                if(buffer != 'P'){buffercount=-1;}else{buffercount=0;} // "P" is the marquer. If we read P, the next data is pitch
            }
            else   //  if(buffercount>=0)
            {
                buffer = Serial.read();
                commandbuffer[buffercount]=buffer;   // The first value next to "P" is saved in commandbuffer in the place "buffercount"
                buffercount++;
                if(buffercount > 2)
                {
                    if(commandbuffer[2]=='C'){    // If the third data is "C", then commandbuffer is full, we can use it
                    
                      lcd.print( (commandbuffer[0])*256+commandbuffer[1]);
                      delay(100);
                      lcd.clear();
                    }
                    buffercount=-1;   // Re-initialize buffercount.
                }
            }
        }
    }