0% found this document useful (0 votes)
21 views7 pages

MATHEW

The document contains code for three experiments using an Arduino and LCD display to time events. Experiment 1 displays "Hello World" on the LCD. Experiment 2 adds buttons to start/stop and reset a timer, displaying elapsed time on the LCD. Experiment 3 adds a lap button to record lap times in an array, writing to EEPROM and displaying laps on the LCD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

MATHEW

The document contains code for three experiments using an Arduino and LCD display to time events. Experiment 1 displays "Hello World" on the LCD. Experiment 2 adds buttons to start/stop and reset a timer, displaying elapsed time on the LCD. Experiment 3 adds a lap button to record lap times in an array, writing to EEPROM and displaying laps on the LCD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT ONE CODE

#include <Wire.h> // Include the Wire library for I2C communication

#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal I2C library

LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address (0x27 for a 16x2 display)

void setup() {

lcd.init(); // Initialize the LCD

lcd.backlight(); // Turn on the backlight (if available)

lcd.setCursor(0, 0); // Set cursor to the first row, first column

lcd.print("Hello, World!");

void loop() {

// Your code goes here

EXPERIMENT 2 CODE
#include <Wire.h> // Include the Wire library for I2C communication

#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal I2C library

LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address (0x27 for a 16x2 display)

const int startStopButtonPin = 2; // Pin for the Start/Stop button

const int resetButtonPin = 3; // Pin for the Reset button

unsigned long startTime = 0; // Variable to store the start time

unsigned long elapsedTime = 0; // Variable to store the elapsed time

boolean timing = false; // Flag to indicate if timing is active

void setup() {

lcd.init(); // Initialize the LCD


lcd.backlight(); // Turn on the backlight (if available)

lcd.setCursor(0, 0); // Set cursor to the first row, first column

lcd.print(“Press Start”);

pinMode(startStopButtonPin, INPUT_PULLUP); // Set the Start/Stop button pin as input

pinMode(resetButtonPin, INPUT_PULLUP); // Set the Reset button pin as input

void loop() {

// Check if the Start/Stop button is pressed

if (digitalRead(startStopButtonPin) == LOW) {

if (!timing) {

startTime = millis(); // Start timing

timing = true;

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Timing...");

} else {

elapsedTime += millis() - startTime; // Stop timing

timing = false;

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Press Start");

delay(100); // Debounce

// Check if the Reset button is pressed


if (digitalRead(resetButtonPin) == LOW) {

elapsedTime = 0; // Reset the elapsed time

timing = false;

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Press Start");

delay(100); // Debounce

// Display the elapsed time on the LCD

lcd.setCursor(0, 1);

lcd.print(“Time: “);

lcd.print(elapsedTime / 1000); // Convert milliseconds to seconds

lcd.print(“s”);

EXPERIMENT 3 CODE
#include <Wire.h> // Include the Wire library for I2C communication

#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal I2C library

#include <EEPROM.h> // Include the EEPROM library

LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address (0x27 for a 16x2 display)

const int startStopButtonPin = 2; // Pin for the Start/Stop button

const int resetButtonPin = 3; // Pin for the Reset button

const int lapButtonPin = 4; // Pin for the Lap button

unsigned long startTime = 0; // Variable to store the start time

unsigned long elapsedTime = 0; // Variable to store the elapsed time


boolean timing = false; // Flag to indicate if timing is active

unsigned long lapTimes[10]; // Array to store lap times (adjust the size as needed)

int lapCount = 0; // Variable to keep track of lap count

int eepromAddress = 0; // Start writing to EEPROM address 0

void setup() {

lcd.init(); // Initialize the LCD

lcd.backlight(); // Turn on the backlight (if available)

lcd.setCursor(0, 0); // Set cursor to the first row, first column

lcd.print(“Press Start”);

pinMode(startStopButtonPin, INPUT_PULLUP); // Set the Start/Stop button pin as input

pinMode(resetButtonPin, INPUT_PULLUP); // Set the Reset button pin as input

pinMode(lapButtonPin, INPUT_PULLUP); // Set the Lap button pin as input

// Load lap times from EEPROM

for (int i = 0; i < 10; i++) { // Adjust the loop based on the number of laps you want to store

lapTimes[i] = EEPROMReadInt(eepromAddress);

eepromAddress += sizeof(int);

void loop() {

// Check if the Start/Stop button is pressed

if (digitalRead(startStopButtonPin) == LOW) {

if (!timing) {

startTime = millis(); // Start timing

timing = true;

lcd.clear();
lcd.setCursor(0, 0);

lcd.print(“Timing...”);

} else {

elapsedTime += millis() - startTime; // Stop timing

timing = false;

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Press Start");

delay(100); // Debounce

// Check if the Lap button is pressed

if (digitalRead(lapButtonPin) == LOW && timing) {

// Record the lap time

lapTimes[lapCount] = millis() - startTime;

// Write the lap time to EEPROM

EEPROMWriteInt(eepromAddress, lapTimes[lapCount]);

eepromAddress += sizeof(int);

lapCount++;

// Display the lap time on the LCD

lcd.setCursor(0, 2 + lapCount); // Adjust the row based on your LCD size

lcd.print(“Lap”);

lcd.print(lapCount);

lcd.print(“:”);

lcd.print(lapTimes[lapCount - 1] / 1000); // Convert milliseconds to seconds


lcd.print(“s”);

delay(100); // Debounce

// Check if the Reset button is pressed

if (digitalRead(resetButtonPin) == LOW) {

// Reset lap times and clear EEPROM

lapCount = 0;

for (int i = 0; i < eepromAddress; i++) {

EEPROM.write(i, 0);

eepromAddress = 0;

// Reset timing

elapsedTime = 0;

timing = false;

// Clear the LCD

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Press Start");

delay(100); // Debounce

// Display the elapsed time on the LCD

lcd.setCursor(0, 1);

lcd.print(“Time: ”);

lcd.print(elapsedTime / 1000); // Convert milliseconds to seconds

lcd.print(“s”);
}

// Custom function to read an integer from EEPROM

int EEPROMReadInt(int address) {

byte lowByte = EEPROM.read(address);

byte highByte = EEPROM.read(address + 1);

return (highByte << 8) | lowByte;

// Custom function to write an integer to EEPROM

void EEPROMWriteInt(int address, int value) {

byte lowByte = (byte)(value & 0xFF);

byte highByte = (byte)((value >> 8) & 0xFF);

EEPROM.write(address, lowByte);

EEPROM.write(address + 1, highByte);

You might also like