10 Replies Latest reply on Jun 27, 2019 3:16 PM by neilk

    Sending multiple data from AppInventor to Arduino?

    bopele

      I have 2 LEDs. With Slider1, I will adjust the brightness of one, Slider2 and the other. When I press these buttons in Animation 1 or Animation 2, the LEDs will light up in various ways. I have received data from Slider1 and Slider2, but how do I make changes to the AppInventor and Arduino code to receive and use the button data?

        • Re: Sending multiple data from AppInventor to Arduino?
          neilk

          Hi bopele ane welcome to element14

           

          It would help if you can explain what you actually want to do when the button(s) are pressed.

           

          Neil

          1 of 1 people found this helpful
          • Re: Sending multiple data from AppInventor to Arduino?
            BigG

            Have you thought about what Arduino is receiving exactly.

             

            Remember, Arduino has no idea how you have designed the app. It just receives data from the Bluetooth Connection. So how are you telling Arduino this data is from Animation1 or Slider1 or Animation2 etc. You need to add in an identifier for example as all you are sending at present is a value.

            2 of 2 people found this helpful
              • Re: Sending multiple data from AppInventor to Arduino?
                bopele

                So I don't know exactly how to add this "identifier".

                  • Re: Sending multiple data from AppInventor to Arduino?
                    BigG

                    It is pretty easy.

                     

                    As you are sending text via the"call BluetoothClient1.SendText" option, just prefix what you have in the "Join" with something you can parse out on the Arduino side.

                     

                    For Example, for the Blue Slider you could use:

                    Join

                    - "Sb"

                    - round(blueSlide.thumbposition)

                    - "\n"

                     

                    For the Orange Slider you could use:

                    Join

                    - "So"

                    - round(orangeSlide.thumbposition)

                    - "\n"

                     

                    For the Blue Button (Animation1) it will be even easier - so with the Animation1.click, just sent this text "Ab\n"

                     

                    Then on Arduino side:

                     

                    You need to first "read" what is available via Bluetooth (your code does not show this) and place or append the character you have just read into a variable of String type.

                     

                    If you look in String object you will see a function startsWith()

                     

                    This is a handy function as you can now check if the string you've just received "startsWith" Sb and if so you know from this identifier it is Slider blue. If it "startsWith" Ab you know that is the Animation Blue button etc.

                    4 of 4 people found this helpful
                      • Re: Sending multiple data from AppInventor to Arduino?
                        bopele

                        Actually, I used the startsWith () function before, but I encountered a problem. There was no problem sending the data, but when I press the "button" from the phone, the operations in the void loop() (the section of DigitalWrite (11, HIGH)...) are stopped for a while.  How can I fix it?

                        1 of 1 people found this helpful
                          • Re: Sending multiple data from AppInventor to Arduino?
                            BigG

                            At the moment the picture showing your App Inventor blocks does not show any functions attached to the buttons (Animation1 and Animation2) so based on what you sent the buttons are not doing anything.

                             

                            bopele  wrote:

                             

                            There was no problem sending the data, but when I press the "button" from the phone, the operations in the void loop() (the section of DigitalWrite (11, HIGH)...) are stopped for a while.  How can I fix it?

                             

                            I'm not sure what you mean by "Stopped for a while". Is it more than delay(200) but it has not stopped indefinitely. Anyhow, I think I know what is happening as you are using the "readString" function. You need to look at how the readString function terminates, as in, knows when it has received all the text string data and returns this back to you.

                             

                            In my experience this is not a good function to use when streaming data.

                            1 of 1 people found this helpful
                            • Re: Sending multiple data from AppInventor to Arduino?
                              neilk

                              Your first solution - just for reading the sliders -  is fine as far as it goes, but it is dependent on always sending two integers, always in the same order. Your AppInventor code could be a little simpler but never mind it works!

                               

                              As BigG said, if you want to send some other, different data items as well, you will need to identify each data item with a prefix so that you can sort them out in the arduino code and act accordingly

                               

                              You say the above code snippet didn't work properly. You need to show the relevant AppInventor blocks and also copy and paste your Arduino code into the posting, rather than insert an image.

                              2 of 2 people found this helpful
                              • Re: Sending multiple data from AppInventor to Arduino?
                                neilk

                                Remember that AppInventor is EVENT driven. Each slider movement and each button press is an independent event and as such can trigger the transmission of a single. identifiable piece of data to the arduino.

                                 

                                So, instead of sending both slider positions and both button presses every time any event is triggered, just send the one responsible for the trigger, preceded by a suitable identifier. If you use a single character identifier (that gives you plenty of scope!), decoding the result in the arduino sketch will be very easy - eg, you could use a case statement.

                                 

                                Neil

                                2 of 2 people found this helpful
                        • Re: Sending multiple data from AppInventor to Arduino?
                          BigG

                          Yes as Neil indicated what you originally had works as you rely on the order to be consistent. blueSlider first, then orangeSlider. This becomes tricky when wanting to include button presses.

                           

                          I don't want to solve this for you, but could think about adding to this logic... which is to place or send the Button presses every time too but always before blueSlider and orangeSlider then you can infer that if these button values are zero then buttons were not pressed and therefore the next two values will be blueSlider and orangeSlider etc. Not saying this is the best way but just another option to think about.

                          1 of 2 people found this helpful