Select list item: list index to large

I am making an application to control motors and take sensor data through arduino using Bluetooth connectivity and display it on the app. But there is a error saying "Select list item: List index too large select list item: attempt to get item number 2 of list of length 1:(39) ".
The data in the parenthesis is constantly changing.


project_robot.aia (1.7 MB)
Arduino code:-

#include <SoftwareSerial.h>
#include "dht.h"

#define echopin  A0 // echo pin
#define trigpin A1 // Trigger pin

SoftwareSerial BT(2, 3);

#define dht_pin A5 // Pin sensor is connected to
dht DHT;

int motor2Pin1 = 6; // pin 6 on L293D IC
int motor2Pin2 = 7; // pin 7 on L293D IC
int motor2EnablePin = 10; // pin 10 on L293D IC

int motor1Pin1 = 4; // pin 4 on L293D IC
int motor1Pin2 = 5; // pin 5 on L293D IC
int motor1EnablePin = 9; // pin 9 on L293D IC


int state;
int Speed = 200; 

int temp;
int hum;

int timer = 0;

int distanceFwd;
long duration;

int chk = 0;
int set = 35;

void setup() {
    pinMode (trigpin, OUTPUT);
    pinMode (echopin, INPUT );
   
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor1EnablePin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(motor2EnablePin, OUTPUT);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
    BT.begin(9600); // Setting the baud rate of Software Serial Library  
    delay(500); 
}

void loop() {
    //if some date is sent, reads it and saves in state
    if(BT.available() > 0){     
      state = BT.read(); 
      Serial.println(state);
      if(state > 10){
         Speed = state;}      
}
           
   
   distanceFwd = data();  
         
   analogWrite(motor1EnablePin, Speed);
   analogWrite(motor2EnablePin, Speed);
   
   if((distanceFwd<set) && (chk==1)){chk = 2; Stop();}
    if(distanceFwd>set){chk = 0;}
    
   // if the state is '1' the DC motor will go forward
   if ((state == 1) && (chk==0)){chk = 1; forword();Serial.println("Go Forward!");}

   // if the state is '2' the motor will Reverse
   else if (state == 2){backword();Serial.println("Reverse!");}
    
   // if the state is '3' the motor will turn left
   else if (state == 3){turnLeft();Serial.println("Turn LEFT");}
    
   // if the state is '4' the motor will turn right
   else if (state == 4){turnRight();Serial.println("Turn RIGHT");}
    
   // if the state is '5' the motor will Stop
   else if (state == 5) {Stop();Serial.println("STOP!");}    

timer = timer+1;
Serial.println(timer);

if(timer==200)
{
if(distanceFwd>200){distanceFwd=200;} 
 BT.print("A");
 BT.print(";");
 BT.print(distanceFwd); //send distance to MIT App
 BT.println(";");
}

if(timer>400)
{
 DHT.read11(dht_pin);
 hum = DHT.humidity;
 temp = DHT.temperature;
     
 BT.print("B");
 BT.print(";");
 BT.print(temp); //send distance to MIT App
 BT.print(";");
 BT.print(hum); //send distance to MIT App
 BT.println(";");    
 timer=0; 
}

delay(1); 
}

void forword(){
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}

void backword(){
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH); 
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}

void turnRight(){
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH); 
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}

void turnLeft(){
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}

void Stop(){
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
}

long data()
{
  digitalWrite(trigpin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin,HIGH);
  delayMicroseconds(10);
  duration=pulseIn (echopin,HIGH);
  return duration / 29 / 2; 
}

Delay 1?? In the loop?
Your app will be battered and soaked with messages, try a longer value.
Also show your AI blocks. They probably/certainly have an error too.
Cheers, Ghica

1 Like


I have also attached .aia file at the end of the error screen shot

1 Like

You did not use the message Delimiter facility properly, so you received fragments of messages.

Please see the Delimiter article in FAQ

Your original Clock Timer Blocks:

Corrected Clock Timer blocks:

1 Like

ABG beats me to it.
And make your delay at least 200.
Cheers, Ghica

I have corrected the clock timer block and also increased the delay to 200. Because of this the error message has stopped showing but the app is not displaying the data of ultra sonic sensor(i.e Label 5 where it should be displayed).
Thank you @ABG and @Ghica for you help in advance :smile:

Thank you for your new Downloaded Blocks in advance


278593387_1405127863234735_6366898940074859356_n
sir how to fix this?

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.