2017년 7월 12일 수요일

Re: Error List index too large


I am researching about Arduino and control it through Blutooth and display data to Android.
I have problem with block list when design Android app. I want to display multi data sensor from Arduino ( light and temp number sensor) and separate each value sensor with "," into different label to display clearly
 When I run first programe without if.. then condition it display on Android that “Select list item: List index too large”
 Then I found in some forum that have to use if..then condition but unfortunately, it appeared that “ Bad Argrument to >  ….   ( in the pictue I attacted)
And can I use both string and number data from Arduino and send it to Android ?
Can anyone find out my issue and fix it for me. Thank you very much



--
For the 'list index too large' error you need to use 'Do it' to determine what value is being returned.
You are assuming that it is two values, but only one is being returned.

In the > error you are comparing a list containing one item to a number.
That is probably not what you wanted to do.

--
On the Arduino side, you need to send one pair of numbers per line.
Use print() ... print(",")... println() for  each line of output.

On the AI2 Bluetooth client in the Designer, set Delimiter to 
decimal 10 (= NL = LF = Line Feed = \n.)

In the AI2 blocks, you need two if/then guards protecting your read text block:
If connected and If bytes available > 0.

From the AI2 docs:


text ReceiveText(number numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.
As for -1 bytes, to get one complete line of data,
and proceed to split it at the comma from there.

--
What is the  (= NL = LF = Line Feed = \n.), Can you make an example block for me to understand.
On the Arduino side, I do as you say
On the AI2 Bluetooth client, I set Delimiter to decimal 10 and make a block as below but nothing display and no error

ps :I collape 2 if into 1 If (connected and bytes available > 0). Is it ok ?

--
What is the  (= NL = LF = Line Feed = \n.),
Those are four different ways you might encounter for people talking about the same byte value.
(Too much information on my part, sorry.)

All you needed was setting the decimal 10 in the Bluetooth client Designer attribute.

 ps :I collape 2 if into 1 If (connected and bytes available > 0). Is it ok ?
This does not help you diagnose problems.
I suggest adding a label lblConnected, containing either the text
"Connected" or "Not Connected", and a label lblIncoming showing the raw input,
and changing the blocks to:
When Clock1.Timer
  If BTClient1.IsConnected then
     set lblConnected.Text to "Connected"
      if BTClient1.BytesAvailabletoReceive > 0 then
          lblIncoming.Text = BTClient1.ReceiveText(-1)  <= asks for everything until delimiter
          (split the lblIncoming.Text  at "," and show the two parts, like you did)
      end if > 0
 else 
     set lblConnected.Text to "Not Connected"
end if connected


Also check to see if your clock timer is set to repeat, and is enabled, otherwise it won't fire.

This should give you more data to see what is going on.

If this still does not give you data, post your Arduino code text as an attachment for us to read.

--
Thank you for helping me. I'm successful in spilting data from Arduino and fixed the list arlert error. But I found one problem that make me uncomfortable ( I circled in the picture below)
Firstly, my data display exaclly but for a while ( about 30s - 1 minute) my data sensor on Android display confusion and about 3-5s it run normally and repeatly
Can you explain for me ? Thanks a lots !  

P/s :
And here is my code Arduino to display data Sensor :
"
mySerial.print("10"); mySerial.print(",");
  mySerial.print("Do_sang"); mySerial.print(",");
  digitalClockDisplay();mySerial.print(",");
  delay(1000);
"

 On designed I set BlutoothClient1 Delimiter Byte to 10 as you said  and check on HighByte First
                     I also check on TimeAlwayFire and TimerEnabled in Clock1






--
I don't see where you are calling println() to close off the end of each message.

That's necessary to keep the receiving end from slipping out of sync with the transmitting end.

You also need to request -1 bytes in the .ReceiveText block, to insure that you get only complete messages.

If you fail to do this, your receiver will lose track of where the beginning of each message starts, and everything will slide down in your display like you saw.

--
I have tried to add println(); in my Arduino code but in Android threre are blank line between 2 value, error still display. the labe to show name of sensor display not exactly line. I know it's because of println() code and I can fix it in App Inventor. when I remove println(); It still work well despite the error I showed you

About -1 bytes in the .ReceiveText block

I make a labe to check byte to recive and I found out that when error byte available to recive # 0 (1,3,23,...) and when nomally byte = 0;

I dont't understand this, because when I set label 4 text to -  call BlutoothClient ReceiveText number of Byte to "-1" , nothing display

Can you make block clearly. Thanks

--
About -1 bytes in the .ReceiveText block
I make a labe to check byte to recive and I found out that when error byte available to recive # 0 (1,3,23,...) and when nomally byte = 0;
???
Please include screen shots of everything
I dont't understand this, because when I set label 4 text to -  call BlutoothClient ReceiveText number of Byte to "-1" , nothing display



text ReceiveText(number numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

Are you sending a delimiter byte ?

Maybe it's time to search this board for "Arduino" and "Bluetooth" for samples?

--

댓글 1개: