Mode Change Indicator using Led

Hi, I wanted to add another feature to my project and that is the mode indicator. My problem was that I have about 5 modes and they should blink from 1 to 5 times. But they actually blink at odd numbers starting at 3,5,7,9,11.

Here is the code snippet to it. I used button for changing the mode

int modes = 5, buttonState = 0, lastButtonState = 1, buttonPushCounter = 0;
void loop()
{
//in the main loop function
switch (arduinomode)
{
  case 0:
  modeone();
  break;
  case 1:
  modetwo();
  break;
  case 2:
  modethree();
  break;
  case 3:
  modefour();
  break;
  case 4:
  modefive();
  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++;
      arduinomode= buttonPushCounter % modes;
    } else {
      // if the current state is LOW then the button
      // wend from on to off:

    }
    // Delay a little bit to avoid bouncing
    for(int i = 0; i<=arduinomode+1;i++)
    {
      digitalWrite(led, LOW);
      delay (250);
      digitalWrite(led, HIGH);
      delay (250);
    }
    //delay(500);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

}

I appreciate any helps in this. I actually using chinese digispark rev 1 or model A for this.

My problem was that I have about 5 modes and they should blink from 1 to 5 times.

How does a mode blink? LEDs can blink. The mode defines how many times blinking should happen.

switch (ledMode)

We can not see where ledMode is defined, or how it is defined, or how it is valued.

A value of 0 triggering a call to modeone() does NOT make sense.

modeChange() does NOT change the value of ledMode.

You have NOT posted the modeone(), modetwo(), etc. functions.

YOU can, therefore, fix your problem yourself.

they are valued with int in modechange() function. modeone() to modefive() is a void type function not relating to state of the arduinomode variable

// 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
    for(int i = 0; i<=ledmode+1;i++)
    {
      digitalWrite(leftLed , LOW);
      digitalWrite(rightLed , LOW);
      delay (250);
      digitalWrite(leftLed , HIGH);
      digitalWrite(rightLed , LOW);
      delay (250);
    }
    delay(500);
  }
  // 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);
}

@sar

sarf2k4:
....

Please follow guidelines as stated in every section - How to use this forum - please read - Website and Forum - Arduino Forum

The more information you give the better help you can expect,

then i will elaborate nice and long.

i am making sound reactive arduino project, 2 pins from left and right channel of audio pin pin 5 and pin 4, sound sensor at pin 2, button at pin 3, left led at pin 0, right led at pin 1. all of the digispark's pin are used, 6 pins of them.

i used the internal input pullup for button at pin 3, and external resistors for both led each.

pressing the mode change button enable me to change between the modes, i tried to add the for loop in modechange(), the led does blink as what i want it to be. only that it blinks with wrong count such as 3,5,7,9,11 instead of 1,2,3,4,5 blinks.

this good enough?

the led does blink as what i want

Which LED blinks?

Do you suppose the fact that you blink both LEDs in modeChange() has anything to do with later blinking (nanoseconds later) some number of times might appear as though the LEDs were blinking the wrong number of times?

Both LED I mean. I did some initial test 2 days ago with the ledmode values with the modulus operation. from mode 0 to mode 1, it should blink 2 times, after reaching mode 4, it should blink 5 times, then 1 time after going back to mode 0.

But to my surprise it was blinking 6 times. I did press the button multiple times to see if there's any changes to the blink count but they were all incorrect

I don't understand your logic in loop(). Do something, based on the value in ledMode. Then, see of the user wants to do something different.

Shouldn't you see what the user wants to do, and then do it?

Anyway, I'm giving up on trying to help you.

sketch_feb01a.ino: In function 'void modeChange()':
sketch_feb01a:101: error: 'ledmode' was not declared in this scope

Perhaps if you posted the code that was ACTUALLY exhibiting the behavior you claim to be seeing, I might reconsider.

Here is the code =)

// 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
    for(int i = 0; i<=ledmode+1;i++)
    {
      digitalWrite(leftLed , LOW);
      digitalWrite(rightLed , LOW);
      delay (250);
      digitalWrite(leftLed , HIGH);
      digitalWrite(rightLed , LOW);
      delay (250);
    }
    delay(500);
  }
  // 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);
}

Here is the code =)

You are just jacking around wasting my time. That code has exactly the same problem.

