Friday 2 April 2021

LCD Timer - i2c - Arduino

This is a nice simple timer circuit using an Arduino Uno, a LCD, two buttons, 
two LEDS and some resistors.
 

It's a stopwatch (and not directly synth related) , but the basic code is great for working out how an arduino clock works and how to display that info on a LCD.
Here is the instructables link:
 
 

 
I've changed the code a little to work with my LCD
And varied the resistors.
The switches are connected to gnd with 330 ohm resistors
The LEDs anodes connect to pins 2 & 3 via 220 ohm resistors.
Cathodes then connect straight to gnd.

Image made using Tinkercad

 

Many thanks to Jay 625549

The code is here:
//********************************
//Jay Raut Jean Augustine Secondary
//January 13 2020
//625549@pdsb.net
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
lcd.backlight();//turns on backlight
pinMode(8, INPUT);//setting up Pins on arduino for output and input from leds and buttons
digitalWrite(8, HIGH);
pinMode(9, INPUT);
digitalWrite(9, HIGH);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);

}
double i = 0;//variables for recording time
double a = millis();
double c ;
void loop()
{
lcd.clear();
lcd.print("press the button");

 if(digitalRead(8) == LOW)//clears LCD when button is pressed
 {
    lcd.clear();
    a = millis();

   while(digitalRead(9) == HIGH)
   {
   c = millis();//function to start recording when the button was pressed to print on the LCD
   i = (c - a) / 1000;
   lcd.print(i);
   lcd.setCursor(7,0);
   lcd.print("Seconds");
   lcd.setCursor(0,0);
   digitalWrite(2, HIGH);//turns on green led
   digitalWrite(3, LOW);//turns off red led
   }
   if(digitalRead(9) == LOW)
   {
     while(digitalRead(8) == HIGH)//if other button is pressed then clear the LCD
     {
       lcd.setCursor(0,0);

       lcd.setCursor(11,0);
       lcd.print("");
       lcd.setCursor(0,0);
          digitalWrite(2, LOW);//turn off the green led
          digitalWrite(3, HIGH);//turn on the red led
     }
   }
 }
}

// *****************************
 

Links
+ Momentary push buttons & Pull down resistors  

 ---------------------------------
-------------------------------------

No comments:

Post a Comment