#include <LiquidCrystal.h> // Include the LiquidCrystal library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD with the appropriate pins
// Define button pins
const int blackButton = 8;
const int brownButton = 9;
const int redButton = 10;
const int goldButton = 11;
// Resistor color codes
const char* colors[] = {"Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Gray", "White"};
// Resistor values
int resistorValues[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// Variables to store selected colors
int band1Color = -1;
int band2Color = -1;
int multiplierColor = -1;
int toleranceColor = -1; // You can add more bands for tolerance if needed
void setup() {
// Set button pins as INPUT_PULLUP
pinMode(blackButton, INPUT_PULLUP);
pinMode(brownButton, INPUT_PULLUP);
pinMode(redButton, INPUT_PULLUP);
pinMode(goldButton, INPUT_PULLUP);
// Initialize the LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Enter Resistor:");
}
void loop() {
// Read button states and update selected colors
updateSelectedColors();
// If all colors are selected, calculate and display resistor value
if (band1Color != -1 && band2Color != -1 && multiplierColor != -1 && toleranceColor != -1) {
int resistorValue = (band1Color * 10 + band2Color) * resistorValues[multiplierColor];
// Display resistor value on LCD
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(resistorValue);
}
}
void updateSelectedColors() {
band1Color = readColor(blackButton);
band2Color = readColor(brownButton);
multiplierColor = readColor(redButton);
toleranceColor = readColor(goldButton);
// You can add more bands and buttons for additional accuracy.
}
int readColor(int buttonPin) {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
// Button is pressed, cycle through colors
while (digitalRead(buttonPin) == LOW); // Wait for button release
return (buttonState == LOW) ? 0 : 1; // Return 0 (Black) or 1 (Brown)
}
return -1; // Default to -1 when the button is not pressed
}