Your code is hard to understand. You say that you need to be able to select a mode (from 1 to 5) with a push button. Whenever a mode has been selected, a LED should flash "mode" times (1 to 5) in order to indicate the selected mode, yes? If that is the case, then your "modeChange" makes no sense since it flashes the LED each time a button press/release is registred. You could consider to change the behaviour of your button so that it must be held down for 1 second to enter learning mode. Turn the LED on to indicate learning mode, and wait for 1..5 button presses or until 1 second has elapsed without any presses. Turn LED off to signal learning mode ended. Flash LED "mode" times to indicate the selected mode. Should be very easy to implement and it will work much better.

EDIT: You code look a lot like something that has been cut'n'pasted from here and there without minding how it is actually supposed to work. ledmode has a typo, since C is case-sensitive ledMode and ledmode are not the same. You should at least try to post working code. :slight_smile:

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);
}

Ok, I understand it now. Each time the button is pressed, the mode changes to next mode, I got that wrong initially. Your code for flashing the LED's is wrong, since you are using digitalWrite and not analogWrite:

for(int i = 0; i<=ledMode;i++)
{
  //LED's on
  analogWrite(leftLed , 255);
  analogWrite(rightLed , 255);
  delay (250);

  //LED's off
  analogWrite(leftLed , 0);
  analogWrite(rightLed , 0);
  delay (250);
}

Your code for flashing the LED's is wrong, since you are using digitalWrite and not analogWrite:

No. You are wrong. Turning a pin on or off IS done with digitalWrite().

OP: Suppose that you are pressing the switch for the first time. buttonPushCount gets incremented, and ledMode is assigned a new value. There is no reason to use two variables. You could increment ledMode and then apply the modulo operator to ledMode.

   ledMode++;
   ledMode %= modes;

In any case, ledMode gets set to 1. How many times do you want the LED to blink? How many times does the LED blink?

If you want to blink once when ledMode becomes 1, how many times do you want to blink when ledMode again becomes 0?

When we understand your requirements, we can help you determine what is wrong with your code.

The ledMode will start at 0, meaning going to the next mode, will have the led flash starting at 2, 3, 4, 5, 1 and cycle that flash count.

However, seems like there are 2 segments of the flashing sequence where the first segment is the wrong mode flash count. To be precise, it is like there are 2 for loops being run.

actual mode | first segment | second segment | total
2              1 flash         2 flashes         3 flashes
3              2 flashes       3 flashes         5 flashes
4              3 flashes       4 flashes         7 flashes
5              4 flashes       5 flashes         9 flashes
1              5 flashes       1 flash           6 flashes

And the for loop, I put them after the delay(500)

// 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:
      ledMode++;
      ledMode %= 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)
{
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement; 
      revFade = revFade - fadeIncrement;
      if (fadeValue >= maxPWM) {
        fadeValue = maxPWM;
        revFade = minPWM;
        fadeDirection = DOWN;
      }
    } else {
      fadeValue = fadeValue - fadeIncrement;
      revFade = revFade + fadeIncrement;
      if (fadeValue <= minPWM) {
        fadeValue = minPWM;
        revFade = maxPWM;
        fadeDirection = UP;
      }
    }
    analogWrite(leftLed, fadeValue); 
    analogWrite(rightLed, revFade);
 
    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);
}

Weird enough is that, only left led are flashing instead of both this time

It looks like there are two increments in mode just after each other and this is what causes the problem. I think the issue may be as simple as to move the assignment of "lastButtonState" into the code block which handles the state change, like so:

void modeChange()
{
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {


    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState; //Only save last button state when the state actually changes


    //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:
      ledMode++;
      ledMode %= modes;
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      
      //EDIT: You code is actually reacting with flashes from the LED
      //because you do not exit from here. So try to add:
      return;
    }
    // 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);
    }
  }
}

EDIT: I have rewritten the code to simplify it:

void modeChange()
{
  buttonState = digitalRead(buttonPin);
  if ((buttonState != lastButtonState) && (buttonState == HIGH))
  {
    ledMode++;
    ledMode %= modes;
    //delay(500); //This debounce is not necessary, the flashing LED's will function as debounce
    
    //LED's off
    digitalWrite(leftLed , LOW);
    digitalWrite(rightLed , LOW);

    //Flash LED's
    for(int i = 0; i<=ledMode; i++)
    {
      digitalWrite(leftLed , HIGH);
      digitalWrite(rightLed , HIGH);
      delay (250);
      digitalWrite(leftLed , LOW);
      digitalWrite(rightLed , LOW);
      delay (250);
    }
  }
  lastButtonState = buttonState;
}

