Hi everyone,
For a school project me and a classmate are making some kind of gameboy. The main thing to know is that there will be one hub and two controllers. Each controller and the hub will have one nRF24L01 module. I need to program 3 games. The first one is a game where a random timer will be generated and when the timer is over the first one to click wins.
Here is where the weird issue started, let's say the first time the hub reads every controller for a signal. The next round it does not read anything anymore. The round after it does and so on. So 1-0-1-0-1-0......
Me and my teacher have looked over the code but since he doesn't know anything about the RF24 library he can't really help me.
I and my classmate have been debugging for 2 weeks and I found that the problem lies with the hub. When the Hub does read the controller and I reset the hub and try again it reads it again. I have a feeling that the module has some variable stored and that it loops.
Example:
tijdTimer = 10000; //Wacht 10 seconden. Als er niets is gedrukt dan gwn terug.
huidigeTijd = millis(); //tijd hoelang het programma al draait.
while (millis() - huidigeTijd < tijdTimer) //doe voor het aantal seconden alles wat in de while staat.
{
//Serial.write("Tijd 2");
//animatie dat timer over is
if(radio.available())
{
Serial.println("Radio is avaiable");
char in[] = {0};
radio.read(&in, sizeof(in)); //alles dat wordt ingelezen wordt opgeslagen in de char in.
lcd.setCursor(0,0);
lcd.print(" The winner is: ");
lcd.setCursor(0,2);
lcd.print(" Player:");
lcd.setCursor(11,2);
lcd.print(in[0]);
char win[] = "T"; //maak een array met karakters genaamd text. Stop hierin "T" van tone.
if (in[0] == '1') //Winnaar con1?
{
radio.stopListening(); //door te stoppen met luisteren wordt het een zender.
radio.openWritingPipe(con1);
radio.write(&win, sizeof(win)); //verstuur de data in de text.
radio.startListening();
}
else if (in[0] == '2')//Winnaar con2?
{
radio.stopListening(); //door te stoppen met luisteren wordt het een zender.
radio.openWritingPipe(con2);
radio.write(&win, sizeof(win)); //verstuur de data in de text.
radio.startListening();
}
delay(4000);
tijdTimer = 0; //Stop de timer
}
}
This is the line of code in the Hub that should make sure there is something available. It however does read something one time and not the other.
On the controller side there are these lines of code:
bool end = false;
while (end == false) //Geen signaal binnen? blijf wachten
{
//Serial.println("Wacht op signaal");
if (digitalRead(knop) == LOW && isGedrukt == LOW) //Als de knop wordt geklikt.
{
char uit[] = "1"; //maak een array met karakters genaamd in. Stop hierin "1".
Serial.println("Knop gedrukt");
radio.stopListening(); //door te stoppen met luisteren wordt het een zender
radio.write(&uit, sizeof(uit)); //data die in 'in' staat wordt verstuurd.
isGedrukt = HIGH;
radio.startListening();
}
We have tested these lines and when the button is pressed on the controller side it does go in this If statement. The problem is not on the controller side (I think
). And because resetting the Hub every other time fixes the bug I again believe the controller is not the problem.
I hope someone knows what the bug is and could help me. I have attached the code of the hub (Menu_transceiver) and the code of one of the controllers.
I know some people will hate on the:
if()
{
}
but this is how my teacher has learned me how to code and he wants us to use it like this.
If there are any questions or you need something else like schematics or something I can provide those.
The GitHub where I store the code is:
https://github.com/ItsNotDaan/DaRan-Console
Edit 1[
I was looking over the post and noticed something that I didn't mention.
The problem lies with the Radio.available(). One time it does find something and the other time not.
]
5.Controller1.ino (4.6 KB) 4._Menu_transciever.ino (17.1 KB)


