You are on page 1of 6

/*

LCD and RGB LED sketch to display different colours and patterns depending on
the value read by a potentiometer.
Author: Dan Gregory.
Date: 27.6.22

Hardware setup:
RGB led with red pin to digital 10, blue to digital 9, and green to digital 8. All through
220 ohm resistors.
LCD with i2c protocol, and potentiometer read through analog pin A2.

*/

#include <Adafruit_LiquidCrystal.h> //include lcd library.

Adafruit_LiquidCrystal lcd_1(0); //initialize the lcd.


const int redPin = 10; // define consts here to hold pin values for R, G and B led pins.
const int bluePin = 9;
const int greenPin = 8;
const int potPin = A2; //assign analog A2 to potPin constant to read the Pot.
int potRead = 0; //create int potRead to store read value from pot.

void setup() //set up code, runs once only.


{
pinMode(redPin, OUTPUT); //set the LED pins to outputs to control the RGB LED.
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(potPin, INPUT); //set the pot pin to input to read the analog signal from it.

lcd_1.begin(16, 2); //set up the LCD with 16 columns, and 2 rows.


lcd_1.setCursor(4, 0); //center the cursor for welcome
lcd_1.print("Welcome"); // display welcome message
lcd_1.setCursor(1, 1); //set cursor for please wait message
lcd_1.print("Please Wait..."); //print please wait message centered.
delay(2000); //display this message for 2 seconds.
lcd_1.clear(); //clear the lcd so it's able to display the loop code.
lcd_1.setCursor(1, 0);
lcd_1.print("The Colour Is:"); //display this message centered and keep it on screen, no
clear.
}

void loop() //begin of loop code to run continuously.


{
potRead = analogRead(potPin); // read pot pin and store in the variable potRead.

/*
The following code is a series of if statements, I won't comment them all as there isn't much
time
But each one checks the pot and if it meets the conditions of the if statement, the code will
set
the led pins (red, blue, and green), to either HIGH or LOW. This will result in different
colours
being displayed when the pot is turned from zero, through to 1023 (max).
The series of colours that the led will display as it's turned higher is
OFF - Red - Pink - Blue - Cyan - Green - Blinking.
At a value of 0, the light is off, and above pot value of 1019, it's blinking.
There are equal zones between these points where the colours are displayed.
*/

if (potRead == 0) // if the pot value is 0...


{
lcd_1.setCursor(6, 1); //center cursor
lcd_1.print("OFF"); //then print "OFF" on the LCD.
digitalWrite(redPin, LOW); //turn all pins for the led to low.
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, LOW);
}

if (potRead > 0 && potRead < 204) //range to display red, and print red to lcd.
{
lcd_1.setCursor(6, 1);
lcd_1.print("Red");
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, LOW);
}

if (potRead > 203 && potRead < 408)//range to display pink, and print pink to lcd.
{
lcd_1.setCursor(6, 1);
lcd_1.print("Pink");
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, HIGH);
digitalWrite(greenPin, LOW);
}

if (potRead > 407 && potRead < 612) //range to display blue, and print blue to lcd
{
lcd_1.setCursor(6, 1);
lcd_1.print("Blue");
digitalWrite(redPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(greenPin, LOW);
}

if (potRead > 611 && potRead < 816) // range to display cyan, and print cyan to display.
{
lcd_1.setCursor(6, 1);
lcd_1.print("Cyan");
digitalWrite(redPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(greenPin, HIGH);
}

if (potRead > 815 && potRead < 1020) // range to display green, and print green to
display.
{
lcd_1.setCursor(5, 1);
lcd_1.print("Green");
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, HIGH);
}

if (potRead > 1019) //range to display blinking led, and print BLINKY! to lcd.
{
lcd_1.setCursor(5, 1); //sets the cursor to be centered.
lcd_1.print("BLINKY!"); // print BLINKY on the lcd to indicate blinking.
digitalWrite(redPin, HIGH); // setting each colour on here for 1 sec.
delay(1000);
digitalWrite(bluePin, HIGH);
delay(1000);
digitalWrite(greenPin, HIGH);
delay(1000);
digitalWrite(redPin, LOW); // this area turns all off.
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, LOW);
delay(1000); // all off for 1 second with this delay.
}

delay(1000); //delay for stability and so lcd displays info for long enough to see.
lcd_1.setCursor(5, 1); // set cursor for clearing bottom row.
lcd_1.print(" "); // clear bottom row to prevent overwriting and jumbling lcd message.
}

You might also like