Select list item:List index too large

Hi all, i have encountered this error: Select list item: List index too large


This is my code blocks


This is my arduino code

#include <Wire.h>                    //For LCD
#include <LiquidCrystal_I2C.h>       //For LCD
#include <Servo.h>                   //For Servo
#include <SoftwareSerial.h>          //For Bluetooth

Servo myservo;                       //Servo 1
Servo myservo2;                      //Servo 2
Servo myservo3;                      //Servo 3

LiquidCrystal_I2C lcd(0x27, 16, 2);  //GND=GROUND,VCC=5V,SCL=A4,SDA=A5

int IR = 2;                          //digital Pin 2 as IR sensor 
int IR2 = 3;                         //digital Pin 3 as IR sensor
int pushButton = 4;                  //digital Pin 4 as PushButton
int TX = 5;                          //digital Pin 5 as TX pin for Bluetooth
int RX = 6;                          //digital Pin 6 as RX pin for Bluetooth
int Echo = 9;                        //Sets the Echo as an Input for Ultrasonic
int Trigger = 10;                    //Sets the Trigger as an Output for Ultrasonic
int LED = 11;                        //digital Pin 11 as Red LED
int LED2 = 12;                       //digital Pin 12 as Green LED
int Buzzer = 13;                     //digital Pin 13 as Buzzer

SoftwareSerial HC05(TX, RX);     //For Bluetooth
String BT_data="";                      //Data From HC-05 for Bluetooth
String Arduino_data="";                 //Data From Arduino to HC-05 
String lcdBTData = "";

// defines variables
long duration;                       //For Ultrasonic
int distance;                        //For Ultrasonic
int safetyDistance;                  //For Ultrasonic

bool scenario1Active = false;        // Variable to track the state of scenario 1

void setup() {
  pinMode(IR, INPUT);
  pinMode(IR2, INPUT);
  pinMode(pushButton, INPUT);
  pinMode(Trigger, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Buzzer, OUTPUT);
 
  myservo.attach(7);   
  myservo.write(0);
  myservo2.attach(8);  
  myservo.write(0);
  myservo3.attach(A0); 
  myservo.write(0);
  
  lcd.begin();
  lcd.backlight();
  lcd.print("System On");

  Serial.begin(9600);   
  HC05.begin(9600); 
}

