#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and dimensions
// Define button pins
const int buttonPins[] = {2, 3, 4, 5};
// Resistor color codes
const char* colors[] = {"Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Gray", "White"};
// Variables to store selected colors
int selectedColors[4] = {0}; // Initialize with all colors as Black (0)
void setup() {
// Set button pins as INPUT_PULLUP
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Initialize the LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Resistor Value:");
}
void loop() {
// Read button states and update selected colors
updateSelectedColors();
// Calculate resistor value
int resistorValue = (selectedColors[0] * 10 + selectedColors[1]) * selectedColors[2];
// Display resistor value on LCD
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
lcd.print(resistorValue);
}
void updateSelectedColors() {
for (int i = 0; i < 4; i++) {
int buttonState = digitalRead(buttonPins[i]);
if (buttonState == LOW) {
// Button is pressed, cycle through colors
while (digitalRead(buttonPins[i]) == LOW); // Wait for button release
selectedColors[i] = (selectedColors[i] + 1) % 10; // Cycle through colors 0-9
}
}
}