RE: Runtime Error: The list cannot get item number 2 of list length 1!

https://mit.appinventor.mit.edu/t/runtime-error-the-list-cannot-get-item-number-2-of-list-length-1/104701/7
Hello, I hope you are well, please I need your help with a similar project, I am making an Arduino circuit with an ultrasonic sensor and a DTH11 temperature and humidity module, the values ​​of these will be printed in the application via Bluetooth. However, when I connect to the application, it works fine for a while and then the message Select list item: Attempt to get item number 2 of a list of length 1: I appears. I


still can't find the solution, this is my code.

(Canned Reply ABG - Public Only)

I do not provide private support.
You will get more informed and quicker worldwide support by asking in a new public thread.

Making this thread public.

...

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

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.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

...

This is my code:

#include "ABlocks_DHT.h"

int TEMPERATURA;
int HUMEDAD;
int DISTANCIA;
DHT dht4(4,DHT11);

double fnc_ultrasonic_distance(int _t, int _e){
unsigned long dur=0;
digitalWrite(_t, LOW);
delayMicroseconds(5);
digitalWrite(_t, HIGH);
delayMicroseconds(10);
digitalWrite(_t, LOW);
dur = pulseIn(_e, HIGH, 18000);
if(dur==0)return 999.0;
return (dur/57);
}

void setup()
{
pinMode(4, INPUT);
pinMode(2, OUTPUT);
pinMode(3, INPUT);

Serial.begin(9600);
Serial.flush();
while(Serial.available()>0)Serial.read();

dht4.begin();

}

void loop()
{

DISTANCIA = fnc_ultrasonic_distance(2,3);
Serial.print(DISTANCIA);
Serial.print(String("|"));
TEMPERATURA = dht4.readTemperature();
Serial.print(TEMPERATURA);
Serial.print(String("|"));
HUMEDAD = dht4.readHumidity();
Serial.print(HUMEDAD);
Serial.print(String("|"));

delay(1000);

Is it okay? I just modified this part of the code.. could you help me please I am pretty new here

Did you read ANY of what I posted?

Sorry, I copied the wrong code (it was the first I was using).... I did all the modifications you told me and now it works really well, thanks a lot for the explanation, and again, sorry for posting the incorrect code. Your post helped me a lot!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.