Hi y'all
I'm writing a code for a laser tag game. I'm using a Arduino Mega 2560 and a Adafruit 2.8 TFT Touch Screen Display.
My Problem is that I'm unable to print to the display in the loop (function "displayScores" at the bottom). In setup I can. Why is that?
Below my code:
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
#include <AltSoftSerial.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
//AltSoftSerial altSerial; // Receive = 8, Transmit = 9
SoftwareSerial HC12(50, 51);
int initial_score = 0;
const int num_players = 4;
const int num_scores = 2;
int score_players[num_players][num_scores] = { // define an array for storing scores
{ initial_score, initial_score },
{ initial_score, initial_score },
{ initial_score, initial_score },
{ initial_score, initial_score }
};
byte incomingByte;
const unsigned int READ_BUFFER_SIZE = 11; //Platz für 10 Zeichen + Terminator
void setup() {
// Serial coms set up to help with debugging.
//altSerial.begin(9600);
Serial.begin(9600);
HC12.begin(9600);
tft.begin();
Serial.println("Startup...");
tft.setRotation(-1);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("PLAYER1");
tft.setTextColor(ILI9341_RED);
tft.println("TargetHits:");
tft.setCursor(200, 16);
tft.println("Hits:");
tft.setCursor(0, 60);
tft.setTextColor(ILI9341_GREEN);
tft.println("PLAYER2");
tft.setTextColor(ILI9341_RED);
tft.println("TargetHits:");
tft.setCursor(200, 76);
tft.println("Hits:");
tft.setCursor(0, 120);
tft.setTextColor(ILI9341_GREEN);
tft.println("PLAYER3");
tft.setTextColor(ILI9341_RED);
tft.println("TargetHits:");
tft.setCursor(200, 136);
tft.println("Hits:");
tft.setCursor(0, 180);
tft.setTextColor(ILI9341_GREEN);
tft.println("PLAYER4");
tft.setTextColor(ILI9341_RED);
tft.println("TargetHits:");
tft.setCursor(200, 196);
tft.println("Hits:");
}
// Main loop most of the code is in the sub routines
void loop() {
char* str = readLine(HC12);
if (str != nullptr) {
Serial.print("Eingelesen: ");
Serial.println(str);
compare(str);
displayScores();
}
}
// SUB ROUTINES
char* readLine(Stream& stream) {
static byte index;
static char buffer[READ_BUFFER_SIZE];
while (stream.available()) {
char c = stream.read();
//Serial.println("Stream:");
//Serial.println(c);
if (c == '\n') //wenn LF eingelesen
{
buffer[index] = '\0'; //String terminieren
index = 0;
return buffer; //melden dass String fertig eingelesen wurde
} else if (c >= 32 && index < READ_BUFFER_SIZE - 1) //solange noch Platz im Puffer ist
{
buffer[index++] = c; //Zeichen abspeichern und Index inkrementieren
}
}
return nullptr;
}
void compare(char* str) {
//Serial.println("compare running!");
if (strstr(str, "Shooter")) {
//Serial.println("Shooter received!");
char* ptr = strchr(str, ':');
int PlayerID = atoi(ptr + 1);
score_players[PlayerID - 1][0]++;
}
if (strstr(str, "Killed")) {
Serial.println("Killed received!");
char* ptr2 = strchr(str, ':');
int PlayerID2 = atoi(ptr2 + 1);
Serial.println("Killed Player");
Serial.println(PlayerID2);
score_players[PlayerID2 - 1][1]++;
}
}
void displayScores() {
tft.setTextSize(2);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setCursor(150, 16);
tft.print(score_players[0][0]);
tft.print(" ");
}