void loop() {
  digitalWrite(Trigger, LOW); 
  delayMicroseconds(2);
  
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trigger, LOW);

  duration = pulseIn(Echo, HIGH);       
  distance = duration * 0.034 / 2;      
  safetyDistance = distance;            
  Serial.print("Ultrasonic Value: ");                                                                                                   
  Serial.println(distance);                                                                                                            
  
  int sensorValue = digitalRead(IR);                                                                                                      
  int sensorValue2 = digitalRead(IR2);                                                                                                    
  
  if ((safetyDistance <= 5)||(BT_data == "2"))            //**Scenario 1**
  {
    scenario1Active = true;  
    tone(Buzzer, 150);       
    delay(50);
    noTone(Buzzer);
    delay(50);
    digitalWrite(LED, HIGH); 
    delay(50);
    digitalWrite(LED, LOW);
    delay(50);
    lcd.clear();
    lcd.print("Obstruction");
    myservo.write(100);                         
    myservo2.write(40);                         
    myservo3.write(20);                        
    digitalWrite(LED2, LOW);
    
  } else {
    scenario1Active = false;            
  }
  if ((!scenario1Active && sensorValue == LOW)||(BT_data == "3"))                                   //**Scenario 2**
  {
    tone(Buzzer, 150);
    digitalWrite(LED, HIGH);
    lcd.clear();
    lcd.print("Train Coming ");
    myservo.write(100);
    digitalWrite(LED2, LOW);
  }

  if ((sensorValue2 == LOW)||(BT_data == "4"))                                                 //**Scenario 3**
  {
    noTone(Buzzer);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED, LOW);
    lcd.clear();
    lcd.print("Safe to Pass ");
    myservo.write(0);
    myservo2.write(0);
  }
  if ((BT_data == "5")|| (digitalRead(pushButton) == HIGH))                                                                          //**Scenario 4**
  {
    noTone(Buzzer);
    digitalWrite(LED, LOW);
    digitalWrite(LED2, LOW);
    lcd.clear();
    myservo.write(0);
    myservo2.write(0);
    myservo3.write(0);  
  }
  if (BT_data == "6")                                                                        
  {
   myservo.write(100);
  }
  if (BT_data == "7")                                                                         
  {
   myservo2.write(40);  
  }
  if (BT_data == "8")                                                                         
  {
    digitalWrite(LED, HIGH);
  }
  if (BT_data == "9")                                                                         
  {
    digitalWrite(LED2, HIGH);
  }
   if (BT_data == "10")                                                                         
  {
    tone(Buzzer, 150);
  }
 
  if (HC05.available())
  {
    BT_data = HC05.readString(); 
    Serial.println(BT_data);     
    lcd.setCursor(0,1);          
    //lcd.print(BT_data);          
    if (BT_data != "2" && BT_data != "3" && BT_data != "4"&& BT_data != "5" && BT_data != "6" && BT_data != "7" && BT_data != "8" && BT_data != "9" && BT_data != "10") {
  lcd.print(BT_data);
}
  }
  if (Serial.available())                                                             //**Scenario 5**
  {
    Arduino_data = Serial.readString();
    BT_data = "";                       
    HC05.print("\r");   
    HC05.println(Arduino_data);         
  }
   HC05.print(distance);
  HC05.print("|");
  HC05.print(sensorValue);
  HC05.print("|");
  HC05.print(sensorValue2);
  HC05.print("|");
  HC05.println(Arduino_data); 
  delay(1000);
}

Really appreciate if could help as I'm at lost.

