Receiveing values from arduino to app

Hi, a week ago my app had no problems receiveing values from arduino, which is these values = -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 (not in a list) and when i send the value it only show "N0" nothing else, should print F1,F2,F3,F4,F5 and R-1,R-2,R-3,R-4,R-5 aswell.
What i see there is no problem in arduino.

image

image

Maybe last week there was incoming data, and now there isnt. What is the initial value of receive_data? The problem is that is empty field. You should move the last if block into the previous, fter the set global receive_data block so the second comparison only run if there is data arrived.

ye, so the inital value is set at 0 in decimal form, not text form, and i've tried putting the last if block under global_receive, but then i don't get a "N0" ...

Ok. So checking your block i have a question. How is the arduino sending the data. If signed bytes then you should use that block instead of text.


If the arduino is sending text, then the comparison is wrong, because you try to compare a string to a decimal value.

Some remarks:
Your timer interval is too short. It should be shorter that the delay interval of sending messages in your Arduino. It should have a chance to send complete messages.
Then, it would be much easier if you would use a message delimiter. End every message from your Arduino with a println(). In the design section of App Inventor, set the delimiter for the BT component to 10. Then, set the number of bytes to receive to -1. App Inventor will now read always complete messages, instead of the number of bytes that are present at the clock timer event, which may be more than one or partial messages. See also here:
http://ai2.appinventor.mit.edu/reference/components/connectivity.html#BluetoothClient
For debugging purposes, it is handy to have a label on your phone screen that shows your incoming data. While you are debugging you can see what is going on. when finished, make it invisible or delete it.

thanks for answer, i think i did everything that u said but doesn't work, delimeter to 10, timer interval set to 2, but what do u mean by setting number of bytes to receive to -1? where and how do i do that?

For the BlueTooth Receive Block.

To help you properly Jimmy, we can't have upmpteen people guessing what you may or may not have done. We need to see your Arduino Sketch and all of your App blocks (of the screen concerned with Bluetooth). What is the Make/Model and Bluetooth version of your Bluetooth Module?

Note that for this type of project, "Timer Always Fires" should be False.

You made the timer interval even shorter???
Set it to 100 or similar. What is the delay in your .ino code? Can you post the part of your .ino code that sends the messages?

blocks (55)

But this only works if you end every Arduino message with println();

#include <SoftwareSerial.h>
SoftwareSerial BTserial(8, 9);  //RX from BT module = pin d9, TX from BT module =pin d8
int bt_value_counter = 0;      //Counter for BT data

bool safety_conditions_for_processing_bt_is_ok = false;
bool check_if_physical_throttle_is_neutral = false;


int bt_value = 0; // variable for storing BT data
bool bt_data_available = false;

#define BT_COMMAND_IS_GO_FORWARD_1  1 //Every step forward is a "1"
#define BT_COMMAND_IS_GO_REVERSE_1  255 //Every step back is a "-1" (255)
#define BT_COMMAND_IS_GO_TO_NEUTRAL 0 //Safety command for "resetting" throttle to 0

#define BT_VALUE_MAX  5  //Max value throttle can move from 0
#define BT_VALUE_MIN  -5 //Min value throttle can move from 0 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); // opens serial port, sets data rate to 115200 bps
  BTserial.begin(9600); //Serial for Bluetooth, 9600 standard baud rate
}

void loop() {
  // put your main code here, to run repeatedly:
  read_bt_data();


  check_throttle_position_based_on_bt();
}

void read_bt_data() {

  if(BTserial.available() > 0) {
    bt_value = BTserial.read();
    bt_data_available = true;
  }
}

void check_throttle_position_based_on_bt() {

  //check_bt_safety_conditions();
  
  if(safety_conditions_for_processing_bt_is_ok  && bt_data_available) {  //check_if_physical_throttle_is_neutral == true

    if(bt_value == BT_COMMAND_IS_GO_TO_NEUTRAL) {

      bt_value_counter = 0;
      
    } else if(bt_value == BT_COMMAND_IS_GO_REVERSE_1) {

      bt_value_counter--;
      
    } else if(bt_value == BT_COMMAND_IS_GO_FORWARD_1) {

      bt_value_counter++;
      
    } else {
      
      Serial.println("ERROR: got unexpected bt_value: ");
      Serial.print(bt_value);
    }

    // check saturation for safety

    if(bt_value_counter > BT_VALUE_MAX) {
      bt_value_counter = BT_VALUE_MAX;
    } else if(bt_value_counter < BT_VALUE_MIN) {
      bt_value_counter = BT_VALUE_MIN;
    }
    
    BTserial.println(bt_value_counter);
    Serial.println(bt_value_counter);
    
      
    bt_data_available = false;
  }
  
}

hc05 and arduino nano

Hi Jimmy

You can upload your Arduino Sketch by changing the extension from .ino to .txt

Re your Blocks:

  1. Setup the Clock timer and Bluetooth Delimiter in the Screen Initialize Block.
  2. It's always a good idea to include the Screen Error catcher Block too - downloadable snippet:

https://www.professorcad.co.uk/appinventorsnippets#GetErrorInfo

  1. The bytes available test must be > 0 (if zero, no bytes!)

  2. In the BT Receive Block, numberOfBytes = -1

  3. Is this App just for your own use? If so and it works fine, no problem but if others are to use it, their phones may or may not be as fast as yours (various reasons), in which case you need to allow a bit of time for all BT devices to be found before populating the ListPicker. You would also want to remind them to:
    a) Switch the target device on
    b) Switch Location on (phone)
    c) Switch Bluetooth on (phone)

i did solve it! thank u so much!

thanks for the help, no more needed, tysm!

That's very good news - care to share your solution to benefit others?
Edit: there are errors in your Sketch.......
Edit2: and in your Blocks......