/********************************
chatgpt prompt i used and rectified is
using ili9341 tft connected to arduino draw various shapes
one by one like traingle squaew rectangle,
circle and fill them with random color
rxplaination This Arduino sketch uses the Adafruit_GFX and Adafruit_ILI9341 libraries
to draw random shapes (triangle, square, rectangle, and circle) on an ILI9341 TFT display.
The sketch randomly generates the position, size, and color of each shape and displays
them one by one with a delay of one second between each drawing.
Let's summarize the code:
Libraries: The sketch includes the required libraries: Adafruit_GFX, Adafruit_ILI9341,
and SPI for communication.
Pin Definitions: The TFT_CS (chip select) and TFT_DC (data/command) pins are defined,
which are connected to the corresponding pins on the TFT display.
Display Initialization: In the setup() function, the TFT display is initialized using the
tft.begin() function. The screen is filled with black initially using tft.fillScreen(ILI9341_BLACK).
Random Seed: The random number generator is seeded with an analog reading from pin A0 using
randomSeed(analogRead(A0)). This helps to generate different random sequences on each run.
Loop Function: In the loop() function, the sketch repeatedly draws different random shapes.
drawRandomShape Function: This function is responsible for drawing the random shapes
based on the shapeType parameter passed to it.
The function first generates random x and y coordinates on the TFT display.
It then generates a random size for the shape in the range of 20 to 100 pixels.
A random 16-bit color is generated for each shape.
The TFT display is filled with the randomly generated color to create the shape background.
Depending on the shapeType value, the function draws a triangle, square, rectangle, or
circle using the specified color on top of the background.
Note: The code assumes the display dimensions are compatible with the tft.width()
and tft.height() functions. It also assumes that the display uses the ILI9341 controller and can accept the commands and functions used in the code. If you are using a different display, you may need to adjust the code accordingly.
Upload the sketch to your Arduino board, and you should see the shapes
being drawn one after the other on the TFT display, each filled with a
random color. The code will loop and redraw the shapes every second.
You can modify the size range and other parameters to customize the
appearance and behavior of the shapes
as per your requirements.
by arvind patil 27 july23
*********************************************/
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#define TFT_CS 10
#define TFT_DC 9
// Create an instance of the TFT display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
tft.begin();
// Seed the random number generator with an analog pin reading
randomSeed(analogRead(A0));
}
void loop() {
// Clear the screen
tft.fillScreen(ILI9341_BLACK);
// Write "by Arvind Patil" in yellow color
writeByArvindPatil();
// Draw a triangle
drawRandomShape(1);
// Draw a square
drawRandomShape(2);
// Draw a rectangle
drawRandomShape(3);
// Draw a circle
drawRandomShape(4);
delay(1000); // Wait for a second before drawing the next shape
}
void writeByArvindPatil() {
tft.setTextColor(ILI9341_YELLOW); // Set the text color to yellow
tft.setTextSize(3); // Set the text size
tft.setCursor(10, tft.height() - 30); // Set the cursor position at the bottom-left
tft.print("by Arvind "); // Print the text
}
void drawRandomShape(int shapeType) {
int x = random(tft.width());
int y = random(tft.height());
int size = random(20, 100); // Adjust size range as needed
int color = random(0xFFFF); // Generate a random 16-bit color
tft.fillRect(x, y, size, size, color); // Fill the shape with the random color
switch (shapeType) {
case 1:
// Draw a triangle
tft.fillTriangle(x, y, x + size, y, x + (size / 2), y - size, ILI9341_BLACK);
break;
case 2:
// Draw a square
tft.drawRect(x, y, size, size, ILI9341_BLACK);
break;
case 3:
// Draw a rectangle
tft.drawRect(x, y, size * 2, size, ILI9341_BLACK);
break;
case 4:
// Draw a circle
tft.drawCircle(x + (size / 2), y + (size / 2), size / 2, ILI9341_BLACK);
break;
}
}