Display Different colors based on data received via bluetooth from sound sensor

You can write e.g. (5, 6) in script arduino. Then pin5 is RX and you connect TX pin with bt to it. Pin6 is TX and you connect RX with bt to it.
Then the arduino side will be fine and you need to improve the appinventor side program.

1 Like

ah, i am reading up on UART. I think i am understanding what youre saying

thank you for your input.

on the app inventor side, how do i create and increment the array of data and display the appropriate image for my project? im not sure if you say my current code blocks but here it is again

I think you would make changes to the arduino first. Then he tested the connection to the app. But in the app, make changes to the clock block.

  1. Now you have "bytesAvalibleToReceives> = 0", change to "bytesAvalibleToReceives> 0"

  2. You are reading all bytes as text. If you read everything, the second block of read tekst will have nothing to read. Consider what the data sent to the application will look like?

For testing, do this:


1 Like

Here's where I'm at now:

The data from the sensor/arduino work properly: i labeled the values coming in with an R,Y,&G depending on the magnitude, then on app inventor i set up the clock and inside it, when a value is detected with an R,Y,or G an image with the appropriate color will be made visible, then made invisible once the value is changed. Something is wrong with my loop and it only stays the green image. can anyone help me troubleshoot this?

You are not splitting the incoming messages individually, so that is causing your Clock1.Timer IF/THEN/ELSEIf cascade to pick up only the first R/Y/G letter in the input chunk. (I would not call it a message until it is delimitted.)

Also, I recommend setting the other 2 images invisible in each if/then/elseif branch, instead of afterwards. It's simpler and more direct.

Here's the standard BlueTooth Delimiter advice ...

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties

Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

I triple checked my arduino code and i confirmed i am using println(). I moved the visibility blocksto the same if else blocks. Now the delimiter placement is where im unsure.

So i set the delimiter byte to negative one here when the start display button is hit and the clock timer & bluetooth is enabledimage

at the end of my when timer cascade i just need to set my message variable global data to -1?


image

The Delimiter Byte should be set to 10. 10 = New Line in ASCII.


global message
(draggable)

You could also just set it in the Designer Attributes of the BlueTooth Client component just once.

I see,

I changed my delimiter byte to ten at the beginning of the clock timer then set my variable equal to the delimiter byte

but now the app shuts down once I press the start button. perhaps it is getting the values too rapidly? how do i implement a delay on the app inventor side? I tried a delay on the arduino side but it ruins the functionality of my code and the values being sent.

f6b9a4354ce6b81c2ab274710fae63b52a358ed2_2_448x500

(tearing out hair)

Thank you for your patience.

Like so?

It doesnt shut down now, but all the still visible

Remove the speech in the Clock Timer, and replace it to a call to this logger ...
https://groups.google.com/d/msg/mitappinventortest/II_RlElGddU/J0q_E-2rAwAJ

You need to see your input messages and their timing.

Hello friends,

There may be translation errors because I use a translator.

Byte 0,1,2 sent by Bluetooth ... I want it to change the picture according to this incoming data value, I couldn't figure it out. Thank you in advance for your help.

Are you receiving text?

I am receiving data via bluetooth.

How can you add 1 to a file name?

Dear ABG,

I may not be able to explain the problem fully because I use translation. Please I would be glad if you can help.

Example;

Bluetooth incoming data 0 - image0.png
Bluetooth incoming data 1 - image1.png
Bluetooth incoming data 2 - image2.png

To concatenate text use the text JOIN block.

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

If all your messages are only one character, ask for only one character per message.

Use text compare.

Do not add 1 to the image file names.