How to delay increments within for() loop?

From what I understand with the code below, x is incrementing way to quickly for the ledModes to switch-

if(count % 2 == 0)
    {
        for (byte x = 1; x < 8; x++)
        {
            ledMode(x);
            Serial.println(x);
        }

    }

I've tried to implement BWD, to slow down the increments like such down below, but each variation failed.

  unsigned long currentMillis, debounceStartMillis;
const unsigned long debouncePeriod = 20;
byte currentButtonState, previousButtonState;
bool debouncing = false;
int count = 0;
const int BUTTON = A1;
const int RLED = 10;
const int GLED = 9;
const int BLED = 8;
const unsigned long Interval = 1000;
unsigned long previousTime = 0;

void setup()
{
	Serial.begin(115200);
	pinMode(BUTTON, INPUT_PULLUP);
	pinMode(RLED, OUTPUT);
	pinMode(GLED, OUTPUT);
	pinMode(BLED, OUTPUT);
}


void button()
{
	currentMillis = millis();
	previousButtonState = currentButtonState;
	currentButtonState = digitalRead(BUTTON);
	if (currentButtonState != previousButtonState)
	{
		debouncing = true;
		debounceStartMillis = currentMillis;
	}
	if (currentMillis - debounceStartMillis >= debouncePeriod)
	{
		if (debouncing == true)
		{
			if (currentButtonState == LOW)
			{
				debouncing = false;
				count++;
				Serial.println(count);
			}
		}
	}
}

void ledMode(byte mode)
{
    unsigned long currentTime = millis();
    if (currentTime - previousTime >= Interval)
    {
        switch (mode)
        {
        case 1:
            digitalWrite(RLED, HIGH); //Red
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, LOW);
            Serial.println("Red!");
            previousTime = currentTime;
            break;

        case 2:
            digitalWrite(RLED, LOW);  //Green
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, HIGH);
            Serial.println("GREEN!");
            previousTime = currentTime;
            break;

        case 3:
            digitalWrite(RLED, LOW);  //blue
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, LOW);
            Serial.println("BLUE!");
            previousTime = currentTime;
            break;

        case 4:
            digitalWrite(RLED, HIGH);  //orange
            digitalWrite(BLED, LOW);
            digitalWrite(GLED, HIGH);
            Serial.println("ORANGE!");
            previousTime = currentTime;
            mode = 5;
            break;

        case 5:
            digitalWrite(RLED, LOW);  //teal
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, HIGH);
            Serial.println("TEAL!");
            previousTime = currentTime;
            break;
        case 6:
            digitalWrite(RLED, HIGH); //purple
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, LOW);
            Serial.println("PURPLE!");
            previousTime = currentTime;
            break;

        case 7:
            digitalWrite(RLED, HIGH); //white
            digitalWrite(BLED, HIGH);
            digitalWrite(GLED, HIGH);
            Serial.println("WHITE!");
            previousTime = currentTime;
            break;
        }
    }
}

void RGBoff()
{
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, LOW);
    digitalWrite(GLED, LOW);
}

void loop()
{
    button();
    if(count % 2 == 0)
  {
    unsigned long currentTime = millis();
    for (byte x = 1; x < 8;)
    {
        ledMode(x);
        if (currentTime - previousTime >= Interval)
        {
            Serial.println(x);
            previousTime = currentTime;
            x++;
        }
    }   
}
    else if (count % 2 == 1)
    {
        RGBoff();
    }
}

The for() loop would end up with a delay instead of the increment operation.

I'd like x to increment every 1000 milliseconds...

Any help or suggestions would be greatly appreciated!

Show us the full sketch with the above lines of code incorporated into it.

1 Like

I edited it back into the original message. :+1:

I think you can replace the for() loop with this incrementing of x in the millis() timer.

if (count % 2 == 0)
{
  unsigned long currentTime = millis();
  static byte x = 1;
  ledMode(x);
  if (currentTime - previousTime >= Interval)
  {
    Serial.println(x);
    previousTime = currentTime;
    x++;
    if(x == 8) x = 1;
  }
}

This looks like more to your other thread

which you might have just continued, so we all don’t chase around and have to ask you for details, not least of which would be a (the) full sketch you are working on.

