saving button presses to eeprom
i had button recorder working until tried saving arrays eeprom. wont enter record mode. suggestions/help?
code: [select]
#include <avr/eeprom.h>
byte trigbtn = 1; // assign pin button input record sequence.
byte recbtn = 2; // assign pin button push , hold recording.
byte pin_d5 = 4; // assign pin d5 pin digital output.
boolean inrecordmode = false; // create boolean variable hold current state of record mode.
boolean wasinrecordmode = false; // create boolean variable hold previous state of record mode.
// buffers record state/duration combinations
int maxsamples = 100; // maximum number of samples in bytes can stored.
boolean states[100]; // create states array record number of on , off states.
int durations[100]; // create durations array , can have many duration times.
int idxplayback = 0; // initialize index playback array 0
int idxrecord = 0; // initialize index record array 1
int samplelength = 50; // 50 ms
void setup() {
pinmode(recbtn, input); // setup mode button input
pinmode(trigbtn, input); // setup input button input
pinmode(pin_d5, output); // setup pin d5 pin_d5 output
}
void loop() {
inrecordmode = digitalread(recbtn); // read mode button
if(inrecordmode == true) { // test if mode button pressed in record mode
if(!wasinrecordmode) { // if button not pressed in record mode, reset arrays , index playback
// reset record buffers
memset(states, 0, sizeof(states)); // set size of states array
memset(durations, 0, sizeof(durations)); // set size of durations array
idxrecord = 0; // reset record idx make playback start point obvious
}
if(inrecordmode == false) {
eeprom_write_block((void *)0x00, states, sizeof(states));
eeprom_write_block((void *)0x20, durations, sizeof(durations));
} else {
recordloop(); // perform recording loop function
}
} else { // or else
playbackloop(); // perform playback loop function
}
wasinrecordmode = inrecordmode; // record prev state next iteration know whether reset record array index
}
void recordloop() {
boolean state = digitalread(trigbtn); // read state of input sequence recorder button.
digitalwrite(pin_d5, state); // change state of output input give feedback person recording loop
if(states[idxrecord] == state) {
// if state not changed, add duration of current state
durations[idxrecord] += samplelength;
} else {
// if state changed, go next index (idx) , set default duration of 1ms
idxrecord++;
if(idxrecord == maxsamples) { idxrecord = 0; } // reset idx if max array size reached
states[idxrecord] = state; // save state states array index
durations[idxrecord] = samplelength; // save duration of sample durations array index
}
delay(samplelength); // slow loop time constant can reproduce timelyness of recording
}
void playbackloop() {
eeprom_read_block(states, (void *)0x00, sizeof(states));
eeprom_read_block(durations, (void *)0x20, sizeof(durations));
digitalwrite(pin_d5, states[idxplayback]); // playback output pin_d5
delay(durations[idxplayback]); // leave output pin in state duration
idxplayback++; // increment playback index can play next value
if(idxplayback == maxsamples) { idxplayback=0; } // repeat recorded loop when reach maximum number of samples
}
no real odd
also, why use 'low level' library , not higher level eeprom library?
code: [select]
if (inrecordmode == true) { // test if mode button pressed in record mode
if (!wasinrecordmode) { // if button not pressed in record mode, reset arrays , index playback
...
...
}
if (inrecordmode == false) {
// think ever here <------------------------------------
...
...
} else {
recordloop(); // perform recording loop function
}
} else { // or else
playbackloop(); // perform playback loop function
}
also, why use 'low level' library , not higher level eeprom library?
Arduino Forum > Using Arduino > Programming Questions > saving button presses to eeprom
arduino
Comments
Post a Comment