// LCD1602 to Arduino Uno connection example
#include <LiquidCrystal.h>
/************************************************************
/
/Please note that this TinkerCAD sketch provides a reduced down
/version of the Adafruit_RGBLCDShield library. This library
/does not support buttons functionality nor does it support
/backlight colour changing.
/
/We have added buttons functionality to the sketch which can
/be accessed in the same manner as the Adafruit_RGBLCDShield
/library, but instead of calling lcd.readButtons(), you
/can directly call readButtons() for the same effect.
/
/As for the backlight colour, we have added an RGB LED which
/can be used instead of the coloured lcd background.
/This can be accessed in the same manner as the
/Adafruit_RGBLCDShield library, but instead of calling
/lcd.setBacklight(uint8_t status), you can directly call
/setBacklight(uint8_t status) for the same effect.
/
************************************************************/
// DONT CHANGE ANYTHING BETWEEN HERE AND THE LINE
// SAYING "END OF LBORO CODE"
#define BUTTON_UP 0x08 //!< Up button
#define BUTTON_DOWN 0x04 //!< Down button
#define BUTTON_LEFT 0x10 //!< Left button
#define BUTTON_RIGHT 0x02 //!< Right button
#define BUTTON_SELECT 0x01 //!< Select button
#define BL_OFF 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define PURPLE 5
#define TEAL 6
#define WHITE 7
//Define pins for LCD Backlight and RGB for LED light
#define LCD_Backlight 10
#define Red 6
#define Green 5
#define Blue 3
//Set pins for LCD
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
uint8_t readButtons();
void setBacklight(uint8_t status);
//Reads pressed buttons and return a single int with bits
//representing pressed buttons (1 for pressed, 0 for not pressed)
uint8_t readButtons() {
uint8_t reply = 0x00;
float select_voltage = analogRead(0) * (5.0 / 1023.0);
float up_voltage = analogRead(2) * (5.0 / 1023.0);
float down_voltage = analogRead(3) * (5.0 / 1023.0);
float right_voltage = analogRead(4) * (5.0 / 1023.0);
float left_voltage = analogRead(1) * (5.0 / 1023.0);
if(right_voltage == 0) reply |= (1UL << 1);//BUTTON_RIGHT
if(up_voltage == 0) reply |= (1UL << 3);//BUTTON_UP
if(down_voltage == 0) reply |= (1UL << 2);//BUTTON_DOWN
if(left_voltage == 0) reply |= (1UL << 4);//BUTTON_LEFT
if(select_voltage == 0) reply |= (1UL << 0);//BUTTON_SELECT
return reply;
}
// Function to set the backlight (RGB LED here)
void setBacklight(uint8_t status) {
//Set LED RGB light by providing rgb values (0-255)
switch(status){
case BL_OFF:
{
analogWrite(Red, 0);
analogWrite(Green, 0);
analogWrite(Blue, 0);
break;
}
case WHITE:
{
analogWrite(Red, 255);
analogWrite(Green, 255);
analogWrite(Blue, 255);
break;
}
case RED:
{
analogWrite(Red, 255);
analogWrite(Green, 0);
analogWrite(Blue, 0);
break;
}
case GREEN:
{
analogWrite(Red, 0);
analogWrite(Green, 255);
analogWrite(Blue, 0);
break;
}
case BLUE:
{
analogWrite(Red, 0);
analogWrite(Green, 0);
analogWrite(Blue, 255);
break;
}
case YELLOW:
{
analogWrite(Red, 255);
analogWrite(Green, 255);
analogWrite(Blue, 0);
break;
}
case PURPLE:
{
analogWrite(Red, 128);
analogWrite(Green, 0);
analogWrite(Blue, 128);
break;
}
case TEAL:
{
analogWrite(Red, 0);
analogWrite(Green, 128);
analogWrite(Blue, 128);
break;
}
}
}
// END OF LBORO CODE
void setup() {
//Setup serial port
Serial.begin(9600);
//Initialise LCD screen
lcd.begin(16, 2);
// DON'T REMOVE THESE LINES
//Set Backlight pin to OUTPUT
pinMode(LCD_Backlight, OUTPUT);
//Set LCD Backlight to high
digitalWrite(LCD_Backlight, HIGH);
//Clear lcd
lcd.clear();
//Set LED RGB pins to OUTPUT
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Blue, OUTPUT);
// Put your code here.
// ...
}
void loop() {
// This is example code. You should delete
// all of this and replace it with your ideas.
//Set LCD crusor at the beging of the first line
lcd.setCursor(0, 0);
lcd.print("Button pressed:");
//Set LCD crusor at the beging of the second line
lcd.setCursor(0, 1);
//Print which button is pressed and
//activate the corresponding LED RGB light
byte pressedButton = readButtons();
//Check if RIGHT button is pressed
if(pressedButton & BUTTON_RIGHT){
lcd.print(" Right ");
setBacklight(RED);
}
//Check if UP button is pressed
if(pressedButton & BUTTON_UP){
lcd.print(" Up ");
setBacklight(GREEN);
}
//Check if DOWN button is pressed
if(pressedButton & BUTTON_DOWN){
lcd.print(" Down ");
setBacklight(BLUE);
}
//Check if LEFT button is pressed
if(pressedButton & BUTTON_LEFT){
lcd.print(" Left ");
setBacklight(YELLOW);
}
//Check if SELECT button is pressed
if(pressedButton & BUTTON_SELECT){
lcd.print(" Select ");
setBacklight(PURPLE);
}
//If no buttons are pressed clear line
if(!pressedButton){
lcd.print(" ");
setBacklight(WHITE);
}
}