Thank you for pointing that out about the "ledmode", indeed it was a typo of "ledMode". I enabled the delay function prior to following up the code and led behavior, there were 2 segments of mode change led blink indicator, the first section is additional "undesired" behavior that suddenly appeared, while second is actually the desired one".
The idea is that to press the momentary/tactile button once to change its mode and blink like you said.
I used millis since the delay isn't suitable for this mode change operation where delay will make the arduino "miss" other events. I believe this is called "blink without delay". Useful for led pulsing or fading led effect instead of waiting for the perfect timing to change mode.
I am not familiar with that "1 second to enter learning mode" you stated.
The code actually working, while I'm going to post again the full code here after doing a bit of change to that for loop
// The code accepts 3 input and interchangeable mode by button press
// left led is at pin 0, right led at pin 1
// left channel pin 5, right channel pin 4
// mic or sound sensor pin 2
// mode button pin 3 and ground
int leftLed = 0, rightLed = 1; // Led output pins
int leftCh = A0, rightCh = A2; //audio L R input
int left, right, i, leftLarge, rightLarge, leftMap, rightMap; //led control variables
int buttonState = 0, buttonPin = 3, lastButtonState = 1, buttonPushCounter = 0; //mode change
int micPin = A1, micDigipin = 2, mic, micMap; //microphone or voice reactive
int ledMode = 0, modes = 5;
int micDigi;
int potmeter = 0;
//using mili instead of delay variable
#define UP 0
#define DOWN 1
const int minPWM = 0;
const int maxPWM = 255;
//state variable
byte fadeDirection = UP;
//global fade
int fadeValue = 0;
int revFade = maxPWM;
//fade smoothing
byte fadeIncrement = 5;
//timing variable
unsigned long previousFadeMillis;
//fade interval
int interval = 3000;
int fadeInterval = interval/(256/fadeIncrement);
//end of milli variables
void setup()
{
ledMode = 0;
pinMode(leftLed, OUTPUT);
pinMode(rightLed, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
//pinMode(micPin,INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if (leftLarge < left)
leftLarge = left;
if (rightLarge < right)
rightLarge = right;
switch (ledMode)
{
case 0:
ledOn();
break;
case 1:
ledPulse1(currentMillis);
break;
case 2:
ledPulse2(currentMillis);
break;
case 3:
ledVU();
break;
case 4:
ledMicd();
break;
}
modeChange();
}
void modeChange()
{
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
ledMode = buttonPushCounter % modes;
} else {
// if the current state is LOW then the button
// wend from on to off:
}
// Delay a little bit to avoid bouncing
delay(500);
for(int i = 0; i<=ledMode;i++)
{
digitalWrite(leftLed , LOW);
digitalWrite(rightLed , LOW);
delay (250);
digitalWrite(leftLed , HIGH);
digitalWrite(rightLed , LOW);
delay (250);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
void ledOn()
{
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, HIGH);
}
void ledPulse1(unsigned long thisMillis)
{
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
analogWrite(leftLed, fadeValue);
analogWrite(rightLed, fadeValue);
// reset millis for the next iteration (fade timer only)
previousFadeMillis = thisMillis;
}
}
void ledPulse2(unsigned long thisMillis)
{
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
revFade = revFade - fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
revFade = minPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
revFade = revFade + fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
revFade = maxPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
analogWrite(leftLed, fadeValue);
analogWrite(rightLed, revFade);
// reset millis for the next iteration (fade timer only)
previousFadeMillis = thisMillis;
}
}
void ledVU()
{
left = analogRead(leftCh);
right = analogRead(rightCh);
leftMap = map(left, 0, 95, 0, 255);
rightMap = map(right, 0, 95, 0, 255);
analogWrite(leftLed, leftMap);
analogWrite(rightLed, rightMap);
delay(50);
}
void ledMica()
{
mic = analogRead(micPin);
micMap = map(mic, 0, 1023, 0, 255);
if(micMap < 32)
micMap = 0;
analogWrite(leftLed, micMap);
analogWrite(rightLed, micMap);
}
void ledMicd()
{
micDigi = digitalRead (micDigipin);
digitalWrite(leftLed, !micDigi);
digitalWrite(rightLed, !micDigi);
}
void ledOff()
{
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, LOW);
}