#include <Arduino.h>
// Pin Definitions for Seven-Segment Display
#define SEG_A 12
#define SEG_B 13
#define SEG_C 14
#define SEG_D 15
#define SEG_E 16
#define SEG_F 17
#define SEG_G 18
// Pin Definitions for Buttons and LEDs
#define LED_PIN 25
#define BUZZER_PIN 10
#define BUTTON_PIN 28 // Heartbeat button
#define BLUE_BUTTON_PIN 22 // Tired button
#define RED_BUTTON_PIN 21 // Angry button
// Bar Graph LED Pins
int barGraphPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int numLEDs = sizeof(barGraphPins) / sizeof(barGraphPins[0]);
// Timing Variables
unsigned long lastActivityTime = 0;
const unsigned long inactivityThreshold = 4000; // 4 seconds
// Timing for LED Blinking
unsigned long lastBlinkTime = 0;
const unsigned long blinkInterval = 750; // 750ms for heartbeat LED (slowed down)
bool ledState = LOW;
void setup() {
// Set up Seven-Segment Display Pins
pinMode(SEG_A, OUTPUT);
pinMode(SEG_B, OUTPUT);
pinMode(SEG_C, OUTPUT);
pinMode(SEG_D, OUTPUT);
pinMode(SEG_E, OUTPUT);
pinMode(SEG_F, OUTPUT);
pinMode(SEG_G, OUTPUT);
// Set up Bar Graph Pins
for (int i = 0; i < numLEDs; i++) {
pinMode(barGraphPins[i], OUTPUT);
}
// Set up Buttons
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
// Set up Buzzer and LED
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Debugging
Serial.begin(9600);
Serial.println("Setup complete!");
}
void loop() {
// Handle LED blinking for heartbeat
unsigned long currentTime = millis();
if (currentTime - lastBlinkTime >= blinkInterval) {
lastBlinkTime = currentTime;
ledState = !ledState; // Toggle LED state
digitalWrite(LED_PIN, ledState);
}
// Read Button States
bool heartbeatButtonState = (digitalRead(BUTTON_PIN) == LOW);
bool blueButtonState = (digitalRead(BLUE_BUTTON_PIN) == LOW);
bool redButtonState = (digitalRead(RED_BUTTON_PIN) == LOW);
// Handle Other Buttons
if (heartbeatButtonState) {
lastActivityTime = millis();
playHeartbeatMelody();
lightBarGraph(2);
displayCharacter('H'); // Display "H" for Heartbeat
Serial.println("Heartbeat button pressed.");
} else if (blueButtonState) {
lastActivityTime = millis();
playSoothingTone();
lightBarGraph(5);
displayCharacter('T'); // Display "T" for Tired
Serial.println("Tired button pressed.");
} else if (redButtonState) {
lastActivityTime = millis();
playAggressiveTone();
lightBarGraph(8);
displayCharacter('A'); // Display "A" for Angry
Serial.println("Angry button pressed.");
} else {
if (currentTime - lastActivityTime > inactivityThreshold) {
performRandomBehavior();
lastActivityTime = currentTime; // Reset inactivity timer
}
clearDisplay(); // Turn off Seven-Segment Display when no button is pressed
}
}
// Light up Bar Graph
void lightBarGraph(int numLights) {
for (int i = 0; i < numLEDs; i++) {
digitalWrite(barGraphPins[i], i < numLights ? HIGH : LOW);
}
}
// Play Heartbeat Melody
void playHeartbeatMelody() {
// Play a short note, followed by a slightly longer one
nonBlockingTone(262, 200); // C4, short note (200ms)
delay(100); // Small pause
nonBlockingTone(330, 400); // E4, longer note (400ms)
delay(100); // Pause before the next beat
}
// Play Soothing Tone
void playSoothingTone() {
nonBlockingTone(200, 500);
}
// Play Aggressive Tone
void playAggressiveTone() {
nonBlockingTone(5000, 500);
}
// Perform Random Behavior
void performRandomBehavior() {
int randomAction = random(0, 3); // Generate random number between 0 and 2
Serial.print("Random Action: ");
Serial.println(randomAction); // Debugging
switch (randomAction) {
case 0:
playHeartbeatMelody();
lightBarGraph(2);
displayCharacter('H');
break;
case 1:
playSoothingTone();
lightBarGraph(5);
displayCharacter('T');
break;
case 2:
playAggressiveTone();
lightBarGraph(8);
displayCharacter('A');
break;
}
}
// Non-Blocking Tone
void nonBlockingTone(int frequency, int duration) {
tone(BUZZER_PIN, frequency);
delay(duration);
noTone(BUZZER_PIN);
}
// Display Character on Seven-Segment Display
void displayCharacter(char character) {
clearDisplay(); // Turn off all segments first
Serial.print("Displaying character: ");
Serial.println(character); // Debugging
switch (character) {
case 'H': // Display "H"
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 'T': // Display "T"
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
break;
case 'A': // Display "A"
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
}
}
// Clear Seven-Segment Display
void clearDisplay() {
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
}