/****************************************************
The modified code creates a heartbeat sound using an Arduino and a speaker,
with intervals that follow the Fibonacci sequence:
Speaker Pin: Uses pin 3 for the speaker.
Fibonacci Intervals: Defines heartbeat intervals using the first few Fibonacci numbers
(in milliseconds): 1000, 1000, 2000, 3000, 5000, 8000, 13000, 21000.
Setup Function: Initializes the speaker pin as an output and starts serial communication.
Loop Function: Iterates through the Fibonacci intervals and plays the heartbeat sounds:
Plays two 150Hz tones for 100 milliseconds each to simulate the "lub-dub" sound of a heartbeat.
Delays for the rest of the interval minus the tone durations.
Prints the current interval to the serial monitor for readability.
Execution: Simulates the heartbeat rhythm following the Fibonacci sequence, creating a
progressively expanding interval pattern.
This code effectively combines the natural rhythm of a heartbeat with the
mathematical elegance of the Fibonacci sequence. If you have any more questions or need further adjustments, feel free to ask! ❤️📐
CRATEDD BY ARVIND PATUL ON 6/JAN2025
**************************************/
const int speakerPin = 3;
// Heartbeat intervals (in milliseconds)
int intervals[] = {800, 800, 1600, 2400, 4000, 6400, 10400, 16800};
void setup() {
// Initialize speaker pin as output
pinMode(speakerPin, OUTPUT);
// Initialize serial communication
Serial.begin(9600); // Start serial communication at 9600 baud rate
}
void loop() {
// Loop through the intervals and play heartbeat sound
for (int i = 0; i < 8; i++) {
int interval = intervals[i];
// Print the current interval to the serial monitor
Serial.print("Playing heartbeat for ");
Serial.print(interval / 1000.0); // Convert to seconds for readability
Serial.println(" seconds...");
// Simulate the "lub" sound of the heartbeat
tone(speakerPin, 150, 100); // 150Hz frequency for 100ms
delay(200); // Short pause between "lub" and "dub"
// Simulate the "dub" sound of the heartbeat
tone(speakerPin, 150, 100); // 150Hz frequency for 100ms
delay(interval - 300); // Wait for the rest of the interval before next sound
// Stop the tone
noTone(speakerPin);
// Delay before the next sound
delay(100); // A small delay between beats
}
// Optionally, loop or stop after a single cycle
while (true); // Stops the loop, remove this line if you want the loop to repeat
}