Receive 4byte number from smartphone

I’m trying to use my arduinio as an alarm.
And I used MIT app inventor to make smartphone application.
When I make application, I used the block ‘call BluetoothClient. Send4ByteNumber’ to send large number from my smartphone to arduino by bluetooth.
But arduino can’t receive that number. I tried to find why this happened.

From, MIT App Inventor Forum

I found that the number is being sent in bytes, which means that I receive the value one byte at a time.
But I don’t know how do I modify arduino code to receive that number.

Here’s my arduino code

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int piezo=7;

int minute=0;

long sound=1137;//(cycle of scale La)/2

void setup()

{

Serial.begin(9600);

pinMode(piezo, OUTPUT);

lcd.begin(16,2);

}

void alarm(long val)

{

long val2=val*2;//val2=cycle of scale La

for (long i=0; i<=30000; i=i+val2)

{

digitalWrite(piezo, 1);

delayMicroseconds(val);

digitalWrite(piezo, 0);

delayMicroseconds(val);

}

}

void loop()

{

if(Serial.available()>0)

{

for(minute; minute==0; )

{minute=Serial.read();}//receive data from smartphone

if(minute>=100)

{

minute=minute/100;

for (minute; minute>0; minute–)

{

lcd.clear();

lcd.print(minute);

delay(60000);//

}

if(minute==0)

{

lcd.clear();

lcd.print("!!!");

alarm(sound);

}

}

}

}

/quote]

  for(minute; minute==0; )

This is crap. The first of three clauses in a for loop is an initialization statement. What are you initializing there? The third clause is the increment clause. Where is your increment clause? If you really have no initialization section and no increment section, you really have a while statement. Use a while statement. Don't bastardize a for statement.

Where are you checking that there are 4 bytes to read? Where are you reading those 4 bytes? Where are you trying to union those 4 bytes into a long? What is that long supposed to represent?

I am not sure that this will be a workable solution for you but I have used the same method, just not with as large of a number.

The strategy is to send the number which you would like to read followed by a characters that is not a digit. According to the documentation this "should" work.

"Serial.parseInt() returns the first valid (long) integer number from the serial buffer. Characters that are not integers (or the minus sign) are skipped. Serial.parseInt() is terminated by the first character that is not a digit."

while (Serial.available() > 0) {

long time = Serial.parseInt(); }

For this you would need to send a non-digit character after your number in order to terminate the parse, anything except "-" should work, I used a coma when doing something similar.

Your code will look better if you use the code tags rather than the quote tags. Also, do you really need a full minute delay in the code?

to Pauls

yeah, I find that is crap. Thanks. Actually my code had another problem. But I fixed it. In that process, I missed to delete that statement. In fact, I'm new to here not only in arduino, but also in C language. In the statement '{minute=Serial.read();}//receive data from smartphone', I want to modify that statement to be able to receive 4byte number. But I can't know how to do.

In MIT App inventor, when I use Block 'Send4bytesNumber', this block send 4 byte number in one byte at a time. For example, when I want to send 1000 from my smartphone, the number is pakaged into 4 bytes, 00000000 00000000 00000011 11101000 and I try to receive them all then to union those 4 bytes into a long. I tried using array, etc. But my arduino received only 11101000.

“int minute=0;”

int is only 2 bytes, you need to use a long.

long minute = 0;

minute = serial.parseInt();

This demonstrates one way to accept a byte stream and convert it to a long.

union myUnion {
  byte in[4];
  long out;
} myConvert;

void setup() {
  byte input[] = {B0, B0, B00000011, B11101000};  // The input data as you get it
  int howBig = sizeof(input);
  Serial.begin(9600);

  for (int i = 0; i < howBig; i++) {
    myConvert.in[i] = input[howBig - i - 1];      // Reverse the order of bytes
  }
  
  Serial.print("\nValue = ");
  Serial.println(myConvert.out);                  // Extract the long
}

void loop() {

}

Did someone else ask a nearly identical question today? I'm pretty sure I replied to a question about AppInventor and Send4ByteNumber

...R