App fail to run with the arduino code

When I use the app in my andriod phone, after I press the button temperature, the app stop response and quit automatically.

Here is my arduino code
void loop() {
//Serial.print("1");

if(Serial.available())
{
char t;
delay(30);
t = Serial.read();
Serial.println(t);

if (t == 'W')
{
Serial.print("a");
}
}
and when I delete the if statement part, it function normally.
Please help.

Hi Cindy

Well, your Sketch does not make a lot of sense. I cannot see how it ties-in with your App as we cannot see all of your Blocks.

  1. Post your Sketch here (change .ino to .txt)
  2. In App Inventor, Blocks, right mouse click and select "download blocks as image".
  3. What is the exact name and model of your arduino?
  4. What is the exact name and model of the Bluetooth module being used with the Arduino?
  5. What is the make and model of your device (phone)?

You are using a New Line Delimiter, but your AI2 App lacks the Clock Timer to repeatedly check for incoming messages and post them.

Standard Delimiter advice:

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.

first_Screen_checkpoint1_Screen_checkpoint3.aia (94.6 KB)

it don't let me upload the arduino ino file so i put the code here

int in = A0;

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
 
}

void loop() {
  
  if(Serial.available()) 
  {
  char t;
  
  t = Serial.read();
  delay(1000);
  Serial.println(t); 

  
  //if (t == 'T')
  {
  //byte val = map(analogRead(in),0,1024.0,0,255); 
  //Serial.write(val);
  }
  
  }
  
  delay(500);

}

I have another problem is that if i send the text "T" from app inventor to arduino
When i print that text "T" from out by serial print, the serial monitor gives "W" instead of "T".
How can I fix this??
thank you for helping

Hello Cindy

You are not helping us to help you! Questions are asked for good reason - the devil is in the technical detail. Without knowing precisely what you want your App to do and the exact set-up, we can only guess.

I attach an improved Sketch, but I don't expect it to work without modification:

  1. Your temperature sensor make, model (type) are unknown
  2. The Sketch must be singing the same song as the App (ABG's notes).

TemperatureSensor.txt (1.3 KB)

#include<SoftwareSerial.h> //If you are using an Uno, it only has 1 port

char dataIn;
unsigned long lgUpdateTime;
int sensorValue;

void setup()
{
         pinMode(A0, OUTPUT);
          Serial.begin(9600); //Receive data from App
         Serial1.begin(9600); //Write to Serial Monitor
         lgUpdateTime = millis();
}

void loop()
{
         //Excute loop every 1000 milliseconds, faster than the App sends to avoid buffer over-run
         
         if ((millis() - lgUpdateTime) > 1000)
         {
               lgUpdateTime = millis();
         
               if (Serial.available() > 0)
               {
                         dataIn = Serial.read();
                         Serial1.println(dataIn);
               }
         
               switch (dataIn)
               {
                         case 'T':
                                   sensorValue = analogRead(A0);
                                   Serial1.println('T');
                                   Serial1.println(sensorValue);
                                   break;
                         
                         case 'W':
                                   Serial1.println('W');
                                   break;
               }
         }
}

1 Like

Dear Chris,
if Cindy is using an UNO board as far as I know the PC monitor works only on the hardware serial, therefore on "Serial". Then, before being allowed to use the secondary line (i.e. Serial1) one shall instantiate the library as follows:
SoftwareSerial Serial1 = SoftwareSerial(10, 11); // Rx on pin 10 and Tx on pin 11 toward the (supposed :thinking:) BT shield.

Consequently the initialization should be something like here below:

     Serial1.begin(9600); //Receive data from App
     Serial.begin(9600); //Write to Serial Monitor

Apart of that @Cindy_tong in effect the information you have given are too few.
Have a great 2021 to all.
Ugo.

Dear Chris,
you know, I'm Italian, and by reading your "English speech" I'm learning a lot.
Really much appreciated !!!!
:hugs: :hugs: :hugs:
Ciao, ugo.

:joy: Odd way to learn English

1 Like

"...odd way..." I learned once more

:rofl: :rofl: :rofl:

Fingers crossed 2021 will be the year that we leave Covid behind.

Sorry i missed some questions, my whole sketch are on the comment before (i don't know why i cannot upload .ino file
i uploaded my app inventor blocks as aia file at the above comment
my arduino board is MEGA2560
Bluetooth used is HC-06 model
my phone is android , Samsung S10+

Dear Cindy,
annexed you'll find two files:
the .aia contains a sample app that features two buttons whose value is sent via BT to an Arduino Mega board.
The .txt contains the respective receving code on the Arduino Mega. (As Chris said: .ino files are not allowed, if you want to upload them you shall convert to txt before).
I have developed and tested them for another guy of the forum (Ruben) so they work for sure.

Don't care if the core job of this couple of files is to switch on/off a relay: what you can easily do is to verify that everything is working, as far as the communication is concerned, then you can modify all that you want.
What you need to do before all is, anyway, to change in the app the physical address of the HC06 because the one that is written in my code is that of my HC06.
Also please be aware to put a resistor partition between the Tx pin of the Mega and the Rx pin of the HC06 in order to reduce the voltage to 3.3 V approx.
The third file contains some useful hints on BT shields.
Enjoy !!!
Ugo.

HC06 Connection_3_3VDC.pdf (714.6 KB)

Ruben.aia (65.1 KB)

Ruben.txt (580 Bytes) .

2 Likes

Yep !
image

:smirk:

2 Likes

thank you very much

Thank you everyone for helping me
It turns out to be the problem of the Bluetooth HC06, everything works normally after I buy a new one are replace it.

Ah - I have fried a few components in my time :robot: