Problem with managing button/logic states.
so i'm trying create midi controller guitar pedal have.
on physical pedal (a line6 m9) each button serves 2 functions.
button1 record/overdub
button2 play/stop
if nothing happening, hitting button 1 go record mode , create loop. if press button 1 again, goes play mode. if press button 2 instead, goes play mode. if press button 2 while in play mode goes stops. if while playing press button1, goes overdub.
that's sort of basic operating principles.
now, in order manage interdependent states, i've created mess of code, , doesn't work. i'm guessing create impossible states, , code can't out of it.
after wrestling debouncing stuff, went button library, , far that's been working fine. don't know how keep track of what's going on, error proof.
also, smaller detail. there benefit (other tidiness) nesting if loops? in code, first 3 bits contain "((button1.uniquepress())". better have if loop starts that, uses elses go next criteria?
here's code.
on physical pedal (a line6 m9) each button serves 2 functions.
button1 record/overdub
button2 play/stop
if nothing happening, hitting button 1 go record mode , create loop. if press button 1 again, goes play mode. if press button 2 instead, goes play mode. if press button 2 while in play mode goes stops. if while playing press button1, goes overdub.
that's sort of basic operating principles.
now, in order manage interdependent states, i've created mess of code, , doesn't work. i'm guessing create impossible states, , code can't out of it.
after wrestling debouncing stuff, went button library, , far that's been working fine. don't know how keep track of what's going on, error proof.
also, smaller detail. there benefit (other tidiness) nesting if loops? in code, first 3 bits contain "((button1.uniquepress())". better have if loop starts that, uses elses go next criteria?
here's code.
code: [select]
#include <button.h>
#define led 13
button button1 = button(8,pulldown);
button button2 = button(9,pulldown);
button button3 = button(10,pulldown);
int state1 = 0; // state of notrecording(0),record(1),overdub(2)
int state2 = 0; // state of stop(0),play(1)
void setup() {
pinmode(13,output);
serial.begin(31250);
}
void loop(){
// ======= record new loop
if ((button1.uniquepress()) && (state1 == 0) && (state2 == 0)) {
state1 = 1;
serial.print(0xb0,byte);
serial.print(50,byte);
serial.print(127,byte);
digitalwrite(led,high);
delay(5);
digitalwrite(led,low);
}
// ======= overdub (from record state)
if ((button1.uniquepress()) && (state1 == 1) && (state2 == 0)) {
state1 = 2;
state2 = 1;
serial.print(0xb0,byte);
serial.print(50,byte);
serial.print(0,byte);
digitalwrite(led,high);
delay(5);
digitalwrite(led,low);
}
// ======= overdub (to play state)
if ((button1.uniquepress()) && (state1 == 2) && (state2 == 1)) {
state1 = 0;
serial.print(0xb0,byte);
serial.print(50,byte);
serial.print(0,byte);
digitalwrite(led,high);
delay(5);
digitalwrite(led,low);
}
// ======= overdub (from play state)
if ((button1.ispressed()) && (state1 == 0) && (state2 == 1)) {
state1 = 2;
serial.print(0xb0,byte);
serial.print(50,byte);
serial.print(0,byte);
digitalwrite(led,high);
delay(5);
digitalwrite(led,low);
}
// ======= play
if ((button2.uniquepress()) && (state2 == 0)) {
state2 = 1;
serial.print(0xb0,byte);
serial.print(28,byte);
serial.print(127,byte);
digitalwrite(led,high);
delay(5);
digitalwrite(led,low);
}
// ======= stop
if ((button2.uniquepress()) && (state2 == 1)) {
state2 = 0;
serial.print(0xb0,byte);
serial.print(28,byte);
serial.print(0,byte);
digitalwrite(led,high);
delay(5);
digitalwrite(led,low);
}
// ======= play once
if(button3.uniquepress()){
serial.print(0xb0,byte);
serial.print(80,byte);
serial.print(127,byte);
digitalwrite(led,high);
delay(5);
digitalwrite(led,low);
}
}
i thought using built in library "statechanged" since button logic intertwined, didn't know if work right off bat.
like if press button1, , go record mode, press button2, enter play mode, button2 again go stop mode, when press button1 again, need go record mode again, not overdub mode, next state button
like if press button1, , go record mode, press button2, enter play mode, button2 again go stop mode, when press button1 again, need go record mode again, not overdub mode, next state button
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Problem with managing button/logic states.
arduino
Comments
Post a Comment