Greetings all,
Ive been trolling here for a month or so reading and learning lots.
Ive started a little project... The idea is a menu system to control things via 16x2 lcd and 6 keys.
(Please excuse the code....Its a personal test bed.)
Currently I have a menu setup (one item) that when selected is supposed to call a function that plays a melody.....This works fine.
My problem is that I need the melody to play in the background and allow the menu system to resume so that other functions could be selected while the melody plays... Ie, stop the melody or select another to play.... whatever!
I'm stuck, ive tried several solutions and all leave me with the menu "paused" on the last option while the melody is playing and only returning once the melody is finished.
Could some "boffin" show me how to achieve what I'm wanting to do before I pull all my hair out - PLEASE!
Note: I understand that delay is a blocking function, I think that my biggest problem is getting this sequence of notes to play without using a delay....
Many thanks.
Here is the code I'm working with currently:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <buttons.h>
#include <MENWIZ.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <toneAC.h>
int melody[] = { 2, 4, 6, 8, 10, 0, 24, 29, 10 };
int noteDurations[] = { 4, 8, 8, 1, 4, 4, 4, 4, 1 };
int timer = 0;
long previousMillis = 0;
long interval = 20;
unsigned long timestamp = 0;
// DEFINE ARDUINO PINS FOR THE NAVIGATION BUTTONS
#define UP_BOTTON_PIN A0
#define DOWN_BOTTON_PIN A1
#define LEFT_BOTTON_PIN A2
#define RIGHT_BOTTON_PIN A3
#define CONFIRM_BOTTON_PIN A4
#define ESCAPE_BOTTON_PIN A5
menwiz tree;
// create lcd obj using LiquidCrystal lib
//LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int list,sp=110;
void setup(){
_menu *r,*s1,*s2,*s3,*s4,*s5;
Serial.begin(19200);
tree.begin(&lcd,16,2); //declare lcd object and screen size to menwiz lib
//create the menu tree
r=tree.addMenu(MW_ROOT,NULL,F("Root"));
s1=tree.addMenu(MW_VAR,r, F("Level 1"));
s1->addVar(MW_ACTION,beep1);
tree.navButtons(UP_BOTTON_PIN,DOWN_BOTTON_PIN,LEFT_BOTTON_PIN,RIGHT_BOTTON_PIN,ESCAPE_BOTTON_PIN,CONFIRM_BOTTON_PIN);
}
void loop(){
if(timer == 1)
{
int buttonState = 0;
buttonState = digitalRead(A5);
if (buttonState == LOW)
{
//lcd.clear();
//lcd.setCursor(0,0);
//lcd.print("ZZZZZZZZZZZZZZ");
//timer = 0;
}
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("TIMER: ");
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
else
{
tree.draw();
}
}
void beep1(){
for (int thisNote = 0; thisNote < 12; thisNote++) {
int noteDuration = 4000/noteDurations[thisNote];
toneAC(melody[thisNote], 10, noteDuration, true); // Play thisNote at full volume for noteDuration in the background.
delay(noteDuration);
}
}