bump on this..
I didnt want to start a whole new thread..... but took another stab at implementing a simple WaveHC sketch and a LedControl sketch together..
I believe I have the FSM working appropriately...
when I look at serial monitor.. I 'see' the correct things being output/printed..
but the MAX72XX chip does 'nothing'.. (when integrated into the WaveHC sketch..)... where as if I change nothing hardware wise..and and upload a simple MAX722/LedControl sketch (using same pins)... it works fine?
at this point.. Im sorta left believing that there is something INSIDE one/both of these libraries that causes a conflict? (but I think thats a bit above me currently to track down? anyone have some experience with this? been down this road before?.... I'll do some searching for WaveHC & MAX chip projcts.. see if anyone has got them working together.. and how?
anyways.
merged code that plays audio.. does NOTHING when MAX chip is supposed to 'breathe' animation (simple intensity changes..up/down)
Code: Select all
//FSM_attempt_v1.0
//created by: xl97 - [email protected]
/*
This sketch is an attempt to play .wav files usnig the WaveHC
library utilizing an FSM (Finite State Machine) approach
*/
//import default/needed libraries
#include "LedControl.h"
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#define S_IDLE 1
#define S_LEDON 2
#define S_WAITON 3
#define S_LEDOFF 4
#define S_WAITOFF 5
//custom defined variables
const int loopOne = 18;
const int breakLoop = 16;
boolean playLoop = 0;
int fadeValue = 0; //MAX intensity value variable
int animationDelay = 50;
//FSM init vars
static int state = S_IDLE; // initial state is 1, the "idle" state.
static unsigned long ts; // To store the "current" time in for delays.
//led control
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
Arduino pin 3 is connected to the DataIn (MAX pin:1)
Arduino pin 6 is connected to the CLK (MAX pin:13)
Arduino pin 5 is connected to LOAD (MAX pin:12)
We have only a single MAX72XX.
*/
//LedControl lc=LedControl(12,11,10,1);
//LedControl lc=LedControl(3, 6, 5, 1);
LedControl lc=LedControl(14, 15, 19, 1);
//project variables
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
#define DEBOUNCE 5 // button debouncer
//make button array for easy looping/checking
int buttons[] = {loopOne, breakLoop};
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'pressed' (the current state)
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
// this handy function will return the number of bytes currently free in RAM, great for debugging!
int freeRam(void)
{
extern int __bss_end;
extern int *__brkval;
int free_memory;
if((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__bss_end);
}
else {
free_memory = ((int)&free_memory) - ((int)__brkval);
}
return free_memory;
}
void sdErrorCheck(void)
{
if (!card.errorCode())
{
return;
}
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
//------------------[end init code]---------------------//
void setup() {
//MAX 72XX chip set-up
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,15);
/* and clear the display */
lc.clearDisplay(0);
byte i;
// Set up serial port
Serial.begin(9600);
putstring_nl("WaveHC with ");
Serial.print(NUMBUTTONS, DEC);
putstring_nl("buttons");
putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
Serial.println(freeRam());
// Set-up pins not in button array
// pin13 LED
pinMode(13, OUTPUT);
// Make input & enable pull-up resistors on switch pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons[i], INPUT);
digitalWrite(buttons[i], HIGH);
}
// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1<<TOIE2;
}
//--------------------[end set-up code]-----------------------//
//Add function for timer to execute.
SIGNAL(TIMER2_OVF_vect) {
check_switches();
}
//check switches function (watched by timer repeatedly)
void check_switches()
{
//create vars to hold the states of the current button being checked
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
byte index;
//run through each button in the array to check if it is being pressed
for (index = 0; index < NUMBUTTONS; index++) {
currentstate[index] = digitalRead(buttons[index]); // read each button/store its status
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// just pressed
//putstring("Button Pressed: ");
//Serial.println(index,DEC);
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
// just released
justreleased[index] = 1;
}
pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed //commenting out stops file while pressed
}
//Serial.println(pressed[index], DEC);
//update previous status to the current status
previousstate[index] = currentstate[index]; // keep a running tally of the buttons
}
//sound loop watch dog
if(playLoop == 1 && !wave.isplaying){
f.rewind();
wave.create(f);
wave.play();
//loopFile("hum2.wav");
}
}
void loop() {
//Check loop status and restart if required
//if(playLoop == 1 && !wave.isplaying){
/*
if(!wave.isplaying){
f.rewind();
wave.create(f);
//loopFile("hum3.wav");
//putstring_nl(".");
wave.play();
}
*/
if (justpressed[0]) {
//reset button var/value to show not pressed
justpressed[0] = 0;
//start led fade
state = S_LEDON;
if(!wave.isplaying){
playLoop = 1; //true
loopFile("breath.wav");
//playfileLoop("hum2.wav");
//playfileForever("hum2.wav");
}
}
if(justpressed[1]){
justpressed[1] = 0;
playLoop = 0; //true
//conditional check while playing to see if audio should stop.
putstring_nl("MANUAL LOOP BREAK.....");
f.close();
wave.stop();
//stop led fade
state = S_IDLE;
}
//C&P from FSM example...not implemented yet
//putstring("STATE STATE: ");
//Serial.println(state);
switch(state)
{
case S_IDLE:
// We don't need to do anything here, waiting for a forced state change.
break;
case S_LEDON:
putstring_nl("increase.........");
lc.setIntensity(0,fadeValue);
//analogWrite(ledPin, fadeValue);
//digitalWrite(13,HIGH); // Turn on the light
ts = millis(); // Remember the current time
state = S_WAITON; // Move to the next state
break;
case S_WAITON:
// If one second has passed, then move on to the next state.
if(millis() > ts + animationDelay)
{
if(fadeValue < 15){
fadeValue++;
state = S_LEDON;
}else{
state = S_LEDOFF;
}
}
break;
case S_LEDOFF:
putstring_nl("decrease.........");
lc.setIntensity(0,fadeValue);
//analogWrite(ledPin, fadeValue);
//digitalWrite(13,LOW); // Turn off the light
ts = millis(); // Remember the current time
state = S_WAITOFF;
break;
case S_WAITOFF:
// If one second has passed, then go back to state 2.
if(millis() > ts + animationDelay)
{
if(fadeValue > 0){
fadeValue--;
state = S_LEDOFF;
}else{
state = S_LEDON;
}
}
break;
default:
state = S_IDLE;
break;
}
}
void loopFile(char *name) {
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
return;
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
return;
}
wave.play();
}
and here is a simple MAX sketch that DOES work just fine.. no change in hardware.. just write different sketch (was testing the max and pins to see if a problem?)
Code: Select all
//We always have to include the library
#include "LedControl.h"
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
Arduino pin 12 is connected to the DataIn (MAX pin:1)
Arduino pin 11 is connected to the CLK (MAX pin:13)
Arduino pin 10 is connected to LOAD (MAX pin:12)
We have only a single MAX72XX.
*/
//LedControl lc=LedControl(12,11,10,1);
//LedControl lc=LedControl(3, 6, 5,1);
LedControl lc=LedControl(14, 15, 19, 1);
/* we always wait a bit between updates of the display */
unsigned long delaytime=50;
int intensityValue = 0;
boolean loopFade = 1;
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,15);
/* and clear the display */
lc.clearDisplay(0);
//turn on all leds
/*
for(int row=0;row<8;row++) {
lc.setRow(0,row,B11111111);
}
*/
//intensityFadeIn();
//writeArduinoOnMatrix();
//rows();
//columns();
single();
intensityLoop();
}
/*
This method will display the characters for the
word "Arduino" one after the other on the matrix.
(you need at least 5x7 leds to see the whole chars)
*/
void writeArduinoOnMatrix() {
/* here is the data for the characters */
byte a[5]={
B01111110,B10001000,B10001000,B10001000,B01111110 };
byte r[5]={
B00111110,B00010000,B00100000,B00100000,B00010000 };
byte d[5]={
B00011100,B00100010,B00100010,B00010010,B11111110 };
byte u[5]={
B00111100,B00000010,B00000010,B00000100,B00111110 };
byte i[5]={
B00000000,B00100010,B10111110,B00000010,B00000000 };
byte n[5]={
B00111110,B00010000,B00100000,B00100000,B00011110 };
byte o[5]={
B00011100,B00100010,B00100010,B00100010,B00011100 };
/* now display them one by one with a small delay */
lc.setRow(0,0,a[0]);
lc.setRow(0,1,a[1]);
lc.setRow(0,2,a[2]);
lc.setRow(0,3,a[3]);
lc.setRow(0,4,a[4]);
delay(delaytime);
lc.setRow(0,0,r[0]);
lc.setRow(0,1,r[1]);
lc.setRow(0,2,r[2]);
lc.setRow(0,3,r[3]);
lc.setRow(0,4,r[4]);
delay(delaytime);
lc.setRow(0,0,d[0]);
lc.setRow(0,1,d[1]);
lc.setRow(0,2,d[2]);
lc.setRow(0,3,d[3]);
lc.setRow(0,4,d[4]);
delay(delaytime);
lc.setRow(0,0,u[0]);
lc.setRow(0,1,u[1]);
lc.setRow(0,2,u[2]);
lc.setRow(0,3,u[3]);
lc.setRow(0,4,u[4]);
delay(delaytime);
lc.setRow(0,0,i[0]);
lc.setRow(0,1,i[1]);
lc.setRow(0,2,i[2]);
lc.setRow(0,3,i[3]);
lc.setRow(0,4,i[4]);
delay(delaytime);
lc.setRow(0,0,n[0]);
lc.setRow(0,1,n[1]);
lc.setRow(0,2,n[2]);
lc.setRow(0,3,n[3]);
lc.setRow(0,4,n[4]);
delay(delaytime);
lc.setRow(0,0,o[0]);
lc.setRow(0,1,o[1]);
lc.setRow(0,2,o[2]);
lc.setRow(0,3,o[3]);
lc.setRow(0,4,o[4]);
delay(delaytime);
lc.setRow(0,0,0);
lc.setRow(0,1,0);
lc.setRow(0,2,0);
lc.setRow(0,3,0);
lc.setRow(0,4,0);
delay(delaytime);
}
/*
This function lights up a some Leds in a row.
The pattern will be repeated on every row.
The pattern will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
void rows() {
for(int row=0;row<8;row++) {
delay(delaytime);
lc.setRow(0,row,B10100000);
delay(delaytime);
lc.setRow(0,row,(byte)0);
for(int i=0;i<row;i++) {
delay(delaytime);
lc.setRow(0,row,B10100000);
delay(delaytime);
lc.setRow(0,row,(byte)0);
}
}
}
void intensityLoop(){
while(loopFade == 1){
//single();
//for (int n=0; n<5; n++){
for (int z=0; z<16; z++){
lc.setIntensity(0,z);
delay(50);
}
for (int z=15; z>-1; --z){
lc.setIntensity(0,z);
delay(50);
}
//}
}
}
void intensityFadeIn(){
byte i;
while(intensityValue <= 15){
lc.setIntensity(0,intensityValue);
delay(delaytime);
intensityValue++;
}
intensityFadeOut();
}
void intensityFadeOut(){
byte i;
while(intensityValue >= 15){
lc.setIntensity(0,intensityValue);
delay(delaytime);
intensityValue--;
}
intensityFadeIn();
}
/*
This function lights up a some Leds in a column.
The pattern will be repeated on every column.
The pattern will blink along with the column-number.
column number 4 (index==3) will blink 4 times etc.
*/
void columns() {
for(int col=0;col<8;col++) {
delay(delaytime);
lc.setColumn(0,col,B10100000);
delay(delaytime);
lc.setColumn(0,col,(byte)0);
for(int i=0;i<col;i++) {
delay(delaytime);
lc.setColumn(0,col,B10100000);
delay(delaytime);
lc.setColumn(0,col,(byte)0);
}
}
}
/*
This function will light up every Led on the matrix.
The led will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
void single() {
for(int row=0;row<8;row++) {
for(int col=0;col<8;col++) {
delay(delaytime);
lc.setLed(0,row,col,true);
delay(delaytime);
/*
for(int i=0;i<col;i++) {
lc.setLed(0,row,col,false);
delay(delaytime);
lc.setLed(0,row,col,true);
delay(delaytime);
}
*/
}
}
}
void loop() {
//writeArduinoOnMatrix();
//rows();
//columns();
//single();
}
Again.. if anyone knows or can point me to what is wrong? or what "I'm" doing wrong...
I appreciate it!.. thanks!!