Visualising button presses on MIT App

For a project for uni I have to develop a simple app.

I have a ItsyBitsy (something similar to an arduino) connected to 4 buttons and a bluetooth module. 15s after the last button press, the HC-42 BLE module sends a couple of strings. Each string looks something like this "1,2.4,0.35" the first number (in this case 1) represents the button that is pressed, the second number (in this case 2.4) represents the time the button has been pressed, and the third number (in this case 0.35) represents the time the button has been pressed for. I have 4 canvasses, each corresponding to a button (or at least that is the idea). When the buttons have been pressed, I want the canvasses to change backgroundcolors to yellow on the time of the button press and for the same length of the button press, visualising the button presses on the phone.
It doesn't work yet, although the strings are received in Label3. Can anyone help me out, I have no idea what is going wrong and how to fix it so it works?


image

image

  1. You don't need to verify any numbers received (received as text).
  2. Collect the values and assign the vars first.
  3. Since you are receiving the duration of the button press, you don't need the start time. So, Button Number and Duration - 2 values.
  4. Set the Button's Canvas Background (I would probably use an image instead of a Canvas) to the required colour.
  5. Set the Clock Timer duration to Button Duration, then enable the Timer. The Timer should stop itself and return Canvas to the default colour.

If the buttons are going to be pressed randomly, it makes sense to have a separate timer for each button, yes - but the first thing it should do is disable itself.

See example below:

1 Like

There is a disconnect in your data flow.

How does global lijst get loaded with data from incoming BLE stringValues?

Exactly two strings?

So you are expecting exactly two button presses, as opposed to recording an indefinitely long piano recital on the 4 buttons?

As many strings as buttons have been pressed before there has been no button presses for 15s. If 6 buttons have been pressed, and then for 15s no buttons pressed, then 6 strings will be sent for example.

Thank you so much for the example, that helps a lot! The buttons will be pressed randomly unfortunately

Before diving in to how to receive and process your sheet music, let me first point out some mistakes in your blocks.

  1. You are missing the step where you take the chosen item from the list of stringValues and split it at commas into global lijst (which needs a more expressive name, like note_parts).
  2. Don't confuse string length (number of characters) with list length (number of items).
  3. The Is Number block returns true or false, and is good for validating numerical data, but is not meant to be stored as the number itself.

And I tried this, but the string value does not get accepted as a number, as the string value is received as text, if that makes sense haha, how could I fix that?

This project gets easier if you divide it into three stages:

  1. Receiving the sheet music via BLE, and saving it into a 3 column table (button number, start seconds, duration seconds)
  2. Transform the sheet music table into a program table for step 3, with three columns (deadline in milliseconds from start of piece, indicator (button) number, indicator color). This table should have twice the number of rows as the sheet music table, and should be sorted in ascending deadline order.
  3. Play the program table under control of a fast clock, keeping a start time in a global variable. Each cycle, if the program table is not yet an empty list, compare SystemTime - start time against the item 1 deadline. If due, do its color change and remove item 1. Otherwise do nothing this clock cycle.

I really appriciate your help! Only problem is I fear I am not smart enough to understand all this haha.

For the project we are programming a smart skateboard, which can detect your footprint and replay it on your phone, which I am making (a very simple version with only 4 sensors, or buttons to keep it simple for now) in the MIT app. Each button is like a sensor that detects your footprint, and that order and length should be shown on the phone.

The sheet music reference gets quite confusing (especially as English is not my native language:)

Now your app looks more like a stacked bar chart, one stack per sensor, with height mapping to duration.

AI2 has a stacked bar chart component:

I don't think so, the app looks like this:


As you see 4 gray canvasses, and each corresponding to a button. The canvas should light up when the corresponding button has been pressed.

App Inventor sees "text numbers" as numbers. If that isn't happening, there must be a tiny issue somewhere, perhaps the string itself, perhaps the text, perhaps the character encoding (UTF8 or ASCII are best for this).

A quick fix can be to add zero to the text number.

Tried that but this is the error I receive: The operation select list item cannot accept the arguments: , [[com.google.appinventor.components.runtime.Canvas@1cede58, com.google.appinventor.components.runtime.Canvas@ce3abb1, com.google.appinventor.components.runtime.Canvas@a87fc17, com.google.appinventor.components.runtime.Canvas@b7f8804]], ["["01"]

but you error says list of lists for list also the index too is in list it seems

Tried that but this is the error I receive: The operation select list item cannot accept the arguments: , [[com.google.appinventor.components.runtime.Canvas@1cede58, com.google.appinventor.components.runtime.Canvas@ce3abb1, com.google.appinventor.components.runtime.Canvas@a87fc17, com.google.appinventor.components.runtime.Canvas@b7f8804]], ["["01"]

also the index place is not strutured properly

This is the cause of that error:

The BLE stringValues is a list of incoming string messages (though I have yet to see it exceed one item.)

If you try to do a text operation like SPLIT on a list, AI2 translates the list into text, JSON by default. So the list becomes [01,...,...] and you are presented with "[01 as item 1 of the split result.

The solution to this is to apply the split to only one item of stringValues, usually in a For Each Item in List stringValues loop.

1 Like

Like this:

MultiClocks.aia (201.4 KB)

As you can see, the fault there is that wayward ["["01"]

I think we need to see the Script for the ItsyBitsy.

I fixed it, it works now!!! Due to your help:)) Thank you so so much!!!

1 Like