Controlling vol. Numbers in ArduinosteemCreated with Sketch.

in #tutorial5 years ago

This tutorial is an open-source Arduino project that teaches how to make a BIG digit numbers displaying on 16X2 i2c type Liquid crystal display and I am going to control numbers up and down using the pushbuttons, the possible purpose of this lesson is you can make an analog 7 segment display using LCD and I am going to show you how to make a counter using this component.


Requirements

  • 16X2 LCD with IIC backpack
  • Push buttons
  • Breadboard
  • jumper wires
  • Type B USB cable
  • Arduino UNO R3 board
  • PC

CIRCUIT DIAGRAM

The 16X2 LCD display interfaces has 4 build-in backpack pins (1) VCC it refers to the power signal of the LCD typically connected to 5volts, (2)GND or sometimes zero voltage, It is also the common connection of the LCD must connect to in one way or another path in order to complete the circuit. (3) SDA and (4) SCL is the i2c serial bus pin it is used to synchronize all data transfers over the I2C bus from the 16 pin of the normal LCD, Both SCL and SDA are connected to analog pin outputs of the Arduino because i2c lines are open-drain drivers, means is that the chip can drive its output low.


The pushbuttons has built with 4 sets of pin we will use 2 set for the GNS and the digital in output on the Arduino


Download the Arduino Desktop IDE: https://www.arduino.cc/en/Main/Software
When the download is finished, un-zip it and open up the Arduino folder to confirm that click yes, there are some files and sub-folders inside. The file structure is important so don’t be moving any files around unless you really know what you’re doing.

Download the liquid crystal LCD library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library This library is a modified version of the H. Mario LiquidCrystal_I2C V.2.0 lib.

Build LCD Custom Character Generator using; http://maxpromer.github.io/LCD-Character-Creator/
it support character LCD and create code for Arduino. once you set the character it will generate a binary data type for Arduino code. each set of binary codes is composed of number 0-9.


SOURCE CODE:



#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int counter = 0;
const int Up_buttonPin = 2; // the pin that the pushbutton is attached to
const int Down_buttonPin = 3;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
int down_buttonState = 0; // current state of the up button
int down_lastButtonState = 0; // previous state of the up button
bool bPress = false;
byte LT[8] =
{
B00111,
B01111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte UB[8] =
{
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte RT[8] =
{
B11100,
B11110,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte LL[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B01111,
B00111
};
byte LB[8] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
};
byte LR[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11110,
B11100
};
byte MB[8] =
{
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B11111,
B11111
};
byte block[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
void setup()
{
pinMode( Up_buttonPin , INPUT_PULLUP);
pinMode( Down_buttonPin , INPUT_PULLUP);
lcd.begin(16,2); // initialize the lcd
lcd.createChar(0,LT);
lcd.createChar(1,UB);
lcd.createChar(2,RT);
lcd.createChar(3,LL);
lcd.createChar(4,LB);
lcd.createChar(5,LR);
lcd.createChar(6,MB);
lcd.createChar(7,block);
// Print a message to the LCD.
lcd.backlight();
lcd.clear();
displayNumber();
}
void printNumber(int val){
int col=5;
if( val >= 10){
printDigits(val/10,col);
printDigits(val%10,col+4);
}
else{
printDigits(val,col);
}
}
void loop()
{
checkUp();
checkDown();
if( bPress){
bPress = false;
displayNumber();
}
}
void displayNumber(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vol:");
printNumber( buttonPushCounter);
}
void custom0(int x){
lcd.setCursor(x,0);
lcd.write((byte)0);
lcd.write(1);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void custom1(int x){
lcd.setCursor(x,0);
lcd.write(1);
lcd.write(2);
lcd.print(" ");
lcd.setCursor(x,1);
lcd.write(4);
lcd.write(7);
lcd.write(4);
}
void custom2(int x){
lcd.setCursor(x,0);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(4);
}
void custom3(int x){
lcd.setCursor(x,0);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(4);
lcd.write(4);
lcd.write(5);
}
void custom4(int x){
lcd.setCursor(x,0);
lcd.write(3);
lcd.write(4);
lcd.write(7);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.print(" ");
lcd.write(7);
}
void custom5(int x){
lcd.setCursor(x,0);
lcd.write(3);
lcd.write(6);
lcd.write(6);
lcd.setCursor(x, 1);
lcd.write(4);
lcd.write(4);
lcd.write(5);
}
void custom6(int x){
lcd.setCursor(x,0);
lcd.write((byte)0);
lcd.write(6);
lcd.write(6);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void custom7(int x){
lcd.setCursor(x,0);
lcd.write(1);
lcd.write(1);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.print(" ");
lcd.write(7);
}
void custom8(int x){
lcd.setCursor(x,0);
lcd.write((byte)0);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void custom9(int x){
lcd.setCursor(x,0);
lcd.write((byte)0);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.print(" ");
lcd.write(7);
}
void printDigits(int digits, int x){
// utility function for digital clock display: prints preceding colon and leading 0
switch (digits) {
case 0:
custom0(x);
break;
case 1:
custom1(x);
break;
case 2:
custom2(x);
break;
case 3:
custom3(x);
break;
case 4:
custom4(x);
break;
case 5:
custom5(x);
break;
case 6:
custom6(x);
break;
case 7:
custom7(x);
break;
case 8:
custom8(x);
break;
case 9:
custom9(x);
break;
}
}
void checkUp()
{
up_buttonState = digitalRead(Up_buttonPin);
// compare the buttonState to its previous state
if (up_buttonState != up_lastButtonState) {
// if the state has changed, increment the counter
if (up_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
up_lastButtonState = up_buttonState;
}
void checkDown()
{
down_buttonState = digitalRead(Down_buttonPin);
// compare the buttonState to its previous state
if (down_buttonState != down_lastButtonState) {
// if the state has changed, increment the counter
if (down_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
down_lastButtonState = down_buttonState;
}


COPY AND UPLOAD the sketch on Arduino IDE:
Now, you can see the value of current counter digit displaying on the 2 row of the LCD, control the volume of the number by up and down 0-99 using the pushbuttons.




Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.036
BTC 107872.78
ETH 4397.57
USDT 1.00
SBD 0.83