LEDs Help required

Hi Everyone,

I wonder if anyone can help me.

I'm starting a new project and it requires that there are 8 LEDs connected to the arduino with an RCWL-0516 microwave radar sensor.

What happens is that when the Microwave sensor is triggered then the LEDs flash.

As mentioned before there will be 8 LEDs (4 green and 4 red).

At the moment I've got the LEDs connected to the arduino with 220R resistors to ground then the trigger pins to the positive side of each LED. They all use a common ground.

I've got the LEDs lighting sequentially with the following code:

for (int I = 0; I < 9; i++){
digitalWrite(LedPin[i], HIGH);
delay(250);
digitalWrite(LedPin[i], LOW);
}

What I want to happen is that the 4 green LEDs flash then the 4 red ones. The green ones needs to flash at the same time and so do the red ones.

I have created an array for the LEDpins to sit in:

int LedPin[] = {2, 3 ,4 ,5 ,6, 7, 8, 9};

In the void setup I've got:
pinMode(LedPin, OUTPUT)

Green LEDs connected to 2,4,6,8 and red LEDs connected to 3,5,7,9

In the loop I've got this:

if (digitalRead(AntPin) == HIGH) {
for (int g = 0; g < 8; g = g+2) { // this selects the output pins for the green leds
digitalWrite(LedPin[g], HIGH);
delay(500);
digitalWrite(LedPin[g], LOW);
}

The above code works so when the RCWL-0516 is triggered (pin 12) then the LEDs light up but they light up in sequential order.

What I would like to know is there a way to get all 4 green LEDs to light up at the same time without having loads of digitalWrite statements for each individual LED pin.

I could be sloppy and write out each digitalWrite statement for each individual LED pin but I like my code to look tidy :laughing::rofl::joy:.

Any help would be appreciated.

Kindest Regards,

Brett.

For me was helpful page "PortManipulation"

I think it will be easier if all green LEDs were on pins 4, 5, 6, 7 and red LEDs on pins 8, 9,10, 11,
then for green:
PORTD |= B11110000; to set on
PORTD &= ~(B11110000); to set off

for red:
PORTB |= B00001111; to set on
PORTB &= ~(B00001111); to set off

enjoy & good luck

Post all your code, whole, in one place. Posting it in snippets makes it very hard to follow and likely leaves out parts of the whole.
Read the forum guidelines.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Apologies for that, I was using my phone and didn't have access to my code

int LedPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
int AntPin = 12;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LedPin, OUTPUT);
  pinMode(AntPin, INPUT);
  for (int i = 0; i < 9; i++) {
    digitalWrite(LedPin[i], HIGH);
    delay(250);
    digitalWrite(LedPin[i], LOW);
  }
  /*digitalWrite(LedPin[0], HIGH);
    digitalWrite(LedPin[1], HIGH);
    digitalWrite(LedPin[2], HIGH);
    digitalWrite(LedPin[3], HIGH);
    digitalWrite(LedPin[4], HIGH);
    digitalWrite(LedPin[5], HIGH);
    digitalWrite(LedPin[6], HIGH);
    digitalWrite(LedPin[7], HIGH);
    delay(250);
    digitalWrite(LedPin[0], LOW);
    digitalWrite(LedPin[1], LOW);
    digitalWrite(LedPin[2], LOW);
    digitalWrite(LedPin[3], LOW);
    digitalWrite(LedPin[4], LOW);
    digitalWrite(LedPin[5], LOW);
    digitalWrite(LedPin[6], LOW);
    digitalWrite(LedPin[7], LOW);
    delay(250);
    digitalWrite(LedPin[0], HIGH);
    digitalWrite(LedPin[1], HIGH);
    digitalWrite(LedPin[2], HIGH);
    digitalWrite(LedPin[3], HIGH);
    digitalWrite(LedPin[4], HIGH);
    digitalWrite(LedPin[5], HIGH);
    digitalWrite(LedPin[6], HIGH);
    digitalWrite(LedPin[7], HIGH);
    delay(250);
    digitalWrite(LedPin[0], LOW);
    digitalWrite(LedPin[1], LOW);
    digitalWrite(LedPin[2], LOW);
    digitalWrite(LedPin[3], LOW);
    digitalWrite(LedPin[4], LOW);
    digitalWrite(LedPin[5], LOW);
    digitalWrite(LedPin[6], LOW);
    digitalWrite(LedPin[7], LOW);*/
  delay(2000);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(AntPin) == HIGH) {
    Serial.println("Motion Detected");
      for (int g = 0; g < 8; g = g + 2) { // this selects the output pins for the green leds
        digitalWrite(LedPin[g], HIGH);
        delay(500);
        digitalWrite(LedPin[g], LOW);
      }
      delay(100);
      for (int r = 1; r < 8; r = r + 2) { //this selects the output pins for the red leds
        digitalWrite(LedPin[r], HIGH);
        delay(500);
        digitalWrite(LedPin[r], LOW);
      }
      /*digitalWrite(LedPin[0], HIGH);
        digitalWrite(LedPin[2], HIGH);
        digitalWrite(LedPin[4], HIGH);
        digitalWrite(LedPin[6], HIGH);
        }
        delay(500);
        digitalWrite(LedPin[0], LOW);
        digitalWrite(LedPin[2], LOW);
        digitalWrite(LedPin[4], LOW);
        digitalWrite(LedPin[6], LOW);
        digitalWrite(LedPin[1], HIGH);
        digitalWrite(LedPin[3], HIGH);
        digitalWrite(LedPin[5], HIGH);
        digitalWrite(LedPin[7], HIGH);
        delay(500);
        digitalWrite(LedPin[1], LOW);
        digitalWrite(LedPin[3], LOW);
        digitalWrite(LedPin[5], LOW);
        digitalWrite(LedPin[7], LOW);*/
  }
  else {
    Serial.println("Waiting for Trigger");
    digitalWrite(LedPin[0], LOW);
    digitalWrite(LedPin[1], LOW);
    digitalWrite(LedPin[2], LOW);
    digitalWrite(LedPin[3], LOW);
    digitalWrite(LedPin[4], LOW);
    digitalWrite(LedPin[5], LOW);
    digitalWrite(LedPin[6], LOW);
    digitalWrite(LedPin[7], LOW);
  }
}

