Hallo lezer,
Ik ben nog steeds een redelijke beginner o.b.v. het programmeren. Voor school ben ik bezig met een project waarbij ik een app heb gemaakt (eigenlijk moet ik ook nog de software serial library is onderzoeken, maar tot nu toe werkt dit helemaal). Nu loop ik tegen een paar dingen aan:
Met de Serial.print() kan ik de data via de HC-05 naar mn app sturen. Hoe kan ik iets printen zonder dat hij dat stuurt, dit wil ik voor het demofilmpje om bijvoorbeeld te printen dat je geconnect bent of dat de training is gestart.
Ik heb nog totaal geen ervaring met het gebruik van Strings. Ik heb allereerst de String Name = "" aangemaakt, vervolgens de app zo geprogrammeerd dat hij een naam moet sturen en dan als Name != "" is je verder kan met de loop, dit heb ik een beetje getest en de eerste keer deed hij dat prima volgens mij. Daarna heb ik het zo geprogrammeerd dat als hij niet meer connected is dat wanneer die pin weer HIGH wordt dus connectie is hij Name weer set naar "". Voor een of andere reden lijkt die dit eigenlijk niet te herkennen en kan ik gwn mn test doen en print die de data wat eigenlijk dus niet de bedoeling is aangezien hij niet in die loop hoort te kunnen komen.
Ik heb mijn code hierin geplakt, ik hoop dat deze duidelijk is en ook mijn denk proces duidelijk beschreven is. Ik zou graag een steuntje in de rug willen om de eerste wens maar vooral mijn tweede probleem op te lossen, aangezien ik niet kan vinden waarom mijn probleem niet werkt.
Groetjes,
Bram
// Connect the HC-05 TX to Arduino pin 0 RX.
// Connect the HC-05 RX to Arduino pin 1 TX through a voltage divider.
const byte BTpin = 3;// connect the STATE pin of the HC-05 to Arduino pin D3
const byte PotPin = A0;// connect the potentiometer to Arduino pin A0
const byte PunchButton = 2;// connect the button to Arduino pin D2
//// Add variables
long Val = 0;// value of potentiometer
unsigned long Timer = 0;// varbiable to work with the timer
float SpeedOfPunch = 0;
float PowerOfPunch = 0;
int TimesPunched = 0;
float AVGPunchingSpeed = 0;
float AVGPunchingPower = 0;
String Name = "";// string which contains the name of the player
//// Add Errors
boolean SDerror = false;
boolean Systemerror = false;
//// Make the punch button work
boolean ButtonState = LOW;
boolean DebouncePunchButton(boolean State)
{
boolean NewState = digitalRead(PunchButton);
if (State != NewState)
{
delay(10);
NewState = digitalRead(PunchButton);
}
return NewState;
}
void setup()
{
pinMode(BTpin, INPUT);
pinMode(PunchButton, INPUT);
Serial.begin(9600);
while (digitalRead(BTpin) == LOW) // can't move on unless you connect
{
if (digitalRead(BTpin) == HIGH)
{
}
}
delay(500);
}
void loop()
{
if (digitalRead(BTpin) == HIGH)
{
if (Serial.available() != 0)
{
Name = Serial.readString(); // read the name send
}
if (Name != "" && SDerror == false && Systemerror == false)
{
if (TimesPunched == 0)
{
if(DebouncePunchButton(ButtonState) == HIGH && ButtonState == LOW) // the first punch will start the timer
{
TimesPunched++; // add 1 punch
Val = analogRead(PotPin);
SpeedOfPunch = map(Val, 0, 1023, 5, 10) + SpeedOfPunch; // boxers generaly punch between 5 and 10 m/s
PowerOfPunch = map(Val, 0, 1023, 440, 1100) + PowerOfPunch; // olympic boxers punch between 440 an 1100 pounds
ButtonState = HIGH;
Timer = millis();
}
else if (DebouncePunchButton(ButtonState) == LOW && ButtonState == HIGH)
{
ButtonState = LOW;
}
}
else if (TimesPunched >= 1)
{
if(DebouncePunchButton(ButtonState) == HIGH && ButtonState == LOW) // when the button is pushed
{
TimesPunched++; // add 1 punch
Val = analogRead(PotPin);
SpeedOfPunch = map(Val, 0, 1023, 5, 10) + SpeedOfPunch; // boxers generaly punch between 5 and 10 m/s
PowerOfPunch = map(Val, 0, 1023, 440, 1100) + PowerOfPunch; // olympic boxers punch between 440 an 1100 pounds
ButtonState = HIGH;
}
else if (DebouncePunchButton(ButtonState) == LOW && ButtonState == HIGH)
{
ButtonState = LOW;
}
else if ((millis() - Timer) >= 10000) // check if 10 seconds have past to simulate the training is done and print the information
{
AVGPunchingSpeed = SpeedOfPunch/TimesPunched; // calc the average speed
AVGPunchingPower = PowerOfPunch/TimesPunched; // calc the average power
Serial.print(TimesPunched);
Serial.print("|");
Serial.print(AVGPunchingSpeed, 2);
Serial.print(" m/s");
Serial.print("|");
Serial.print(AVGPunchingPower, 2);
Serial.print(" N");
Serial.println();
delay(10000);
Serial.print("Break done");
Serial.println();
TimesPunched = 0;
SpeedOfPunch = 0;
PowerOfPunch = 0;
}
}
}
if (SDerror == true) // print the error once for the app to recieve
{
Serial.print("SD error");
Serial.println();
}
if (Systemerror == true) // print the error once for the app to recieve
{
Serial.print("System error");
Serial.println();
}
}
else if (digitalRead(BTpin) == LOW)
{
if (digitalRead(BTpin) == HIGH) // reset the variables to their default
{
TimesPunched = 0;
SpeedOfPunch = 0;
PowerOfPunch = 0;
Name = "";
}
}
}