#include <Servo.h>
#include <FastLED.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#define LED_PIN 6 // define LED pin
#define NUM_LEDS 16 // define number of LEDs
#define SERVO_PIN 5 // define servo pin
#define BUTTON_PIN 2 // define button pin
#define TFT_DC 8
#define TFT_CS 10
CRGB leds[NUM_LEDS]; // create LED array
Servo servo; // create servo object
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int buttonState = 0; // variable to store button state
int servoAngle = 0; // variable to store servo angle
int brightness = 0; // variable to store LED brightness
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // set button pin as input with internal pull-up resistor
servo.attach(SERVO_PIN); // attach servo to servo pin
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); // initialize LED ring
tft.begin(); // initialize tft
tft.setRotation(3); // set tft rotation
tft.fillScreen(0); // clear tft screen
}
void loop() {
buttonState = digitalRead(BUTTON_PIN); // read button state
if (buttonState == HIGH) { // if button is pressed
servoAngle++; // increase servo angle
if (servoAngle > 180) { // if servo angle is greater than 180
servoAngle = 0; // reset servo angle
}
brightness = map(servoAngle, 0, 180, 0, 255); // map servo angle to LED brightness
for (int i = 0; i < NUM_LEDS; i++) { // loop through all LEDs
leds[i] = CHSV(255, 255, brightness); // set LED color to magenta
}
leds[servoAngle/11] = CHSV(0, 255, brightness); // set LED at servo angle to red
leds[servoAngle/22] = CHSV(60, 255, brightness); // set LED at half servo angle to yellow
FastLED.show(); // update LED ring
tft.fillScreen(0); // clear tft screen
tft.setCursor(50, 50); // set cursor position
tft.print("❤"); // print heart symbol
delay(100); // delay for 100 milliseconds
} else { // if button is not pressed
servo.write(0); // set servo angle to 0
tft.fillScreen(0); // clear tft screen
tft.setCursor(30, 50); // set cursor position
tft.print("Happy Valentine's Day Sophia"); // print text
tft.setCursor(50, 100); // set cursor position
tft.print("Press me"); // print text
tft.drawTriangle(60, 120, 70, 130, 80, 120, 0xFFFF); // draw arrow pointing down
delay(100); // delay for 100 milliseconds
}
}
// Code sourced from:
// https://www.arduino.cc/en/Tutorial/ButtonStateChange
// https://github.com/FastLED/FastLED/blob/master/examples/ColorPalette/ColorPalette.ino
// https://learn.adafruit.com/adafruit-gfx-graphics-library/graphics-primitives
// https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts
/* Code für Arduino mit GC9A01-Display:
#include <Servo.h>
#include <FastLED.h>
#include <Adafruit_GFX.h>
#include <GC9A01.h>
#define BUTTON_PIN 2 //button pin
#define SERVO_PIN 3 //servo pin
#define LED_PIN 4 //LED ring pin
#define tft_PIN 5 //tft display pin
Servo servo; //create servo object
CRGB leds[16]; //create LED array
GC9A01 tft; //create tft display object
int angle = 0; //initial servo angle
int brightness = 0; //initial LED brightness
void setup() {
pinMode(BUTTON_PIN, INPUT); //set button pin as input
servo.attach(SERVO_PIN); //attach servo to pin
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, 16); //initialize LED ring
tft.begin(); //initialize tft display
tft.setRotation(1); //set display rotation
tft.fillScreen(0); //clear display
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) { //if button is pressed
angle = 0; //reset servo angle
brightness = 0; //reset LED brightness
while (angle <= 180) { //while servo angle is less than or equal to 180 degrees
servo.write(angle); //move servo to current angle
leds[0] = CHSV(255, 255, brightness); //set first LED to magenta
leds[1] = CHSV(0, 255, brightness); //set second LED to red
leds[2] = CHSV(60, 255, brightness); //set third LED to yellow
FastLED.show(); //update LED ring
tft.fillScreen(0); //clear display
tft.setCursor(50, 50); //set cursor position
tft.setTextSize(2); //set text size
tft.setTextColor(0xFFFF); //set text color to white
tft.print("❤"); //print heart symbol
delay(100); //delay for smooth movement
angle++; //increment servo angle
brightness += 10; //increment LED brightness
}
tft.fillScreen(0); //clear display
tft.setCursor(0, 50); //set cursor position
tft.setTextSize(2); //set text size
tft.setTextColor(0xFFFF); //set text color to white
tft.print("Happy Valentine's Day Sophia"); //print text
delay(1000); //delay for 1 second
tft.fillScreen(0); //clear display
tft.setCursor(0, 50); //set cursor position
tft.setTextSize(2); //set text size
tft.setTextColor(0xFFFF); //set text color to white
tft.print("Press me"); //print text
tft.drawTriangle(60, 100, 80, 120, 100, 100, 0xFFFF); //draw triangle
delay(1000); //delay for 1 second
}
}
Program an Arduino code where when a button is pressed,
a servo motor slowly moves back and forth 180 degrees. At the same time,
a WS2812B-LED ring in magenta, red and yellow will light up differently,
depending on the angle of the servo motor.
The higher the degree of the servo motor, the higher the brightness.
Parallel to the servo motor and the LED ring,
a GC9A01 LCD TFT display is also to be programmed in such a way
that a flashing heart symbol is displayed at the beginning of the keystroke.
As soon as the servo motor moves back,
the text "Happy Valentine's Day Sophia" should be displayed on the display in centered form.
As soon as the servo motor has returned to its origin,
the text "Press me" and a moving arrow pointing down should be shown on the display.
All executions should always be executed by pressing a button and no delay() may be used.
The speed of the servo motor should be able to be variably adjusted.
Use the FastLED.h library for the LED ring, which is equipped with 16 LEDs.
Use the Adafruit_GFX.h and the GC9A01 library for the TFT LCD display.
The button only needs to be pressed for longer than 100 milliseconds for the sketch to execute.
If the button is not pressed, the LED ring should be off and the servo motor should be at 0.
*/