Do my Indexes match my Code? (Bluetooth classic)

Hi everyone,
I'm still a beginner on MIT app inventor, I'm contacting you to ask for your help on my BLOCS on MIT app, here is my code I just want to know if my indexes match my code. The application must count points, when the button is pressed we win a point and a turn, if we miss we press service raté " and we only win a turn. thank you in advance

#include <SoftwareSerial.h>

#define rxPin 11 // Broche 11 en tant que RX, à raccorder sur TX du HC-05
#define txPin 10 // Broche 10 en tant que TX, à raccorder sur RX du HC-05

SoftwareSerial mySerial(rxPin, txPin);

int joueur1 = 0;
int noTour = 1;
int tourMax = 100;
bool endGame = false;

// TARGET1
int ledTarget1 = 2;
int switchTarget1 = A0;

long previousMillisTarget1 = 0;
long intervalTarget1 = 1000;
bool isTargetHit1 = false;
// FIN TARGET1

void setup() {
  Serial.begin(9600);
  pinMode(ledTarget1, OUTPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.println(String(joueur1) + "|" + String(noTour) + "|" + String(endGame));
}

void loop() {
  String customKey = "";

  if (Serial.available() > 0) {
    customKey = Serial.readString();
  }

  if (noTour == tourMax) {
    noTour = 1;
    delay(1500);
    Serial.println(String(joueur1) + "|" + String(noTour) + "|" + String(endGame));
  }

  if (customKey == "service raté") {
    noTour++;
    Serial.println(String(joueur1) + "|" + String(noTour) + "|" + String(endGame));
  }

  if (customKey == "start") {
    endGame = false;
    joueur1 = 0;
    noTour = 1;
    Serial.println(String(joueur1) + "|" + String(noTour)  + "|" + String(endGame));
  }

  checkTarget1();
}

void checkTarget1() {
  int switchValue = analogRead(switchTarget1);

  if (switchValue > 0 && !isTargetHit1) {
    digitalWrite(ledTarget1, HIGH);
    isTargetHit1 = true;
    joueur1++;
    noTour++;
    Serial.println(String(joueur1) + "|" + String(noTour) + "|" + String(endGame));
  }

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillisTarget1 > intervalTarget1) {
    previousMillisTarget1 = currentMillis;
    isTargetHit1 = false;
    digitalWrite(ledTarget1, LOW);
  }
} 

and here is the app inventory part :


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.

Could you code in English, or would you rather wait for someone who speaks your language?

The last conversation in French on this board was years ago.

P.S. I do not see any correspondence between data types you receive and the data types you send.

#include <SoftwareSerial.h>

#define rxPin 11 // Pin 11 as RX, connected to TX of HC-05
#define txPin 10 // Pin 10 as TX, connected to RX of HC-05

SoftwareSerial mySerial(rxPin, txPin);

int player1 = 0;
int turnNumber = 1;
int maxTurns = 100;
bool endGame = false;

// TARGET1
int ledTarget1 = 2;
int switchTarget1 = A0;

long previousMillisTarget1 = 0;
long intervalTarget1 = 1000;
bool isTargetHit1 = false;
// END TARGET1

void setup() {
Serial.begin(9600);
pinMode(ledTarget1, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.println(String(player1) + "|" + String(turnNumber) + "|" + String(endGame));
}

void loop() {
String customKey = "";

if (Serial.available() > 0) {
customKey = Serial.readString();
}

if (turnNumber == maxTurns) {
turnNumber = 1;
delay(1500);
Serial.println(String(player1) + "|" + String(turnNumber) + "|" + String(endGame));
}

if (customKey == "service failed") {
turnNumber++;
Serial.println(String(player1) + "|" + String(turnNumber) + "|" + String(endGame));
}

if (customKey == "start") {
endGame = false;
player1 = 0;
turnNumber = 1;
Serial.println(String(player1) + "|" + String(turnNumber) + "|" + String(endGame));
}

checkTarget1();
}

void checkTarget1() {
int switchValue = analogRead(switchTarget1);

if (switchValue > 0 && !isTargetHit1) {
digitalWrite(ledTarget1, HIGH);
isTargetHit1 = true;
player1++;
turnNumber++;
Serial.println(String(player1) + "|" + String(turnNumber) + "|" + String(endGame));
}

unsigned long currentMillis = millis();

if (currentMillis - previousMillisTarget1 > intervalTarget1) {
previousMillisTarget1 = currentMillis;
isTargetHit1 = false;
digitalWrite(ledTarget1, LOW);
}
}

Here is the code in English, and how could I fix this problem between the data

Please download and post each of those event block(s)/procedures here ...
P.S. These blocks can be dragged directly into your Blocks Editor workspace.

See Download Block Images for a demo.





blocks (6)

Like This ?

Do you have an option to turn your blocks into English too?

Export your .aia file and upload it here.
export_and_upload_aia

Your code that transmits:

Serial.println(String(player1) + "|" + String(turnNumber) + "|" + String(endGame));

I see three parts to each message.

The declarations of those variables:
image

That's two numbers and a true/false variable.

Here's the corrected way to receive and split data from println():

I removed your item selection part, which asked for nonexistent item numbers (4,5)
and did not even guess right for which of the 3 items was in the right place.

I suggest you sit down and have a conversation with whoever wrote the other part of this mismatched app pair.

2 Likes

OSI.export.aia (14.3 KB)

so the code matches now?

No.

It can be displayed, but it is up to you to match the subscripts.

You might actually have to even read your own code and the code of the other person.

And I have a question where "message_part_list" comes from ?

blocks (1)

But then the code is wrong at what point?

and do I need the <SoftwareSerial.h> library ?

Do you have reason to believe it would benefit you by removing it?

In fact, I use a shield base and therefore I have no specific port, moreover my BT is connected in grove on the shield base so I do not know...

Perhaps start from the question

Did a message arrive at my app?

What did it look like?

avrdude: ser_open(): can't set com-state for "\.\COM4"
Here is the error it gives me when I want to transmit the program to the arduino (I removed the library)