help! - how to make a fixed counter with millis()?

Hi All,

I am currently working in a project which includes an arduino and a RF Receiver board. On this project, the arduino shall read trough I/O Pins the signals of 4 channels of the RF Receiver board and it has to enable 4 outpus (one output per channel), all at same time. Each output will be connected to a LED and I need that each LED shall blink for 30 seconds.

By using "delay" the arduino will not read multiple I/O pins at same time, but according to a lecture on "Adafruit page", by using "millis()" the arduino can do the job.

I've implemented the code showed on "Adafruit Page" to use "millis()" on my code and to read multiple imputs, but right now the code do not work as expected.

The arduino can read multiple inputs with this code and it can enable multiple outputs at same time, but the LEDs do not blink and the LEDs will not go off, in fact, if I press one button of the RF control to enable one channel of the RF board, by pressing the same botton, the arduino will turn off the LED.

Can you help me?

What do I have to do to enable a fixed timer and the blink effect per each output?

//RF BELL PROJECT FOR MFG LINES

//Name assigned to I/O Pins
const int RF1 = 9; 
const int RF2 = 10;
const int RF3 = 11;
const int RF4 = 12;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;
const int ledPin4 = 6;

//Channel 1
int ledState1 = LOW;
unsigned long previousMillis1 = 0;        // will store last time LED was updated
long OnTime1 = 250;           // milliseconds of on-time
long OffTime1 = 750;          // milliseconds of off-time

//Channel 2
int ledState2 = LOW;  
unsigned long previousMillis2 = 0;        // will store last time LED was updated
long OnTime2 = 330;           // milliseconds of on-time
long OffTime2 = 400;          // milliseconds of off-time

//Channel 3
int ledState3 = LOW;
unsigned long previousMillis3 = 0;        // will store last time LED was updated
long OnTime3 = 250;          // milliseconds of on-time
long OffTime3 = 750;          // milliseconds of off-time

//Channel 4
int ledState4 = LOW;
unsigned long previousMillis4 = 0;        // will store last time LED was updated
long OnTime4 = 250;           // milliseconds of on-time
long OffTime4 = 750;          // milliseconds of off-time

void setup() {
  
  // I/O Pins
pinMode (ledPin1, OUTPUT); //Output channel 1
pinMode (ledPin2, OUTPUT); //Output channel  2
pinMode (ledPin3, OUTPUT); //Output channel  3
pinMode (ledPin4, OUTPUT); //Output channel  4

  pinMode (RF1, INPUT); //Input channel 1
    pinMode (RF2, INPUT); //Input channel 2
      pinMode (RF3, INPUT); //Input channel 3
        pinMode (RF4, INPUT); //Input channel 4
}

void loop() {
   
       unsigned long currentMillis = millis();
       
       // If channel 1 is activated, initialize counter # 1
while (digitalRead(RF1) == HIGH) {

  
    if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis;  // Remember the time
    digitalWrite(ledPin1, ledState1);  // Update LED 1
  }
  else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    ledState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    digitalWrite(ledPin1, ledState1);	  // Update LED 1
  
  }
}
 // If channel 2 is activated, initialize counter # 2
while (digitalRead(RF2) == HIGH) {  

  if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
  {
    ledState2 = LOW;  // Turn it off
    previousMillis2 = currentMillis;  // Remember the time
    digitalWrite(ledPin2, ledState2);  // Update LED 2
  }
  else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
  {
    ledState2 = HIGH;  // turn it on
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(ledPin2, ledState2);	  // Update LED 2
  } 
    
}
 // // If channel 3 is activated, initialize counter # 3
while (digitalRead(RF3) == HIGH) {  

  if((ledState3 == HIGH) && (currentMillis - previousMillis3 >= OnTime2))
  {
    ledState3 = LOW;  // Turn it off
    previousMillis3 = currentMillis;  // Remember the time
    digitalWrite(ledPin3, ledState3);  // Update LED 3
  }
  else if ((ledState3 == LOW) && (currentMillis - previousMillis3 >= OffTime2))
  {
    ledState3 = HIGH;  // turn it on
    previousMillis3 = currentMillis;   // Remember the time
    digitalWrite(ledPin3, ledState3);	  // Update LED 3

}
}
 // // If channel 4 is activated, initialize counter # 4
while (digitalRead(RF4) == HIGH) {  

  if((ledState4 == HIGH) && (currentMillis - previousMillis4 >= OnTime2))
  {
    ledState4 = LOW;  // Turn it off
    previousMillis4 = currentMillis;  // Remember the time
    digitalWrite(ledPin4, ledState4);  // Update LED 4
  }
  else if ((ledState4 == LOW) && (currentMillis - previousMillis4 >= OffTime2))
  {
    ledState4 = HIGH;  // turn it on
    previousMillis4 = currentMillis;   // Remember the time
    digitalWrite(ledPin4, ledState4);	  // Update LED 4
  }
}
}

Each channel is tested inside a while loop, which won't work. This blocks the flow of execution on one channel.

I think you need to replace while with 'if'

My strong suspicion is that you should replace all the WHILEs with IFs, because WHILE blocks.

Have a look at the demo several things at a time.

I think it would be easiest of you create a small function to cause the LED to flash and call that as needed.

...R