Hi, thank you for your solution Danois90, it worked well as desired. I have an off-topic question, I don't want to spam the forum with new threads either.

If you noticed that I put 2 variables of leftLarge and rightLarge, those variable are meant to serve the led brightness depending on the audio channel input. I was wondering if I am able to have a varying max value of left and right channel based on certain interval?

For example if from the line-out volume originally set to 50%, analogread value should be roughly about 512, and leftLarge, rightLarge should also be about 512. If I turn down the line-out volume to 25%, it should be 256 for both leftLarge and rightLarge.

In my case, it seems not to change. I will try to check using other boards first with serial print to see what's going on with these. Here is the latest code I put for the max volume interval

// 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 volInterval = 1000;

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();

switch (ledMode)
{
  case 0:
  ledOn();
  break;
  case 1:
  ledPulse1(currentMillis);
  break;
  case 2:
  ledPulse2(currentMillis);
  break;
  case 3:
  ledVU();
  maxVol(currentMillis);
  break;
  case 4:
  ledMicd();
  break;
}

modeChange();
}

void modeChange()
{
  buttonState = digitalRead(buttonPin);
  if ((buttonState != lastButtonState) && (buttonState == HIGH))
  {
    ledMode++;
    ledMode %= modes;
    //delay(500); //This debounce is not necessary, the flashing LED's will function as debounce
   
    //LED's off
    digitalWrite(leftLed , LOW);
    digitalWrite(rightLed , LOW);

    //Flash LED's
    for(int i = 0; i<=ledMode; i++)
    {
      digitalWrite(leftLed , HIGH);
      digitalWrite(rightLed , HIGH);
      delay (250);
      digitalWrite(leftLed , LOW);
      digitalWrite(rightLed , LOW);
      delay (250);
    }
  }
  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)
{
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement; 
      revFade = revFade - fadeIncrement;
      if (fadeValue >= maxPWM) {
        fadeValue = maxPWM;
        revFade = minPWM;
        fadeDirection = DOWN;
      }
    } else {
      fadeValue = fadeValue - fadeIncrement;
      revFade = revFade + fadeIncrement;
      if (fadeValue <= minPWM) {
        fadeValue = minPWM;
        revFade = maxPWM;
        fadeDirection = UP;
      }
    }
    analogWrite(leftLed, fadeValue); 
    analogWrite(rightLed, revFade);
 
    previousFadeMillis = thisMillis;
  }
}

void ledVU()
{
  left = analogRead(leftCh);
  right = analogRead(rightCh);
  
  if (leftLarge < left)
    leftLarge = left;
  if (rightLarge < right)
    rightLarge = right;

  leftMap = map(left, 0, leftLarge, 0, 255);
  rightMap = map(right, 0, rightLarge, 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);
}

void maxVol(unsigned long thisMillis)
{
  if (thisMillis - previousFadeMillis >= volInterval) {
    leftLarge = 0;
    rightLarge = 0;
    }
    previousFadeMillis = thisMillis;
}

I'm not sure that I entirely understand what you want, but "line out" from a device is usually a fixed volume, so please examine if your "line out" actually changes volume or not. Sound pressure is not linear and it can be pretty tricky to deal with. As far as I recall an increase of 6dB doubles the sound pressure - if you double the dB from 10 to 20 you are more than tripling the sound pressure. If volume is set to 50% and this yields a value of 512, then 25% volume cannot yield a value of 256 - that is AFAIK impossible. So in order to map such a value, you must use some sort of logarithmic algorithm.

To put it simply, I wanted to reset the leftLarge and rightLarge value to 0 based on a certain interval set in the beginning. you can refer the code in my previous post at the very bottom I added maxVol() function to reset those 2 variables using millis.

I used the serial monitor from a nano and those max value didn't get reset overtime.

Seems to be a bit of the same issue again, you must only update previousFadeMillis when *Large is reset:

void maxVol(unsigned long thisMillis)
{
  if (thisMillis - previousFadeMillis >= volInterval) {

    //This code block is never reached, because previousFadeMillis is
    //outside the block which it is supposed to trigger. Move it inside the
    //brackets and the problem should be solved.

    leftLarge = 0;
    rightLarge = 0;
    }
    previousFadeMillis = thisMillis; //This is wrong
}