(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.
export_and_upload_aia

Hi there,
im really sorry i posted the wrong image previously
this is my block diagram




this is my arduino code

#include <Wire.h>                    //For LCD
#include <LiquidCrystal_I2C.h>       //For LCD
#include <Servo.h>                   //For Servo
#include <SoftwareSerial.h>          //For Bluetooth

Servo myservo;                       //Servo 1
Servo myservo2;                      //Servo 2
Servo myservo3;                      //Servo 3

LiquidCrystal_I2C lcd(0x27, 16, 2);  //GND=GROUND,VCC=5V,SCL=A4,SDA=A5

int IR = 2;                          //digital Pin 2 as IR sensor 
int IR2 = 3;                         //digital Pin 3 as IR sensor
int pushButton = 4;                  //digital Pin 4 as PushButton
int TX = 5;                          //digital Pin 5 as TX pin for Bluetooth
int RX = 6;                          //digital Pin 6 as RX pin for Bluetooth
int Echo = 9;                        //Sets the Echo as an Input for Ultrasonic
int Trigger = 10;                    //Sets the Trigger as an Output for Ultrasonic
int LED = 11;                        //digital Pin 11 as Red LED
int LED2 = 12;                       //digital Pin 12 as Green LED
int Buzzer = 13;                     //digital Pin 13 as Buzzer

SoftwareSerial HC05(TX, RX);     //For Bluetooth
String BT_data="";                      //Data From HC-05 for Bluetooth
String Arduino_data="";                 //Data From Arduino to HC-05 
String lcdBTData = "";

// defines variables
long duration;                       //For Ultrasonic
int distance;                        //For Ultrasonic
int safetyDistance;                  //For Ultrasonic

bool scenario1Active = false;        // Variable to track the state of scenario 1

void setup() {
  pinMode(IR, INPUT);
  pinMode(IR2, INPUT);
  pinMode(pushButton, INPUT);
  pinMode(Trigger, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Buzzer, OUTPUT);
 
  myservo.attach(7);   
  myservo.write(0);
  myservo2.attach(8);  
  myservo.write(0);
  myservo3.attach(A0); 
  myservo.write(0);
  
  lcd.begin();
  lcd.backlight();
  lcd.print("System On");

  Serial.begin(9600);   
  HC05.begin(9600); 
}

void loop() {
  digitalWrite(Trigger, LOW); 
  delayMicroseconds(2);
  
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trigger, LOW);

  duration = pulseIn(Echo, HIGH);       
  distance = duration * 0.034 / 2;      
  safetyDistance = distance;            
  Serial.print("Ultrasonic Value: ");                                                                                                   
  Serial.println(distance);                                                                                                            
  
  int sensorValue = digitalRead(IR);                                                                                                      
  int sensorValue2 = digitalRead(IR2);                                                                                                    
  
  if ((safetyDistance <= 5)||(BT_data == "2"))            //**Scenario 1**
  {
    scenario1Active = true;  
    tone(Buzzer, 150);       
    delay(50);
    noTone(Buzzer);
    delay(50);
    digitalWrite(LED, HIGH); 
    delay(50);
    digitalWrite(LED, LOW);
    delay(50);
    lcd.clear();
    lcd.print("Obstruction");
    myservo.write(100);                         
    myservo2.write(40);                         
    myservo3.write(20);                        
    digitalWrite(LED2, LOW);
    
  } else {
    scenario1Active = false;            
  }
  if ((!scenario1Active && sensorValue == LOW)||(BT_data == "3"))                                   //**Scenario 2**
  {
    tone(Buzzer, 150);
    digitalWrite(LED, HIGH);
    lcd.clear();
    lcd.print("Train Coming ");
    myservo.write(100);
    digitalWrite(LED2, LOW);
  }

  if ((sensorValue2 == LOW)||(BT_data == "4"))                                                 //**Scenario 3**
  {
    noTone(Buzzer);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED, LOW);
    lcd.clear();
    lcd.print("Safe to Pass ");
    myservo.write(0);
    myservo2.write(0);
  }
  if ((BT_data == "5")|| (digitalRead(pushButton) == HIGH))                                                                          //**Scenario 4**
  {
    noTone(Buzzer);
    digitalWrite(LED, LOW);
    digitalWrite(LED2, LOW);
    lcd.clear();
    myservo.write(0);
    myservo2.write(0);
    myservo3.write(0);  
  }
  if (BT_data == "6")                                                                        
  {
   myservo.write(100);
  }
  if (BT_data == "7")                                                                         
  {
   myservo2.write(40);  
  }
  if (BT_data == "8")                                                                         
  {
    digitalWrite(LED, HIGH);
  }
  if (BT_data == "9")                                                                         
  {
    digitalWrite(LED2, HIGH);
  }
   if (BT_data == "10")                                                                         
  {
    tone(Buzzer, 150);
  }
 
  if (HC05.available())
  {
    BT_data = HC05.readString(); 
    Serial.println(BT_data);     
    lcd.setCursor(0,1);          
    //lcd.print(BT_data);          
    if (BT_data != "2" && BT_data != "3" && BT_data != "4"&& BT_data != "5" && BT_data != "6" && BT_data != "7" && BT_data != "8" && BT_data != "9" && BT_data != "10") {
  lcd.print(BT_data);
}
  }
  if (Serial.available())                                                             //**Scenario 5**
  {
    Arduino_data = Serial.readString();
    BT_data = "";                       
    HC05.print("\r");   
    HC05.println(Arduino_data);         
  }
   HC05.print(distance);
  HC05.print("|");
  HC05.print(sensorValue);
  HC05.print("|");
  HC05.print(sensorValue2);
  HC05.print("|");
  HC05.println(Arduino_data); 
  delay(1000);
}

this is my AIA file
JS_Fin.aia (6.4 MB)

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.

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.

1 Like

What do you get in the "input" variable?

In case you could not translate my post, here is the fix in draggable blocks ...

2 Likes

Thank you for your help, really appreciate it. It seems to work now.
I am so glad you're here, thank you!

By the way, there's no need to empty out those two variables at the end of the clock timer.

In fact, it's better for debugging to leave data in place.

Do you wash paper plates?

1 Like

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