Hi all!
i've recently got back into playing with the arduino after a very long time. I had a motorized fader laying around and felt like playing with it a bit.
the code is a bit crude and not done/optimized by any means but ive ran into something that i cant seem to figure out.
under the section marked as "ch recall settings" i have a few if statements that check the readout of analog in 0 (called fader) and compares it to a previously stored integer that was updated when a new "channel" is selected of which i currently have 3.
my problem is that the "ch recall settings" if statement only works once, which is when the code is first started and channel 2 is selected which is initialized as analogue read value 800.
the fader moves to position (aproximately for now) and then stops moving, updates the fader restore value to false so it does not run the code constantly and all is well.
however if i then switch to another channel, it correctly updates all values, every integer is correct but it does not start the movement of the fader again.
does anyone see any "obvious" thing that im missing right now?
ive been confused for long enough
id like to hear it!
code provided below
////////////////////////////////////// All Input / Output Pins (except A0) >
const int buttonPin[3] = {2,3,4};
const int ledPin[3] = {5,6,7};
const int fUp = 8;
const int fDown = 9;
////////////////////////////////////// integers for ch recall memory >
int selectedCh = 0;
int prevCh = 1;
int muteCh[3] = {0,0,0};
boolean faderRestore[3] = {false,false,false};
int faderPos[3] = {3,800,3};
////////////////////////////////////// integers for ch interactions >
int pushState[3] = {0,0,0};
int pushStateNext[3] {0,0,0};
int pushStatePrev[3] {0,0,0};
int ledState = LOW;
int pushPrev = LOW;
int faderPreMove = 0;
int faderReturn = 0;
////////////////////////////////////// Global debounce/Timer >
unsigned long globalDebounce = 20;
unsigned long debounceTimer[3] = {27,28,29};
unsigned long globalBlink = 150;
unsigned long ledBlink[3] = {24,25,26};
//////////////////////////////////////
void setup() {
for (int i=0; i<3; i++) {
pinMode(buttonPin[i], INPUT);
}
for (int i=0; i<3; i++) {
pinMode(ledPin[i], OUTPUT);
}
pinMode(fUp,OUTPUT);
pinMode(fDown,OUTPUT);
prevCh = selectedCh;
analogWrite(ledPin[0],50);
delay(200);
digitalWrite(ledPin[0],LOW);
digitalWrite(fDown,LOW);
}
void loop() {
////////////////////////////////////// Rename Integers for live readings
int fader = analogRead(A0);
int prevPress = digitalRead(buttonPin[0]);
int nextPress = digitalRead(buttonPin[1]);
int mutePress = digitalRead(buttonPin[2]);
fader = map(fader,0,1022,1,1021); // remap for les wonky extreme readings (needs buffering)
//////////////////////////////////////Led Button Interactions
if ((millis() - ledBlink[0]) < globalBlink) {
analogWrite(ledPin[0],20);
} else {
digitalWrite(ledPin[0],LOW);
}
if ((millis() - ledBlink[1]) < globalBlink) {
analogWrite(ledPin[1],40);
} else {
digitalWrite(ledPin[1],LOW);
}if ((millis() - ledBlink[2]) < globalBlink) {
analogWrite(ledPin[2],30);
} else {
digitalWrite(ledPin[2],LOW);
}
////////////////////////////////////// Ch recall Settings
if (faderRestore[selectedCh] == true && fader < faderPos[selectedCh] || fader > faderPos[selectedCh]) {
if (fader < faderPos[selectedCh]) {
digitalWrite(fUp,HIGH);
if (fader == faderPos[selectedCh]) {
digitalWrite(fUp,LOW);
faderRestore[selectedCh] = false;
}
}
if (faderRestore[selectedCh] == true && fader > faderPos[selectedCh]) {
digitalWrite(fDown,HIGH); if (faderRestore[selectedCh] == true && fader == faderPos[selectedCh]) {
digitalWrite(fDown,LOW);
faderRestore[selectedCh] = false;
}
}
}
////////////////////////////////////// Ch Next
if (nextPress != pushStateNext[selectedCh]) {
debounceTimer[selectedCh] = millis();
}
if ((millis() - debounceTimer[selectedCh]) > globalDebounce) {
if (nextPress != pushState[selectedCh]) {
pushState[selectedCh] = nextPress;
if (pushState[selectedCh] == HIGH) {
if (selectedCh <= 1) {
prevCh = selectedCh;
faderPos[selectedCh] = fader;
selectedCh++;
faderRestore[selectedCh] = true;
pushStateNext[prevCh] = nextPress;
ledBlink[1] = millis();
delay(100); ///////////////////////////////////// Needs Fixing (to prevent pushstate issues)
}
}
}
}
pushStateNext[selectedCh] = nextPress;
////////////////////////////////////// Ch Prev
if (prevPress != pushStatePrev[selectedCh]) {
debounceTimer[selectedCh] = millis();
}
if ((millis() - debounceTimer[selectedCh]) > globalDebounce) {
if (prevPress != pushState[selectedCh]) {
pushState[selectedCh] = prevPress;
if (pushState[selectedCh] == HIGH) {
if (selectedCh >= 1) {
prevCh = selectedCh;
faderPos[selectedCh] = fader;
selectedCh--;
faderRestore[selectedCh] = true;
pushStatePrev[prevCh] = prevPress;
ledBlink[0] = millis();
delay(100); ///////////////////////////////////// Needs Fixing (to prevent pushstate issues)
}
}
}
}
pushStatePrev[selectedCh] = prevPress;
////////////////////////////////////// Troubleshooting SERIAL BUS
//Serial.print(" pushstate ");
//Serial.print(pushStatePrev[1]);
Serial.print(" FaderRestore ");
Serial.print(faderRestore[2]);
Serial.print(" FaderPosOld ");
Serial.println(faderPos[2]);
//Serial.print(" Fader ");
//Serial.print(fader);
//Serial.print(" ch ");
//Serial.println(selectedCh);
}