select list item attempt to get item number 2 of a list of length 1

#include <SoftwareSerial.h>

#define TX_PIN 9
#define RX_PIN 8

SoftwareSerial bluetooth(RX_PIN, TX_PIN);
long BTBAUD=57600;
char lenhbat;

int relay1 = 10;
int giobat = 0;
int phutbat = 0;
int giaybat = 0;

char lenhtat;

int giotat = 0;
int phuttat = 0;
int giaytat = 0;

//==============end bluetooth============

#include <IRremote.h>
int RECV_PIN = 11; // the pin where you connect the output pin of sensor
byte led1 = 6;//OUT1
byte led2 = 5;//AUTO1
int pv=3;
// dau vao tin hieu tm pin mat troi
int CM = 4;//CHUYEN DIỆN LƯỚI
//int v; // biến tính toán chuyển mạch
int u;
int t=255;// trạng thái led1
int a=255;// trạng thái led2

//
void setup()
{

//=========khoi setup bluetooth==========
bluetooth.begin(BTBAUD);

//==========ket thuc koi tao=============
Serial.begin(9600);
//Serial.begin(19200);
// you can ommit this linesss

pinMode(led1, OUTPUT); // MAIN
pinMode(led2, OUTPUT); // AUTO
pinMode(7, OUTPUT); //TRUNG GIAN MỞ AUTO

pinMode(13,OUTPUT);
pinMode(CM,OUTPUT); // CHUYỂN MẠCH
pinMode(pv,INPUT); //

pinMode(A0,INPUT); // ĐẦU VÀO PV
pinMode(A1,INPUT);
pinMode(A2,INPUT); // ĐỌC ĐẦU VÀO MAIN
pinMode(A3,INPUT); // ĐỌC ĐẦU VÀO AUTO

digitalWrite(7,LOW);
digitalWrite(13,LOW);
digitalWrite(CM,LOW);
digitalWrite(2,LOW); // RESET

analogWrite(5,0);
analogWrite(6,0);

//==============================
(giobat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giobat);
bluetooth.print(" : ");
(phutbat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phutbat);
bluetooth.print(" : ");
(giaybat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaybat);
bluetooth.println();

(giotat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giotat);
bluetooth.print(" : ");
(phuttat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phuttat);
bluetooth.print(" : ");
(giaytat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaytat);
bluetooth.println();

}

void loop() {

HENGIO_BATDEN();
HENGIO_TATDEN();

}

void HENGIO_BATDEN(){

if (bluetooth.available()>0 ) {
lenhbat=bluetooth.read();
Serial.write(lenhbat);

//============ Hien thi cho che do cai dat thoi gian ====================

(giobat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giobat);
bluetooth.print(" : ");
(phutbat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phutbat);
bluetooth.print(" : ");
(giaybat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaybat);
bluetooth.println();
}
else lenhbat=0;
switch (lenhbat){
case 'a':
analogWrite(led1,t);
break;
case 'b':
analogWrite(led1,LOW);

break;
case '1':
giobat ++;
delay(600);
if( giobat ==24) {
giobat = 0;}
//=====================================
break;
case '2':

giobat -=1;  
delay(600);

if ( giobat < 0) {
giobat = 24;}

//===============================
break;
case'3':
phutbat ++;
delay(600);
if( phutbat > 59) {
phutbat = 0;

}
//==================================
break;
case'4':
phutbat -=1;
delay(600);
if ( phutbat < 0) {
phutbat = 59;

}

//=====================================
break;
case'5':
giaybat ++;
delay(600);
if( giaybat > 59) {
giaybat = 0;
}

//=========================================
break;
case'6':
giaybat -=1;
delay(600);
if ( giaybat < 0) {
giaybat = 59;
}
break;
case'8':{ // lenh reset
delay(500);
giobat = 0;
phutbat = 0;
giaybat = 0;

}
break;
default:
break;

}}
//===============hẹn giờ tắt đèn==========

void HENGIO_TATDEN(){

if (bluetooth.available()>0 ) {
lenhtat=bluetooth.read();
Serial.write(lenhtat);

//============ Hien thi cho che do cai dat thoi gian ====================

(giotat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giotat);
bluetooth.print(" : ");
(phuttat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phuttat);
bluetooth.print(" : ");
(giaytat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaytat);
bluetooth.println();
}
else lenhtat=0;
switch (lenhtat){
case 'x':
giotat ++;
delay(600);
if( giotat ==24) {
giotat = 0;}
//=====================================
break;
case 'y':

giotat -=1;  
delay(600);

if ( giotat < 0) {
giotat = 24;}

//===============================
break;
case'z':
phuttat ++;
delay(600);
if( phuttat > 59) {
phuttat = 0;

}
//==================================
break;
case'p':
phuttat -=1;
delay(600);
if ( phuttat < 0) {
phuttat = 59;

}

//=====================================
break;
case'q':
giaytat ++;
delay(600);
if( giaytat > 59) {
giaytat = 0;
}

//=========================================
break;
case'r':
giaytat -=1;
delay(600);
if ( giaytat < 0) {
giaytat = 59;
}

break;
default:
break;

}}

//==============================================

1 Like

As you have already confirmed the length of list >= 2 and then you are asking for element at index 2, it should not answer with this error.

Maybe it is because of smother code block.

1 Like

Hello thiện sửu TV

There are issues everywhere, App Project and Sketch, but at least (even though we can't see all of your code) you are connected and data is received. Firstly, how you receive data and how it is handled is wrong, and there should be no warnings in the App Blocks:

In your Block, you are separating the data with ',' but in your Sketch you are separating the data with ':'. I recommend using the bar char '|' , least likely to be within the data itself.

In an ideal world your Sketch would be organised better, it's very difficult to follow. The data being sent to the App should be at the end of the loop, together. The Sketch, via bluetooth.print, is sending way more than the two values the App is waiting to receive - so that must be sorted out. Also, remove the Delay() and instead use elapsed time - Delay makes everything stop, which can result in inaccurate sensor readings.

2 Likes

i send project to you....
can you help me check
thanks you!!!

We (The Power Users) are not here to do that. If you have an error, tell us exactly what the error is and we will help you fix it.

ok..thanks you!

i wuant setup timer on and timer off ,but don't know how to create block to system run!
can you support to me!
thanks you!

Did you sort that out?

1 Like

I still do not understand
Can you be more specific?
thanks you!

  1. Your App code is setup to receive two values, but your Arduino Sketch is setup to send more than two values. They have to match in number and in order.

  2. The App and the Sketch must both use the same value delimiter - I recommend the bar char '|'

  3. In Void Setup(), the Sketch initialises Serial @ 9600 baud rate, but it does not initialise the SoftwareSerial instance named 'bluetooth' -that is used to send the data to the App. e.g. bluetooth.begin(9600);

  4. When data is sent to the App, it should only be the values - no labels. A Bluetooth data packet only carries 20 bytes in total. Send the data at the end of the void loop() like this:

bluetooth.print(value1);
bluetooth.print("|"); //value delimiter
bluetooth.print(value2);
bluetooth.println(); //indicates end of data

2 Likes

I am using the character ":"

That's OK - just make sure it is used consistently.

Very nice GUI :sunglasses: