Hi all,
I have a little project I'm working on. I want to make loop/switcher for my guitar pedals that switches them in and out individually and allows me to save preset combinations and recall the presets. I'm mocking up a small version now that uses 4 momentary switches, 2 digital inputs that I'm using is as momentary switch inputs, and 4 relay outputs. I have the code below but here is how it all (should) works:
When pin 6 is pulled low the program is in toggle mode. In this mode each each push button toggles a relay on/off.
When pin 6 is high it is in preset mode and I want it to recall a saved configuration of outputs (not quite working yet), of which there are 4 each implemented by one of the momentary switches.
When pin 7 is pulled low I'm trying to capture the current states of the relay outputs as a 4bit word to be drawn from later.
I have 2 methods I'm trying to use for saving the stays, with bit shifts and with bitSet. In the writePrest function I serial print what it supposedly just saved and instead of getting a nice neat "preset 1 = 0110" I get a crazy "preset 1 =preset4 = 11111111111111111111111111111010". Not sure why…. looking for a sanity check.
You can see below that I've written a few functions to try and keep the logic straight.
//
#include <EEPROM.h>
const int mode = 6;
const int save = 7;
const int pb1 = 8;
const int pb2 = 9;
const int pb3 = 10;
const int pb4 = 11;
int modeV = 12;
int saveV = 13;
const int rl1 = 2;
const int rl2 = 3;
const int rl3 = 4;
const int rl4 = 5;
int preset = 0;
int loopState1 = 0;
int loopState2 = 0;
int loopState3 = 0;
int loopState4 = 0;
int preset1 = 0;
int preset2 = 0;
int preset3 = 0;
int preset4 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(mode, INPUT_PULLUP);
pinMode(save, INPUT_PULLUP);
pinMode(pb1, INPUT);
pinMode(pb2, INPUT);
pinMode(pb3, INPUT);
pinMode(pb4, INPUT);
pinMode(rl1, OUTPUT);
pinMode(rl2, OUTPUT);
pinMode(rl3, OUTPUT);
pinMode(rl4, OUTPUT);
pinMode(modeV, OUTPUT);
pinMode(saveV, OUTPUT);
digitalWrite(rl1, LOW);
digitalWrite(rl2, LOW);
digitalWrite(rl3, LOW);
digitalWrite(rl4, LOW);
digitalWrite(modeV, HIGH);
digitalWrite(saveV, HIGH);
//preset =
//loadLoopState ();
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead (mode) == HIGH)
{
presetMode();
Serial.println("PRESET MODE");
}
else
{
toggleMode();
Serial.println("TOGGLE MODE");
}
if (digitalRead (save) == LOW)
{
saveLoopState ();
}
}
//*************************************************
void presetMode ()
{
if (digitalRead (pb1) == HIGH)
{
preset = 1;
loadLoopState ();
writeLoopState ();
}
else if (digitalRead (pb2) == HIGH)
{
preset = 2;
loadLoopState ();
writeLoopState ();
}
else if (digitalRead (pb3) == HIGH)
{
preset = 3;
loadLoopState ();
writeLoopState ();
}
else if (digitalRead (pb4) == HIGH)
{
preset = 4;
loadLoopState ();
writeLoopState ();
}
}
void toggleMode ()
{
if (digitalRead (pb1) == HIGH)
{
loopState1 = ~ loopState1;
writeLoopState ();
while (digitalRead (pb1) == HIGH);
{
delay(1);
}
}
else if (digitalRead (pb2) == HIGH)
{
loopState2 = ~ loopState2;
writeLoopState ();
while (digitalRead (pb2) == HIGH);
{
delay(1);
}
}
else if (digitalRead (pb3) == HIGH)
{
loopState3 = ~ loopState3;
writeLoopState ();
while (digitalRead (pb3) == HIGH);
{
delay(1);
}
}
else if (digitalRead (pb4) == HIGH)
{
loopState4 = ~ loopState4;
writeLoopState ();
while (digitalRead (pb4) == HIGH);
{
delay(1);
}
}
}
void loadLoopState()
{
switch (preset)
{
case 1:
loopState1 = bitRead(preset1,0);
loopState2 = bitRead(preset1,1);
loopState3 = bitRead(preset1,2);
loopState4 = bitRead(preset1,3);
break;
loopState1 = bitRead(preset2,0);
loopState2 = bitRead(preset2,1);
loopState3 = bitRead(preset2,2);
loopState4 = bitRead(preset2,3);
break;
case 3:
loopState1 = bitRead(preset3,0);
loopState2 = bitRead(preset3,1);
loopState3 = bitRead(preset3,2);
loopState4 = bitRead(preset3,3);
break;
case 4:
loopState1 = bitRead(preset4,0);
loopState2 = bitRead(preset4,1);
loopState3 = bitRead(preset4,2);
loopState4 = bitRead(preset4,3);
break;
Serial.print(loopState1);
Serial.print(loopState2);
Serial.print(loopState3);
Serial.print(loopState4);
}
}
void writeLoopState ()
{
digitalWrite(rl1, loopState1);
digitalWrite(rl2, loopState2);
digitalWrite(rl3, loopState3);
digitalWrite(rl4, loopState4);
}
void saveLoopState ()
{
switch (preset)
{
case 1:
preset1 = ((loopState4 << 3) + (loopState3 << 2) + (loopState2 << 1) + loopState1);
Serial.print("preset1 = ");
Serial.println(preset1, BIN);
break;
case 2:
preset2 = ((loopState4 << 3) + (loopState3 << 2) + (loopState2 << 1) + loopState1);
Serial.print("preset2 = ");
Serial.println(preset2, BIN);
break;
case 3:
preset3 = ((loopState4 << 3) + (loopState3 << 2) + (loopState2 << 1) + loopState1);
Serial.print("preset3 = ");
Serial.println(preset3, BIN);
break;
case 4:
preset4 = ((loopState4 << 3) + (loopState3 << 2) + (loopState2 << 1) + loopState1);
Serial.print("preset4 = ");
Serial.println(preset4, BIN);
break;
}
while (digitalRead (save) == LOW);
{
delay(1);
}
}
/*
void saveLoopState ()
{
switch (preset)
{
case 1:
if(loopState1 == 1) {bitSet(preset1,0);}
if(loopState2 == 1) {bitSet(preset1,1);}
if(loopState3 == 1) {bitSet(preset1,2);}
if(loopState4 == 1) {bitSet(preset1,3);}
Serial.print("preset1 = ");
Serial.println(preset1, BIN);
break;
case 2:
if(loopState1 == 1) {bitSet(preset2,0);}
if(loopState2 == 1) {bitSet(preset2,1);}
if(loopState3 == 1) {bitSet(preset2,2);}
if(loopState4 == 1) {bitSet(preset2,3);}
Serial.print("preset2 = ");
Serial.println(preset2, BIN);
break;
case 3:
if(loopState1 == 1) {bitSet(preset3,0);}
if(loopState2 == 1) {bitSet(preset3,1);}
if(loopState3 == 1) {bitSet(preset3,2);}
if(loopState4 == 1) {bitSet(preset3,3);}
Serial.print("preset3 = ");
Serial.println(preset3, BIN);
break;
case 4:
if(loopState1 == 1) {bitSet(preset4,0);}
if(loopState2 == 1) {bitSet(preset4,1);}
if(loopState3 == 1) {bitSet(preset4,2);}
if(loopState4 == 1) {bitSet(preset4,3);}
Serial.print("preset4 = ");
Serial.println(preset4, BIN);
break;
}
while (digitalRead (save) == LOW);
{
delay(1);
}
*/
Once I get this working I'm going to expand it to 8 momentary switches (the foot pedal types) to control 8 loops and 8 presets.
Thanks for any help you can give,
Brian