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);
}
}
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.
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.
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.
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)