Thanks Kolaha, I will try this tomorrow!

Many Thanks,

Brett

Does pinMode work with an array of pins like that?

Yeah, it seem to work for me.

I tried it with pinMode(LedPin[], OUTPUT) but it threw up an error because there's nothing in the brackets. I deleted the brackets and it went through, compiled and uploaded successfully.

Arrays don't work that way. You need a loop.

You should get a compiler warning about this. Your array has 8 elements numbered 0 through 7. When you try to use element number 8 you will go off the end.

Hi, @kolaha

It might be worth your while looking at this, so your information is easy to read.
Please read the post at the start of any forum , entitled "How to use this Forum".

Especially how to place code.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi Folks,

This is a quick update to my situation.

I have tried what @kolaha mentioned and it worked a treat.

Here's what I have now in my code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  DDRD = DDRD | B11110010; //Sets Digital Pins 2, 4, 5, 6, 7 as Outputs - Buzzer = Pin 2, Green LED's = Pins 4, 5, 6, 7
  DDRB = DDRB | B00001111; //Sets Digital Pins 8, 9, 10, 11 as Outputs - Red LED's = Pins 8, 9, 10, 11
  //************************************************************************************************************************
  //STARTUP ROUTINE - All LED's FLASH TWICE
  PORTD |= B11110000; //Turns on all Green LED's
  PORTB |= B00001111; //Turns on all Red LED's
  delay(500);
  PORTD &= ~(B11110000); //Turns off all Green LED's
  PORTB &= ~(B00001111); //Turns off all Red LED's
  delay(500);
  PORTD |= B11110000; //Turns on all Green LED's
  PORTB |= B00001111; //Turns on all Red LED's
  delay(500);
  PORTD &= ~(B11110000); //Turns off all Green LED's
  PORTB &= ~(B00001111); //Turns off all Red LED's
  delay(2000);
  //************************************************************************************************************************
}

void loop() {
  // put your main code here, to run repeatedly:
  if (PINB & (1 << 4)) { //If Pin 12 is HIGH (Triggered) - RCWL-0516 Attached to Digitial Pin 12.
    buzzer();
    PORTD |= B11110000; //Turns all The Green LED's ON
    delay(500);
    PORTD &= ~(B11110000); //Turns all The Green LED's OFF
    delay(500);
    PORTB |= B00001111; //Turns all The Red LED's ON
    delay(500);
    PORTB &= ~(B00001111); //Turns all The Red LED's OFF
    delay(500);
  }
  else {
    Serial.println("Waiting for Trigger");
  }
}

void buzzer() {
  tone(2, 500, 2000);
}

It's much neater now. Thank you for the help. Much appreciated.

Brett

But a lot less portable.

wrong. must be B11110100. last two bits are for serial pins 0 and 1. you should be careful with them because of possible trouble by upload the sketch.

Thanks for that, I will amend my code. MY BAD! got the wrong digit!

Regards,

The last sketch with the correction in Wokwi simulation (you might want to turn the volume down):

I think that the sketch should lengthen the trigger signal. So I made a fancy sketch for that, but it turned out to be a lot longer. I'm not very happy with it, but here it is:

const int buzzerPin = 2;
const int redLedPins[4] = {8, 9, 10, 11};
const int greenLedPins[4] = {4, 5, 6, 7};
const int buttonPin = 12;

#define RED_ON    for( auto pin:redLedPins)   digitalWrite(pin, HIGH)
#define RED_OFF   for( auto pin:redLedPins)   digitalWrite(pin, LOW)
#define GREEN_ON  for( auto pin:greenLedPins) digitalWrite(pin, HIGH)
#define GREEN_OFF for( auto pin:greenLedPins) digitalWrite(pin, LOW)

