//navigation
#include <RotaryEncoder.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
// --------------------------
// TFT Display Pin Definitions
// --------------------------
#define TFT_CS 17
#define TFT_DC 15
#define TFT_RST 16
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// --------------------------
// Rotary Encoder Pin Definitions
// --------------------------
#define ENCODER_CLK 2 // Encoder A (CLK)
#define ENCODER_DT 3 // Encoder B (DT)
#define ENCODER_SW 4 // Encoder push button
// Create rotary encoder instance (using a common latch mode)
RotaryEncoder encoder(ENCODER_CLK, ENCODER_DT, RotaryEncoder::LatchMode::TWO03);
// --------------------------
// LED Ring Definitions
// --------------------------
#define LED_PIN 5 // Pin for LED ring data (choose a free pin)
#define NUM_LEDS 8
Adafruit_NeoPixel ledRing = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
// --------------------------
// Global variables
// --------------------------
int lastPos = 0;
int currentLEDIndex = 0; // 0-based index for LED ring
// --------------------------
// Function to update the LED ring: only one LED is lit
// --------------------------
void updateLedRing() {
// Turn off all LEDs
for (int i = 0; i < NUM_LEDS; i++) {
ledRing.setPixelColor(i, 0);
}
// Light the current LED (using white as an example)
ledRing.setPixelColor(currentLEDIndex, ledRing.Color(255, 255, 255));
ledRing.show();
}
void setup() {
Serial.begin(115200);
// Initialize TFT display
tft.begin();
tft.setRotation(1); // Adjust rotation if needed
tft.fillScreen(ILI9341_BLACK);
// Initialize rotary encoder push button
pinMode(ENCODER_SW, INPUT_PULLUP);
// Initialize encoder position
encoder.setPosition(0);
lastPos = 0;
// Initialize LED ring
ledRing.begin();
ledRing.show(); // Turn off all LEDs initially
currentLEDIndex = 0;
updateLedRing();
}
void loop() {
// Update rotary encoder state
encoder.tick();
int newPos = encoder.getPosition();
if (newPos != lastPos) {
// Clear TFT display
tft.fillScreen(ILI9341_BLACK);
// Set text parameters
tft.setCursor(20, 150);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
if (newPos > lastPos) {
tft.println("Turn Right");
// Rotate right: increment LED index, wrapping around
currentLEDIndex = (currentLEDIndex + 1) % NUM_LEDS;
} else {
tft.println("Turn Left");
// Rotate left: decrement LED index, wrapping around
currentLEDIndex = (currentLEDIndex - 1 + NUM_LEDS) % NUM_LEDS;
}
lastPos = newPos;
updateLedRing();
}
// Check if button is pressed (active LOW)
if (digitalRead(ENCODER_SW) == LOW) {
// Clear TFT display and show button message
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 150);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.println("Button Pressed");
// Reset LED ring: set to original state (only LED 0 lit)
currentLEDIndex = 0;
updateLedRing();
// Debounce delay and wait until button is released
delay(300);
while (digitalRead(ENCODER_SW) == LOW) {
delay(10);
}
delay(300);
}
delay(10); // Small delay to reduce CPU load
}