I have a code that uses the adafruit libraries with the giga r1 and display shield. The code compiles and uploads fine but the screen is blank. I have tried using the examples and most don't do anything to the screen. I can get some examples to work but once adafruit libraries are included, nothing works. Has anyone experienced this before and if so, how did you fix it? The code I have looks perfect on paper but it just won't display properly.
#include <Adafruit_GFX.h> // Include Adafruit GFX library
#include <Adafruit_ILI9341.h> // Include the specific display driver library
// Initialize the display (adjust the pins as needed)
Adafruit_ILI9341 Display = Adafruit_ILI9341(10, 9, 8); // CS, DC, RESET pins
#define BLACK ILI9341_BLACK
#define WHITE ILI9341_WHITE
#define MAX_RECORDS 10
unsigned long lastReadingTime = 0; // Last reading timestamp
unsigned long timerStart = 0; // Timer start timestamp
unsigned long recordedTimes[MAX_RECORDS] = {0}; // Array to store recorded times
int recordIndex = 0; // Current index for storing times
float averageTime = 0; // Average time calculation
int readingCount = 0; // Number of readings taken
void setup() {
Serial.begin(9600); // Start serial communication
Display.begin(); // Initialize the display
Display.setRotation(3); // Set the rotation if needed
Display.fillScreen(BLACK); // Clear the screen at the beginning
Display.setTextColor(WHITE);
Display.setTextSize(2); // Set the text size
// Draw section headers
Display.setCursor(10, 10);
Display.print("Current Time:");
Display.setCursor(130, 10);
Display.print("Previous Times:");
Display.setCursor(10, 240);
Display.print("Average Time:");
}
void displayCurrentReading(unsigned long duration) {
Display.fillRect(0, 30, 240, 80, BLACK); // Clear previous display area
Display.setCursor(10, 30);
Display.print("Duration: ");
Display.print(duration);
Display.println(" ms");
}
void displayPreviousTimes() {
Display.fillRect(120, 30, 120, 210, BLACK); // Clear previous times display
Display.setCursor(130, 30);
for (int i = 0; i < MAX_RECORDS; i++) {
if (recordedTimes[i] > 0) {
Display.setCursor(130, 50 + i * 20); // Adjust y position for each time
Display.print(recordedTimes[i]);
Display.println(" ms");
}
}
}
void displayAverageTime() {
Display.fillRect(0, 240, 240, 40, BLACK); // Clear previous average display
Display.setCursor(10, 240);
Display.print("Average: ");
Display.print(averageTime);
Display.println(" ms");
}
void loop() {
int reading = analogRead(A5); // Read the value from pin A5
if (reading > 0) { // Check if there's a valid reading
unsigned long currentTime = millis(); // Get current time in milliseconds
// If this is the first reading or if a new reading is detected
if (timerStart == 0) {
timerStart = currentTime; // Start the timer
} else {
// Calculate the duration since the last reading
unsigned long duration = currentTime - timerStart;
// Store the recorded time in the array
recordedTimes[recordIndex] = duration;
recordIndex = (recordIndex + 1) % MAX_RECORDS; // Increment index cyclically
readingCount++;
// Calculate the average time
unsigned long totalTime = 0;
for (int i = 0; i < MAX_RECORDS; i++) {
totalTime += recordedTimes[i];
}
averageTime = (readingCount > 0) ? (float)totalTime / readingCount : 0;
// Display the new reading duration, previous times, and average time
displayCurrentReading(duration);
displayPreviousTimes();
displayAverageTime();
// Reset the timer for the next reading
timerStart = currentTime;
}
delay(1000); // Update every second
}
}