List index too large help required

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
const int bp = 2;
const int lp1 = 4;
const int lp2 = 3;
const int buzz = 5;
int cp =0;
int fp = 0;
int item = 0;
double total = total;
int but = 0;

void setup()
{
pinMode(lp1,OUTPUT);
pinMode(lp2,OUTPUT);
pinMode(bp, INPUT);
pinMode(buzz,OUTPUT);
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522

}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));

}
Serial.println();
Serial.print(" ");
content.toUpperCase();
if (content.substring(1) == "A9 BB A0 B2") //change here the UID of the card/cards that you want to give access
{
return;
Serial.println("Welcome");
Serial.println("You can start shopping...");
Serial.println();
Serial.println("|");
Serial.println();
delay(2000);
//delay(3000)

}
int but = digitalRead(bp);

  if ((content.substring(1) == "99 F6 B5 6D")&&(but == LOW))
{
  return;
  Serial.println("PRODUCT ADDED:-");
  Serial.println();
  Serial.println("|");
  Serial.println(" REDBULL ");
  Serial.println();
  Serial.println("|");
  total = total + 90;
  cp++;
  digitalWrite(lp1,HIGH);
  delay(200);
  digitalWrite(lp1, LOW);
  Serial.println("QTY :");
  Serial.println();
  Serial.println("|");
  Serial.println(cp);
  Serial.println();
  Serial.println("|");
  Serial.println();
  delay(200);
  
}
    else if ((content.substring(1) == "99 F6 B5 6D")&&(but == HIGH ))
{
  if(cp > 0)
  {
    return;
  Serial.println("PRODUCT REMOVED");
  Serial.println();
  Serial.println("|");
  Serial.println(" REDBULL ");
  Serial.println();
  Serial.println("|");
  total = total - 20;
  cp--;
  Serial.println("QTY :");
  Serial.println();
  Serial.println("|");
  Serial.println(cp);
  Serial.println();
  Serial.println("|");
  digitalWrite(lp2,HIGH);
  delay(300);
  digitalWrite(lp2,LOW);
  tone(buzz,1000);
  delay(300);
  noTone(buzz);
  Serial.println();
  delay(200);
  
}
else
{
  Serial.println("product not in cart");
  Serial.println();
  delay(200);
  }
}

if((content.substring(1) == "89 ED 88 9C") && (but == LOW))
{
Serial.println("PRODUCT ADDED:-");
Serial.println();
Serial.println("|");
Serial.println(" BINGO ");
Serial.println();
Serial.println("|");
Serial.println();
total = total + 20;
fp++;
Serial.println("QTY:");
Serial.println();
Serial.println("|");
Serial.println(fp);
Serial.println();
Serial.println("|");
digitalWrite(lp1,HIGH);
delay(200);
digitalWrite(lp1, LOW);
Serial.println();
delay(200);

}

  else if((content.substring(1) == "89 ED 88 9C") && (but == HIGH ))
{
  if(fp > 0)
  {
    Serial.println("PRODUCT REMOVED:-");
    Serial.println();
    Serial.println("|");
  Serial.println(" BINGO ");
  Serial.println();
  Serial.println("|");
  total = total - 20;
  fp--;
  Serial.println("QTY:");
  Serial.println();
  Serial.println("|");
  Serial.println(fp);
  Serial.println();
  Serial.println("|");
  digitalWrite(lp2,HIGH);
  delay(300);
  digitalWrite(lp2, LOW);
  tone(buzz,1000);
  delay(300);
  noTone(buzz);
  Serial.println();
  delay(200);

}
else
{
  Serial.println("item not in cart");
  Serial.println();
  delay(200);
}
}
int items = cp+fp ;
 Serial.println(" Total number of items in the cart:-");
 Serial.println();
 Serial.println("|");
 Serial.println(items);
 Serial.println();
 Serial.println("|");
 Serial.println("The total price to be paid is:- ");
 Serial.println();
 Serial.println("|");
 Serial.println(total);
 Serial.println();
 Serial.println("|");
 Serial.println();
 delay(300);

}

Your app and your sketch have entirely different ideas of how to send and receive different pieces of data.

Here is one way to keep them in sync.

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.

Im sorry bt could you help me about what change i should do specifically cause i couldnt understand what you just said

Look through the samples at
https://community.appinventor.mit.edu/search?q=bluetooth%20category%3A12
for examples.

I notice you have a block to open Screen1.

Bluetooth connections in AI2 are lost when you switch screens.
It is easier to stay in one screen (Screen1) for your Bluetooth work.

Instead of splitting your input immediately at '|', just show it in a Label and watch it as it arrives. How many messages will have so many parts as you expect in your split?

Here is a sample logging procedure you can use to capture your input and save it for review ...
Clicked on log

logger_to_list.aia (2.6 KB) sample run when btnEnter Click

1 Like

Hello Shreyas

Off topic, but important - your Sketch is in grave danger of exceeding the Bluetooth data packet size (typically 21 to 23 bytes). you might consider sending short codes to the App and then look-up the code meaning from a list in the App blocks code.

For Example:
Code | App List Item
PA | Product Added
PR| Product Removed
RB | Red Bull
BO | Bingo
NO | Product not in cart

Note, you should not have any "Serial.println(); " entries in between Values, there should be just one at the end of all the other lines. Example:

Serial.print("PR");
Serial.print("|");
Serial.print("RB");
Serial.print("|");
total = total - 20;
cp--;
Serial.print(QTY);
Serial.print("|");
Serial.print(cp);
digitalWrite(lp2,HIGH);
delay(300);
digitalWrite(lp2,LOW);
tone(buzz,1000);
delay(300);
noTone(buzz);
Serial.println(); // = ascii char 10, tells App end-of-data
delay(200);

Note also, you must ensure the processing time in the App is approx 20% faster than the send time interval of the Sketch (Main loop time interval). The overall time interval should be as long as is practical.