bool triggered = false;
unsigned long previousMillisDuration;
const unsigned long duration = 2000;       // duration of blinking leds and buzzer
unsigned long previousMillisBlink;
const unsigned long blinkTime = 500;       // blinktime for blinking leds
int count;

int lastButtonState;

void setup() 
{
  Serial.begin(9600);
  Serial.println( "Led flasher");
  Serial.println( "-----------");

  pinMode( buzzerPin, OUTPUT);

  for( auto pin:redLedPins)
    pinMode( pin, OUTPUT);
  for( auto pin:greenLedPins)
    pinMode( pin, OUTPUT);

  pinMode( buttonPin, INPUT);


  // --------------------------------------------------
  // STARTUP ROUTINE - All LED's FLASH TWICE
  // --------------------------------------------------
  for( int i=0; i<2; i++)
  {
    RED_ON; 
    GREEN_ON;
    delay(500);
    RED_OFF;
    GREEN_OFF;
    delay(500);
  }
}


void loop() 
{
  unsigned long currentMillis = millis();

  // --------------------------------------------------
  // State Change Detection for trigger input
  // --------------------------------------------------
  int buttonState = digitalRead( buttonPin);
  if( buttonState != lastButtonState)
  {
    if( buttonState == HIGH)        // triggered ?
    {
      Serial.println( "Trigger");
      previousMillisDuration = currentMillis;
      triggered = true;

      tone( 2, 500);
    }
    lastButtonState = buttonState;
  }

  // --------------------------------------------------
  // millis-timer
  // --------------------------------------------------
  if( triggered)
  {
    if( currentMillis - previousMillisBlink >= blinkTime)
    {
      previousMillisBlink = currentMillis;

      switch( count)
      {
        case 0:
          GREEN_ON;
          break;
        case 1:
          GREEN_OFF;
          break;
        case 2:
          RED_ON;
          break;
        case 3:
          RED_OFF;
          break;
      }

      count++;
      if( count >= 4)
        count = 0;
    }

    if( currentMillis - previousMillisDuration >= duration)
    {
      noTone( 2);
      GREEN_OFF;
      RED_OFF;
      triggered = false;
      Serial.println( "Waiting ...");
    }
  }
}

This fancy sketch in Wokwi (every trigger is captured, even when the buzzer is on and the leds are flashing):

based on @Koepel
some variable types,
removed precompiler #define
used reference in for (auto

const byte buzzerPin = 2;
const byte redLedPins[] = {8, 9, 10, 11};
const byte greenLedPins[] = {4, 5, 6, 7};
const byte buttonPin = 12;

void green(byte val)
{
  for( auto &pin:greenLedPins)   digitalWrite(pin, val);
}

void red(byte val)
{
  for( auto &pin:redLedPins)   digitalWrite(pin, val);
}

bool triggered = false;
unsigned long previousMillisDuration;
const unsigned long duration = 2000;       // duration of blinking leds and buzzer
unsigned long previousMillisBlink;
const unsigned long blinkTime = 500;       // blinktime for blinking leds
int count;

int lastButtonState;

void setup() 
{
  Serial.begin(9600);
  Serial.println( "Led flasher");
  Serial.println( "-----------");
  pinMode( buzzerPin, OUTPUT);
  for( auto &pin:redLedPins)
    pinMode( pin, OUTPUT);
  for( auto &pin:greenLedPins)
    pinMode( pin, OUTPUT);
  pinMode( buttonPin, INPUT);

  // --------------------------------------------------
  // STARTUP ROUTINE - All LED's FLASH TWICE
  // --------------------------------------------------
  for( int i=0; i<2; i++)
  {
    red(HIGH); 
    green(HIGH);
    delay(500);
    red(LOW);
    green(LOW);
    delay(500);
  }
}


void loop() 
{
  unsigned long currentMillis = millis();

  // --------------------------------------------------
  // State Change Detection for trigger input
  // --------------------------------------------------
  int buttonState = digitalRead( buttonPin);
  if( buttonState != lastButtonState)
  {
    if( buttonState == HIGH)        // triggered ?
    {
      Serial.println( "Trigger");
      previousMillisDuration = currentMillis;
      triggered = true;

      tone( 2, 500);
    }
    lastButtonState = buttonState;
  }

  // --------------------------------------------------
  // millis-timer
  // --------------------------------------------------
  if( triggered)
  {
    if( currentMillis - previousMillisBlink >= blinkTime)
    {
      previousMillisBlink = currentMillis;

      switch( count)
      {
        case 0:
          green(HIGH);
          break;
        case 1:
          green(LOW);
          break;
        case 2:
          red(HIGH);
          break;
        case 3:
          red(LOW);
          break;
      }

      count++;
      if( count >= 4)
        count = 0;
    }

    if( currentMillis - previousMillisDuration >= duration)
    {
      noTone( 2);
      green(LOW);
      red(LOW);
      triggered = false;
      Serial.println( "Waiting ...");
    }
  }
}
1 Like

Very nice!

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