#include <FastLED.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin Definitions
#define LED_PIN 11
#define BUTTON_1_PIN 10
#define BUTTON_2_PIN 9
#define BUTTON_3_PIN 8
#define BUTTON_4_PIN 7
#define NUM_LEDS 4
// Initialize LED strip
CRGB leds[NUM_LEDS];
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Resistor bands and RGB values
struct ResistorBand {
String colorName;
String digit; // For first and second band
String multiplier; // For third band
String tolerance; // For fourth band
CRGB colorRGB;
};
// Define resistor bands
ResistorBand bands[] = {
{"Black", "0", "1", "", CRGB::Black}, // Black
{"Brown", "1", "10", "±1%", CRGB(150, 75, 0)}, // Brown
{"Red", "2", "100", "±2%", CRGB::Red}, // Red
{"Orange", "3", "1k", "", CRGB::Orange}, // Orange
{"Yellow", "4", "10k", "", CRGB::Yellow}, // Yellow
{"Green", "5", "100k", "±0.5%", CRGB::Green}, // Green
{"Blue", "6", "1M", "±0.25%", CRGB::Blue}, // Blue
{"Violet", "7", "10M", "±0.1%", CRGB(138, 43, 226)}, // Violet
{"Gray", "8", "100M", "±0.05%", CRGB::Gray}, // Gray
{"White", "9", "", "", CRGB::White}, // White
{"Gold", "", "0.1", "±5%", CRGB(255, 215, 0)}, // Gold
{"Silver", "", "0.01", "±10%", CRGB(192, 192, 192)} // Silver
};
// Variables to store the current state of the resistor bands
int bandIndices[4] = {0, 0, 0, 0}; // Stores the indices of the current selected color for each band
void setup() {
// Setup buttons
pinMode(BUTTON_1_PIN, INPUT_PULLUP);
pinMode(BUTTON_2_PIN, INPUT_PULLUP);
pinMode(BUTTON_3_PIN, INPUT_PULLUP);
pinMode(BUTTON_4_PIN, INPUT_PULLUP);
// Initialize LED
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
// Initialize LCD
lcd.init();
lcd.backlight();
// Display initial resistor value
displayResistorInfo();
}
void loop() {
if (digitalRead(BUTTON_1_PIN) == LOW) {
changeBandColor(0);
displayResistorInfo();
delay(500); // Debounce delay
}
if (digitalRead(BUTTON_2_PIN) == LOW) {
changeBandColor(1);
displayResistorInfo();
delay(500); // Debounce delay
}
if (digitalRead(BUTTON_3_PIN) == LOW) {
changeBandColor(2);
displayResistorInfo();
delay(500); // Debounce delay
}
if (digitalRead(BUTTON_4_PIN) == LOW) {
changeBandColor(3);
displayResistorInfo();
delay(500); // Debounce delay
}
}
// Function to change the color of the specified band
void changeBandColor(int band) {
bandIndices[band]++;
if (band < 2) { // For first two digits
bandIndices[band] %= 10; // Cycle through 0-9
} else if (band == 2) { // Multiplier
bandIndices[band] %= 12; // Cycle through 12 colors including Gold and Silver
} else if (band == 3) { // Tolerance
bandIndices[band] %= 5; // Cycle through 5 tolerance values
}
// Update the color of the LED corresponding to the band
leds[band] = bands[bandIndices[band]].colorRGB;
FastLED.show();
}
// Function to display the current resistor info on the LCD
void displayResistorInfo() {
lcd.clear();
lcd.setCursor(0, 0);
String resistanceValue = bands[bandIndices[0]].digit + bands[bandIndices[1]].digit;
resistanceValue += " x " + bands[bandIndices[2]].multiplier;
lcd.print("Res: " + resistanceValue + "ohms");
lcd.setCursor(0, 1);
lcd.print("Tol: " + bands[bandIndices[3]].tolerance);
}