...is that a word?
Anyway, my dumb noob question is this:
Is it possible to make two things happen (or at least appear to happen) simultaneously if they both involve functions that happen over time intervals that are perceptible on a human scale? I'm trying to build a little box controlled by a Nano that will simultaneously (a) play "Happy Birthday" and (b) scroll a " HAPPY BIRTHDAY" message across a 7-segment 8-digit MAX72XX display.
I've got two sketches working. The first (thank you Mr. Nick Gammon!) scrolls the "Happy Birthday" message:
//http://www.gammon.com.au/forum/?id=11516&reply=5#reply5
#include <SPI.h>
#include <bitBangedSPI.h>
#include <MAX7219.h>
const byte chips = 1;
// 1 chip, bit banged SPI on pins 6, 7, 8
MAX7219 display (chips, 6, 7, 8); // Chips / LOAD / DIN / CLK
const char message [] = "HAPPY BIRTHDAY ";
void setup ()
{
display.begin ();
} // end of setup
unsigned long lastMoved = 0;
unsigned long MOVE_INTERVAL = 150; // mS
unsigned int messageOffset;
void updateDisplay ()
{
const unsigned int messageLength = strlen (message);
// get each byte required
for (byte i = 0; i < (8 * chips); i++)
{
// find the offset in the message array
unsigned int charOffset = messageOffset + i;
// if we have passed the end, go back to the start
if (charOffset >= messageLength)
charOffset -= messageLength;
// send that character
display.sendChar (i, message [charOffset]);
}
// next time show one character on
if (messageOffset++ >= messageLength)
messageOffset = 0;
} // end of updateDisplay
void loop ()
{
// update display if time is up
if (millis () - lastMoved >= MOVE_INTERVAL)
{
updateDisplay ();
lastMoved = millis ();
}
// do other stuff here
} // end of loop
and the second (based on a sketch posted as a link in a thread by Jurs--thanks! ):
#include "musical_notes.h"
int speakerPin = 9; // speaker connected to digital pin 9
void setup()
{
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
}
void loop()
{
happyBirthday();
delay(500);
}
void beep (int speakerPin, float noteFrequency, long noteDuration)
{
int x;
// Convert the frequency to microseconds
float microsecondsPerWave = 1000000/noteFrequency;
// Calculate how many HIGH/LOW cycles there are per millisecond
float millisecondsPerCycle = 1000/(microsecondsPerWave * 2);
// Multiply noteDuration * number or cycles per millisecond
float loopTime = noteDuration * millisecondsPerCycle;
// Play the note for the calculated loopTime.
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(microsecondsPerWave);
digitalWrite(speakerPin,LOW);
delayMicroseconds(microsecondsPerWave);
}
}
void happyBirthday(){
beep(speakerPin, note_Bb4,400); //B b
delay(25);
beep(speakerPin, note_Bb4,200); //B b
delay(25);
beep(speakerPin, note_C5,600); //C
delay(25);
beep(speakerPin, note_Bb4,600); //B b
delay(100);
beep(speakerPin, note_Eb5,600); //E b
delay(100);
beep(speakerPin, note_D5,900); //D
delay(400);
beep(speakerPin, note_Bb4,400); //B b
delay(25);
beep(speakerPin, note_Bb4,200); //B b
delay(25);
beep(speakerPin, note_C5,600); //C
delay(25);
beep(speakerPin, note_Bb4,600); //B b
delay(100);
beep(speakerPin, note_F5,600); //F
delay(100);
beep(speakerPin, note_Eb5,900); //E b
delay(400);
beep(speakerPin, note_Bb4,400); //B b
delay(25);
beep(speakerPin, note_Bb4,200); //B b
delay(25);
beep(speakerPin, note_Bb5,600); //B b up an octave
delay(25);
beep(speakerPin, note_G5,600); //G
delay(100);
beep(speakerPin, note_Eb5,600); //Eb
delay(100);
beep(speakerPin, note_D5,800); //D
delay(100);
beep(speakerPin, note_C5,1000); //C
delay(400);
beep(speakerPin, note_Ab5,400); //A b
delay(25);
beep(speakerPin, note_Ab5,200); //A b
delay(25);
beep(speakerPin, note_G5,600); //G
delay(100);
beep(speakerPin, note_Eb5,600); //E b
delay(100);
beep(speakerPin, note_F5,600); //F
delay(100);
beep(speakerPin, note_Eb5,1000); //E b
delay(2500);
}
plays the song.
I haven't figured out how to merge the two and I wonder if it's even possible to do without disrupting the rhythm of the music or making the scrolling seem jerky. Have any of you more experienced coders got any ideas you'd be willing to share?
Thanks!