#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Caleb Ogbeta MidTerm Exam CCE 599 SAM
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address, columns, rows
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int intensityPotPin = A0;
const int colorButtonPin = 2;
const int modeButtonPin = 3;
int ledColor = 0; // 0: White, 1: Blue, 2: Red, 3: Green
int intensity = 128; // Initial intensity (0-255)
int mode = 0; // 0: Constant, 1: Blinking Mode 1, 2: Blinking Mode 2
unsigned long previousMillis = 0;
const long interval1 = 2000; // Blinking Mode 1 interval (ms)
const long interval2 = 1000; // Blinking Mode 2 interval (ms)
bool ledState = false; // LED on/off state
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(colorButtonPin, INPUT_PULLUP);
pinMode(modeButtonPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("TSS Sensor"); // Total Suspended Solids (TSS) Sensor
}
void loop() {
// Read color button to change LED color
if (digitalRead(colorButtonPin) == LOW) {
ledColor = (ledColor + 1) % 4; // Cycle through colors
setRGBColor(ledColor, intensity);
updateLCD();
delay(200); // Debounce
}
// Read mode button to change operating mode
if (digitalRead(modeButtonPin) == LOW) {
mode = (mode + 1) % 3;
updateLCD();
delay(200); // Debounce
}
// Read potentiometer for intensity
intensity = analogRead(intensityPotPin) / 4; // Map 0-1023 to 0-255
// Update LED based on mode
switch (mode) {
case 0: // Constant Color
setRGBColor(ledColor, intensity);
break;
case 1: // Blinking Mode 1
blinkLED(interval1);
break;
case 2: // Blinking Mode 2
blinkLED(interval2);
break;
}
}
//void setRGBColor(int color, int brightness) {
// analogWrite(redPin, map(color, 0, 3, 0, brightness));
// analogWrite(greenPin, map(color, 0, 3, 0, brightness));
// analogWrite(bluePin, map(color, 0, 3, 0, brightness));
//}
void setRGBColor(int color, int brightness) {
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
switch (color) {
case 0: // White
redValue = brightness;
greenValue = brightness;
blueValue = brightness;
break;
case 1: // Blue
redValue = 0;
greenValue = 0;
blueValue = brightness;
break;
case 2: // Red
redValue = brightness;
greenValue = 0;
blueValue = 0;
break;
case 3: // Green
redValue = 0;
greenValue = brightness;
blueValue = 0;
break;
}
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
void blinkLED(long interval) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
ledState = !ledState;
if (ledState) {
setRGBColor(ledColor, intensity);
} else {
setRGBColor(0, 0); // Turn off LEDs
}
}
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode: ");
lcd.print(mode);
lcd.setCursor(0, 1);
lcd.print("LED: ");
lcd.print(ledState ? "On" : "Off");
lcd.setCursor(9, 0);
lcd.print("Color: ");
switch (ledColor) {
case 0:
lcd.print("White");
break;
case 1:
lcd.print("Blue");
break;
case 2:
lcd.print("Red");
break;
case 3:
lcd.print("Green");
break;
}
}