Hello
I have a working code that was written for U8G2, but now due to a display replacement (TFT) the U8G2 can no longer be used! Therefore, I have to rewrite everything in Adafruit!
What I want to do is to display vertical lines on the right side of the display and a random number value gives the length of the line! If the line is drawn, move left by one pixel and draw the new line! If you reach the left side, move on and do not start drawing the lines again! U8g2 infinite side move - #13 by Norad77
The code works well so far, the value of the line is not smaller than the next value! If the length of the previous line is larger, it displays it, but if it is smaller, it displays the value of the previous line!
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
#define TFT_DC 48
#define TFT_CS 53
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
unsigned long startTime = millis();
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 200
#define START_Y 20
int buff[SCREEN_WIDTH];
unsigned long lastUpdate = 0;
const int updateInterval = 500; // Fél másodpercenként frissít
int szam;
void rand1() {
szam =random(8, SCREEN_HEIGHT);
Serial.print("Random:");
Serial.println(szam);
Serial.println();
}
void setup() {
tft.begin();
Serial.begin(9600);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0, START_Y, SCREEN_WIDTH, SCREEN_HEIGHT, ILI9341_BLUE); // initial water background
for (int i = 0; i < SCREEN_WIDTH; i++) {
buff[i] = 0;
}
}
void strip() {
if (millis() - lastUpdate < updateInterval) {
return;
}
lastUpdate = millis();
tft.drawFastVLine(0, START_Y, buff[0], ILI9341_BLUE);
// Shift existing values left
for (int i = 0; i < SCREEN_WIDTH - 1; i++) {
buff[i] = buff[i + 1];
}
// Generate new random height
rand1();
buff[SCREEN_WIDTH - 1] = szam;
for (int i = 0; i < SCREEN_WIDTH; i++) {
tft.drawFastVLine(i, START_Y, buff[i], ILI9341_WHITE);
}
}
void loop() {
strip();
}
The strip() function generates random values for the line heights and then shifts all previous lines to the left. I think you need to edit your code to solve this problem.
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
#define TFT_DC 48
#define TFT_CS 53
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
unsigned long startTime = millis();
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 200
#define START_Y 20
int buff[SCREEN_WIDTH];
unsigned long lastUpdate = 0;
const int updateInterval = 500; // Update every half a second
int szam;
void rand1() {
// Generate a random number for the line height
szam = random(8, SCREEN_HEIGHT);
Serial.print("Random:");
Serial.println(szam);
Serial.println();
}
void setup() {
tft.begin();
Serial.begin(9600);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0, START_Y, SCREEN_WIDTH, SCREEN_HEIGHT, ILI9341_BLUE); // Initial water background
// Initialize the buffer with zeros
for (int i = 0; i < SCREEN_WIDTH; i++) {
buff[i] = 0;
}
}
void strip() {
// Only update the screen after the interval
if (millis() - lastUpdate < updateInterval) {
return;
}
lastUpdate = millis();
// Shift the existing values in the buffer
for (int i = 0; i < SCREEN_WIDTH - 1; i++) {
buff[i] = buff[i + 1];
}
// Generate a new random value for the last position
rand1();
buff[SCREEN_WIDTH - 1] = szam;
// Clear the entire screen (optional, but ensures a clean state)
tft.fillRect(0, START_Y, SCREEN_WIDTH, SCREEN_HEIGHT, ILI9341_BLUE);
// Draw each line based on the buffer values
for (int i = 0; i < SCREEN_WIDTH; i++) {
tft.drawFastVLine(i, START_Y, buff[i], ILI9341_WHITE);
}
}
void loop() {
strip();
}
If you have a WIMOS Lolin D2, you can cross check your ILI9341 with that platform following this tutorial.