It looks like you haven’t fully understood the help you got on the other thread yet.

a7

Something to try.



bool flag                           = false;

int count                           = 0;
int x                               = 1;

const byte BUTTON                   = A1;

const byte heartbeatLED             = 13;
const byte RLED                     = 10;
const byte GLED                     = 9;
const byte BLED                     = 8;

byte currentButtonState             = HIGH;
byte previousButtonState            = HIGH;

const unsigned long debouncePeriod  = 50;
const unsigned long Interval        = 1000;

unsigned long currentMillis;
unsigned long ledMillis;
unsigned long switchMillis;
unsigned long heartbeatMillis;

//**************************************************************************
void setup()
{
  Serial.begin(115200);

  pinMode(BUTTON, INPUT_PULLUP);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(BLED, OUTPUT);

} //END of setup()


//**************************************************************************
void loop()
{
  //save the current time
  currentMillis = millis();

  //********************************
  //to help show if there is any blocking code, this LED toggles every 500ms
  if (currentMillis - heartbeatMillis >= 500)
  {
    //start this TIMER
    heartbeatMillis = currentMillis;

    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));

  }

  //every 50ms check the switch
  if (currentMillis - switchMillis >= debouncePeriod)
  {
    //restart the TIMER
    switchMillis = currentMillis;

    //go and check the switches
    button();
  }

  //*********************
  //if we are timing, has the TIMER expired ?
  if (flag == true && currentMillis - ledMillis >= Interval)
  {
    //restart the TIMER
    ledMillis = millis();

    //go and change the LED states
    ledMode(x);

    Serial.println(x);

    //next in the sequence
    x++;

    //have we finished the sequence ?
    if (x > 7)
    {
      //we are finished, disable the TIMER
      flag = false;
    }
  }

} //END of  loop()


//**************************************************************************
void button()
{
  currentButtonState = digitalRead(BUTTON);

  //****************
  //has the switch state changed ?
  if (previousButtonState != currentButtonState)
  {
    //update to the new state
    previousButtonState = currentButtonState;

    //is the switch now closed ?
    if (currentButtonState == LOW)
    {
      //is this an even count
      if (count % 2 == 0)
      {
        //enable timing
        flag = true;

        //start at the first state in the sequence
        x = 1;
      }

      else
      {
        RGBoff();
        Serial.println("All LEDs OFF ");
      }

      Serial.print("Switch count = ");
      Serial.println(count);
      
      count++;
    }
    
  } //END of   if(previousButtonState != currentButtonState)
  
} //END of button()


//**************************************************************************
void ledMode(byte mode)
{
  switch (mode)
  {
    case 1:
      digitalWrite(RLED, HIGH); //Red
      digitalWrite(BLED, LOW);
      digitalWrite(GLED, LOW);
      Serial.println("Red!");

      break;

    case 2:
      digitalWrite(RLED, LOW);  //Green
      digitalWrite(BLED, LOW);
      digitalWrite(GLED, HIGH);
      Serial.println("GREEN!");

      break;

    case 3:
      digitalWrite(RLED, LOW);  //blue
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, LOW);
      Serial.println("BLUE!");

      break;

    case 4:
      digitalWrite(RLED, HIGH);  //orange
      digitalWrite(BLED, LOW);
      digitalWrite(GLED, HIGH);
      Serial.println("ORANGE!");

      break;

    case 5:
      digitalWrite(RLED, LOW);  //teal
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, HIGH);
      Serial.println("TEAL!");

      break;

    case 6:
      digitalWrite(RLED, HIGH); //purple
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, LOW);
      Serial.println("PURPLE!");

      break;

    case 7:
      digitalWrite(RLED, HIGH); //white
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, HIGH);
      Serial.println("WHITE!");

      break;

    case 8:
      digitalWrite(RLED, LOW); //black
      digitalWrite(BLED, LOW);
      digitalWrite(GLED, LOW);
      Serial.println("BLACK!");

      break;

  } //END of switch/case
  
} //END of   ledMode(byte mode)


//**************************************************************************
void RGBoff()
{
  digitalWrite(RLED, LOW);
  digitalWrite(BLED, LOW);
  digitalWrite(GLED, LOW);
  
} //END of  RGBoff()

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.