/**********************************************************************
Summary:
This code creates a heartbeat sound using an Arduino and a speaker, with intervals that
increase and then decrease following the Fibonacci sequence. It also uses an LCD2004
display to show the current heartbeat interval.
Key Components:
Speaker Pin: Uses pin 3 for the speaker.
Intervals: Defines heartbeat intervals using the first few Fibonacci numbers
(in milliseconds) that increase up to 21 seconds and then decrease back to 1 second.
LCD Initialization: Uses the LiquidCrystal_I2C library to initialize the LCD2004
display with the I2C address (usually 0x27) and dimensions (20x4).
Setup Function:
Initializes the speaker pin as an output.
Starts serial communication.
Initializes and turns on the LCD backlight.
Loop Function:
Iterates through the heartbeat intervals:
Constructs a message and prints it to the serial monitor.
Displays the interval on the LCD.
Plays two 150Hz tones for 100 milliseconds each to simulate the "lub-dub" sound of a heartbeat.
Waits for the rest of the interval minus the tone durations.
Adds a 1-second pause before restarting the sequence.
This code effectively combines the natural rhythm of a heartbeat with the
mathematical elegance of the Fibonacci sequence and provides a visual display
of the intervals on an LCD. If you have any more questions or need further
adjustments, feel free to ask! ❤️📐
BY ARVIND PATIL INDIS 6/01/25
********************************************************/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int speakerPin = 3;
// Intervals (in milliseconds) to simulate increasing and then decreasing heartbeat intervals
int intervals[] = {1000, 1000, 2000, 3000, 5000, 8000, 13000, 21000,
21000, 13000, 8000, 5000, 3000, 2000, 1000, 1000};
// Initialize the LCD with the I2C address (usually 0x27) and dimensions (20x4)
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
pinMode(speakerPin, OUTPUT); // Set the speaker pin as an output
Serial.begin(9600); // Initialize serial communication at a baud rate of 9600
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
}
void loop() {
for (int i = 0; i < 16; i++) { // Loop through the heartbeat intervals
int interval = intervals[i]; // Get the current interval
String message = "Playing heartbeat for " + String(interval / 1000.0) + " seconds...";
Serial.println(message); // Print the message to the serial monitor
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Interval: ");
lcd.setCursor(0, 1);
lcd.print(interval / 1000.0);
lcd.print(" sec");
lcd.setCursor(0, 2);
lcd.print(interval / 1000.0);
lcd.print(" FIBONACCI BIT");
lcd.setCursor(0, 3);
lcd.print(" BY ARVIND 6 /1/25");
// Simulate the "lub" sound of the heartbeat
tone(speakerPin, 150, 100); // Play a 150Hz tone for 100 milliseconds
delay(200); // Short pause between "lub" and "dub"
// Simulate the "dub" sound of the heartbeat
tone(speakerPin, 150, 100); // Play a 150Hz tone for 100 milliseconds
delay(interval - 300); // Wait for the rest of the interval
noTone(speakerPin); // Stop the tone
delay(100); // Small delay between beats
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting 1 sec...");
delay(1000); // 1-second pause before restarting the sequence
}