Sending Serial Data From Processing to arduino to test reliability?

edited April 19 in Arduino

Ive been told if I want to test reliability of a serial connection, I would send data to arduino from processing, an have arduino send data back. By reliability I mean, lets say I have some sliders that when dragged from left to right, send info to a device I have connected to arduino UNO. Before the info gets sent to the device I want to verify that this info is free of errors and it will do what I intend for it to do before it gets sent out, via maybe a coding condition. So I was wondering how I would do this coding wise?

Answers

  • Before the info gets sent to the device I want to verify that this info is free of errors and it will do what I intend for it to do before it gets sent out

    Why are you concerned about errors? Free of errors? You need to comment more on that. Usually, you as the designer, you will know all the details of both parties, referring to Arduino and Processing. You know exactly what you need to send to the arduino, what format and what value ranges for example. On your arduino side, you have to make sure you catch the received data, you parse it and you process it based on some expectations.

    For example, your slider control in processing will have a range from 0 to 100. When you design your code, every time there is a slide action event detected on this control, your program will take the current slider position and send it to your arduino via serial. In this example, we know that we are sending data from the slider and it ranges from 0 to 100. On the arduino side, it reads the data and it uses this value for its internal operation. For example, setting up the volume of a microphone.

    You are talking about checking for errors which it is not clear from a design point of view. You know all the details of your project, and you will know exactly what you are sending including format, magnitudes, rate of transaction, etc.

    Kf

  • edited April 19

    When I say errors I mean lets say I move the slider, and the Arduino does not received the data to move my device? How would I know whether or not my Arduino got that info from Processing instead of looking to see whether or not my device moved?

    Since you have to use a mouse input to move the slider, can I test/do a check of whether the mouse was pressed/released/moved or not? Kinda like a function.

    Basically reliability is by definition from a micro-controller perspective, your testing your input multiple times, before you send out the data, then if you get your desired output base on your multiple checks, that info then gets sent to the device. So its kinda like 2 or 3 checks to ensure you are sending the proper info before it actually gets sent to its final destination, which in this case is the device connected to the Arduino. Since the input device is the mouse, and it needs to be used to press to drag the slider, we can do checks on this particular action, I think.

    So what I need is a way to do this?

    This is the best way I can put it. FYI Firmatta is a layer of code used on the Arduino so that we don't have to use the Arduino IDE AND Processing to write code. Once firmatta is uploaded we only use Processing IDE to communicate with the Arduino

  • So, like a println()?

  • If you use println, you can see exactly what you are sending to your arduino. If you try few times, you will find out the operation is consistent. If you use a slider, the values you get are consistent. If you set your slider, say, from a range from 0 to 100, then when you send the data, it will be from the current position of the slider in that range. No surprises there.

    Now, if you send the data and your ardunio didn't perform the operation properly, then there is an error in the receiving end of the software. From what I understand, when using firmatta, you are interacting directly with the arduino unit from processing. If your code is proper, then no error should occurred. Have you experienced errors like what you describe before?

    I think you are trying to catch an error that won't be there in first place. In other words, you can avoid any of these errors with a proper design. Let's go back to the 0 to 100 range case which is my gold example because there is no knowledge about the nature of your sensors nor your application. In the arduino side, you could check for the value that you received and if it is outside the expected range, you will discard the command. Going further, you could send a signal back to processing saying the status of the operation, if it was a success or a failure. Every time processing sends a signal, Processing listens for the return token describing if the command was successfully executed, or not.

    Kf

  • Ah ok I understand your logic. Its basically something like a ping. Processing sends out a signal and expects to get a reply. My only question is how would I implement this in my code? Here is what I have its only for 1 servo but I will add more once I get this aspect done:

    import processing.serial.*;          //imports serail library
    import cc.arduino.*;                 //imports arduino library so I don't have to use arduino IDE
    import controlP5.*;                  //Part of processing library
    
    Arduino arduino;
    ControlP5 cp5;
    
    Slider Servo1;
    void setup() 
    {
      size(950, 900);
    
      println(Arduino.list());                                  //sends data to serial port for arduino
      arduino = new Arduino(this, Arduino.list()[0], 57600);    //sets COMM Port Baud Rate
      cp5 = new ControlP5(this);                                //dynamic variable of control class
    
      arduino.pinMode(3, Arduino.SERVO); 
    Servo1 = cp5.addSlider(".").setRange(0,180).setValue(90).setPosition(110,134).setSize(270,25).setNumberOfTickMarks(270).setLabelVisible(true).setColorForeground(color(102,102,102)).setColorBackground(color(255,153,0)).setColorActive(color(102,102,102));
     void draw() 
    {
     //will fill alter
    
      background(color(51,102,153));                        //background of GUI color in RGB format
      arduino.servoWrite(3, int(Servo1.getValue())); }
    
  • For previous servo code, check: https://forum.processing.org/two/search?Search=servo

    Notice in your code, you are sending data in draw. However draw runs 60fps so you are writing to your servo 60 times per second. I don't know if this is your intention but I just wanted to make you aware. Please check this:

    https://processing.org/reference/draw_.html

    You can also check for firmatta code in the forum. I am going to guess you can find more sample code in the arduino.cc site. That could be a second alternative for you.

    Kf

  • edited April 20

    I think I should have been more specific. Where do I use the println() in my code to do the checks so that I can see if I am getting what i'm suppose to get, before the data gets outputted to the device I got connected to the arduino?

    Also thank you for the help so far, I am very grateful for it.

  • First, check a previous post working with servos. Link here https://forum.processing.org/two/discussion/comment/94090/#Comment_94090

    I will use the println after you write to the servo, so you can see what you sent to the servo. I believe that is what you want.

    Kf

  • edited April 20

    I did write to the servo, in my code that I posted in the function void draw. I am a little confused as to what you want me to do, as I see in the link you posted you are also writing to the servo in the function void draw, but its based on a condition?

  • I think the question is if it is working and if you are able to interact with your servo.

    That is the key feature of that example, to run the command in draw only under certain condition. I don't think it is a good idea to be sending commands 60fps but on the other hand, there is nothing wrong if your application calls for it. It depends how the arduino handles the data and what kind of instructions the servo expects.

    Kf

  • edited April 20

    Yes it is working, and I only put the write function in draw for convenience purposes.

  • I will put it right before you write to the servo. You will see that this is good for debugging purposes. Another option is to use this right before you write to your servo:

    text("Sending to servo: "+val,width/2,height/2);

    Where val is the actual data you are sending to the unit.

    Kf

  • edited April 20

    So val would be arduino.servoWrite(3, int(Servo1.getValue())) ? Also what does this text("Sending to servo: "+val,width/2,height/2); actually do/mean? I tried googling it and it doesn't really help me understand that line of code.

  • println output the value to the console and text prints the info directly into your sketch. They show exactly what you are sending to your device but they don't do anything else. I will suggest you try an example working with an actual servo so you can see how everything comes together.

    You are correct, the previous post should be instead:

    text("Sending to servo: "+Servo1.getValue(),width/2,height/2);
    arduino.servoWrite(3, int(Servo1.getValue()));
    

    Kf

  • edited April 20

    So I put text("Sending to servo: "+Servo1.getValue(),width/2,height/2); before I write to the servo then I would putprintln(arduino.servoWrite(3, int(Servo1.getValue())); ) after I write to the servo correct?

  • Text and print do the same thing at the end. I will stick to println. Text was just an additional option.

    Kf

  • But is my syntax for println correct?

  • But is my syntax for println correct?

    Have you tried it?

    Spoiler: no, your syntax is not correct. There is a random semicolon in your println statement and I think servoWrite() doesn't return any value so there is nothing to print.

    Really what you are trying to do is still very confusing for me. You could just put a println("Activating servo 1"); (or indeed a text("Activating servo 1", width/2, height/2);) where you place the arduino.servoWrite(3, int(Servo1.getValue())); line.

    If you must absolutely be certain that a command is received by the Arduino and is being executed, you can program it to send a command back. I work with a device that sends something like "ACK:XXXX" where XXXX is a command identifier number when everything went well, or "ERR" when I mangled the message and the device couldn't parse it.

  • How do I program it to send back a command?

  • @kloud

    If I were you, I will stick first to running a simple sample program. Is your servo working? When you get a sample code working, then one could build on top of this code.

    Kf

  • edited April 23

    Ok I got it to print on the Processing window and its working fine with the servo, now how do I ensure the Arduino communicates back?

    @colouredmirrorball I need your help or anyone else's for this part since I have no idea where to start? Some code would be a nice example?

  • Answer ✓

    @kloud

    Unfortunately I don't have any more ideas without checking the documentation. I suggest you explore the links they provide in the response on this question: http://stackoverflow.com/questions/16874096/firmata-servo-control

    Also I found this following link useful : https://www.arduino.cc/en/reference/firmata

    However, I am not sure if it applies to the Servo case because i don't know if you are allowed to read from it. Because you are using firmata, I don't think you can get an event response as discussed in this post. Maybe maybe if you areable to set an event callback as described in the previous link....

    Also explore this example: https://github.com/firmata/arduino/tree/master/examples/ServoFirmata

    Kf

  • Thank you very much for all your help

  • You are very welcome.

    Kf

Sign In or Register to comment.