I want to run 2 functions at once, those being playing an animation on the oled 0.96 display and playing a sound on the buzzer. How would I go about doing that? I tried looking around but couldn't find anything usefull.
Now it plays the sound then the animation. I want the sound to play while the animation is playing.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <CuteBuzzerSounds.h>
#include "idle.h"
#include "blink.h"
#include "happy.h"
#include "happy2.h"
#define BUZZER_PIN D6
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int TouchPin = D7; // Touch sensor pin
const float R1 = 20000.0; // 20k Ohm
const float R2 = 10000.0; // 10k Ohm
// Function to display a single frame
void displayFrame(const uint8_t* frame) {
display.clearDisplay();
display.drawBitmap(0, 0, frame, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);
display.display();
}
int analogValue = analogRead(A0);
float voltage = analogValue * (3.3 / 1023.0);
float inputVoltage = voltage * ((R1 + R2) / R2);
bool isBlinkPlaying = false;
unsigned long lastTouchCheckTime = 0;
const unsigned long touchCheckInterval = 100; // Check touch sensor every 100 milliseconds
void setup() {
Serial.begin(115200);
pinMode(TouchPin, INPUT); // Initialize touch sensor pin
cute.init(BUZZER_PIN);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("Loading animations...");
display.display();
randomSeed(analogRead(0)); // Initialize random seed for random delays
}
void playBlinkAnimation() {
static unsigned long lastBlinkTime = 0;
static unsigned long blinkDelay = 0;
static size_t blinkFrameIndex = 0;
unsigned long currentMillis = millis();
// Check if it's time to blink
if (currentMillis - lastBlinkTime >= blinkDelay) {
lastBlinkTime = currentMillis;
isBlinkPlaying = true;
for (size_t i = 0; i < blink_frames_count; ++i) {
displayFrame(blink_frames[i]);
delay(blink_frameDelay);
}
// Update the blink delay
blinkDelay = random(5000, 10000); // Random delay between 5000 ms and 10000 ms
}
}
void playHappyAnimation(const uint8_t* frames[], size_t frameCount) {
static unsigned long lastBlinkTime = 0;
static unsigned long blinkDelay = 0;
static size_t blinkFrameIndex = 0;
unsigned long currentMillis = millis();
// Check if it's time to blink
if (currentMillis - lastBlinkTime) {
lastBlinkTime = currentMillis;
isBlinkPlaying = true;
for (size_t i = 0; i < frameCount; ++i) {
displayFrame(frames[i]);
delay(happy_frameDelays[i]);
}
}
}
void playHappyAnimation2(const uint8_t* frames[], size_t frameCount) {
static unsigned long lastBlinkTime = 0;
static unsigned long blinkDelay = 0;
static size_t blinkFrameIndex = 0;
unsigned long currentMillis = millis();
// Check if it's time to blink
if (currentMillis - lastBlinkTime) {
lastBlinkTime = currentMillis;
isBlinkPlaying = true;
for (size_t i = 0; i < frameCount; ++i) {
displayFrame(frames[i]);
delay(happy2_frameDelay);
}
}}
void loop() {
unsigned long currentMillis = millis();
Serial.println("Voltage: ");
Serial.println(inputVoltage);
// Check if it's time to check the touch sensor
if (currentMillis - lastTouchCheckTime >= touchCheckInterval) {
lastTouchCheckTime = currentMillis;
// Check if the touch sensor is pressed
if (digitalRead(TouchPin) == HIGH) {
Serial.println("Touch sensor activated");
int melodyChoice = random(3);
switch (melodyChoice) {
case 0:
cute.play(S_HAPPY);
break;
case 1:
cute.play(S_HAPPY_SHORT);
break;
case 2:
cute.play(S_SUPER_HAPPY);
break;
}
// Randomly choose between playing "happy" or "happy2" animation
if (random(2) == 0) {
playHappyAnimation(happy_frames, happy_frames_count);
} else {
playHappyAnimation2(happy2_frames, happy2_frames_count);
}
}
}
// Display the idle frame
displayFrame(idle_frame1);
// Play the blink animation
playBlinkAnimation();
}