I need some BIG help with this project of mine

Ok. For my wires, there are 4 leds side by side, but not too close. on the minus part of the bread board, i have a ground circuit running through it, and there are 4 wires each one powering each leds with ground. then I have 4 more leds that are each in their output and powering the led. the sensors are getting their ground power from the same circuit I mentioned, and there is a 2nd circuit on the plus side running 5v through it. and It is being powered by its 2 circuit, 3 circuit, etc. Keep in mind that the dfplayer mini is also on the board, but I made sure it isn't interfering with anything. Here is the code. what do i do next?

#include <SPI.h>

int sensor2 = 2;
int led2 = 22;
int sensor3 = 3;
int led3 = 23;
int sensor4 = 4;
int led4 = 24;
int sensor5 = 5;
int led5 = 25;

bool LED_2_ENABLED = true ;
bool LED_3_ENABLED = true ;
bool LED_4_ENABLED = true ;
bool LED_5_ENABLED = true ;

void setup() {
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(sensor2, INPUT);// Input from sensor
  pinMode(led2, OUTPUT);// OUTPUT to alarm or LED
    pinMode(sensor3, INPUT);
    pinMode(led3, OUTPUT);
    pinMode(sensor4, INPUT);
  pinMode(led4, OUTPUT);
 pinMode(sensor5, INPUT);
  pinMode(led5, OUTPUT);
    
    digitalWrite(sensor2,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(sensor3,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(sensor4,LOW);
  digitalWrite(led4,HIGH);
  digitalWrite(sensor5,LOW);
  digitalWrite(led5,HIGH);               
}

void loop() {

  int sensor2 =digitalRead(2);
  if ((sensor2)&& (LED_2_ENABLED==true)){
            digitalWrite(led2,LOW);
        Serial.println("Motion Detected");
 LED_2_ENABLED = false ;
  }else{
       digitalWrite(led2,HIGH);
  Serial.println("Nothing Moves");
  }

  int sensor3 =digitalRead(3);
  if ((sensor3)&& (LED_3_ENABLED==true)){
           digitalWrite(led3,LOW);
        Serial.println("Motion Detected2");
    LED_3_ENABLED = false ;
  }else{
     digitalWrite(led3,HIGH);       
       Serial.println("Nothing Moves2");
  }
 
 int sensor4 =digitalRead(4);
  if ((sensor4)&& (LED_4_ENABLED==true)){
    digitalWrite(led4,LOW);        
        Serial.println("Motion Detected3");
    LED_4_ENABLED = false ;
  }else{
     digitalWrite(led4,HIGH);       
       Serial.println("Nothing Moves3");
   }

  int sensor5 =digitalRead(5);
  if ((sensor5)&& (LED_5_ENABLED==true)){
    digitalWrite(led5,LOW);        
        Serial.println("Motion Detected4");
  LED_5_ENABLED = false ;
  }else{
      digitalWrite(led5,HIGH);      
       Serial.println("Nothing Moves4");
 }
}

I'm sorry, But my account says I'm a newbie for a reason :blush:

this is the coding I have right now, but I think I took a huge step backwards. Basically when I test it, the lights turn off one after another on their own, in a really fast manner. Please just let me know what i did wrong. I want them to turn off forever when their sensor gets movement.

#include <SPI.h>

int sensor2 = 2;
int led2 = 22;
int sensor3 = 3;
int led3 = 23;
int sensor4 = 4;
int led4 = 24;
int sensor5 = 5;
int led5 = 25;

int sensorState=1;
int ledState;
int sensorState2=1;
int ledState2;
int sensorState3=1;
int ledState3;
int sensorState4=1;
int ledState4;

void setup() {
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(sensor2, INPUT);// Input from sensor
  pinMode(led2, OUTPUT);// OUTPUT to alarm or LED
    pinMode(sensor3, INPUT);
    pinMode(led3, OUTPUT);
    pinMode(sensor4, INPUT);
  pinMode(led4, OUTPUT);
 pinMode(sensor5, INPUT);
  pinMode(led5, OUTPUT);
    
    digitalWrite(sensor2,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(sensor3,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(sensor4,LOW);
  digitalWrite(led4,HIGH);
  digitalWrite(sensor5,LOW);
  digitalWrite(led5,HIGH);               
}

void loop() {

sensorState = digitalRead(sensor2);
 
while(sensorState == 1);
          
            digitalWrite(led2,LOW);
       delay(50);
        Serial.println("Motion Detected");
   

sensorState2 = digitalRead(sensor3);
  
while(sensorState2 == 1);

           digitalWrite(led3,LOW);
               delay(50);
        Serial.println("Motion Detected2");



sensorState3 = digitalRead(sensor4);
  
while(sensorState2 == 1);
  
    digitalWrite(led4,LOW);        
               delay(50);
        Serial.println("Motion Detected3");
  

sensorState4 = digitalRead(sensor5);
  
while(sensorState4 == 1);
   
    digitalWrite(led5,LOW);        
               delay(50);
        Serial.println("Motion Detected4");
}

An example of how I would do it for you to play with

struct dataLayout
{
  byte sensorPin;
  byte ledPin;
  boolean active;
};

dataLayout data[4] =
{
  {A3, 3, true},
  {A2, 5, true},
  {A1, 6, true},
  {A0, 9, true}
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 4; d++)
  {
    pinMode(data[d].ledPin, OUTPUT);
    digitalWrite(data[d].ledPin, LOW);  //turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}

void loop()
{
  for (int d = 0; d < 4; d++)
  {
    if (data[d].active)
    {
      Serial.print(d);
      if (digitalRead(data[d].sensorPin) == LOW)
      {
        digitalWrite(data[d].ledPin, HIGH);  //turn off the LED
        data[d].active = false;  //stop further action for this LED
      }
    }
  }
  Serial.println();
}

UKHeliBob:
An example of how I would do it for you to play with

struct dataLayout

{
  byte sensorPin;
  byte ledPin;
  boolean active;
};

dataLayout data[4] =
{
  {A3, 3, true},
  {A2, 5, true},
  {A1, 6, true},
  {A0, 9, true}
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 4; d++)
  {
    pinMode(data[d].ledPin, OUTPUT);
    digitalWrite(data[d].ledPin, LOW);  //turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}

void loop()
{
  for (int d = 0; d < 4; d++)
  {
    if (data[d].active)
    {
      Serial.print(d);
      if (digitalRead(data[d].sensorPin) == LOW)
      {
        digitalWrite(data[d].ledPin, HIGH);  //turn off the LED
        data[d].active = false;  //stop further action for this LED
      }
    }
  }
  Serial.println();
}

How exactly do I wire it. like do i put the lights in the A3 Slot or is that for the sensors? is the 3, 5, 6, 9 for the sensors or lights?

Edit: never mind I figured it out. But do I have to write the setup and loop code 4 times? you know, one for each light and sensor, Or will this string of code cover all of them separately?

I am glad that you figured it out. The clues are in the names of the pins

will this string of code cover all of them separately?

The code as written will deal with 4 inputs, each with its own LED. That is what the for loop is doing. Why not try it with your own pin numbers ?

Note that as written the inputs need to be taken LOW when activated due to the use of INPUT_PULLUP in their pinMode and that the LEDs are on when their pins are LOW and off when they are HIGH. That arrangement suits my test system so change the wiring and/or the logic to suit your circuit

Thank you for this. there are 2 last things I want to ask

  1. Just to clarify, the A0, A1, A2, and A3 are for the sensors?

  2. what is the code dealing with the sensors? I would like to know because it isn't working for me.

Also keep in mind that this sketch is for regular leds. Later on when I get them, I will have to use rgb lights, so that they change color. Could you tell me the RGB color code for the colors purple, orange, and red? But again, thank you for the Help!

  1. Just to clarify, the A0, A1, A2, and A3 are for the sensors?

Yes, but you can use any pins except 0 and 1

I used buttons that take the inputs (A3, A2, A1 and A0) LOW when pressed. They are HIGH when not pressed due to the INPUT_PULLUP in pinMode() activating the built in pullup resistors

  1. what is the code dealing with the sensors? I would like to know because it isn't working for me.
if (digitalRead(data[d].sensorPin) == LOW)

reads the pin of one of the sensors. Which pin, and hence which sensor, depends on the value of d in the for loop. As d changes the code successively reads each of the input pins read from the array of structs. For instance, when d is zero the code is equivalent to

if (digitalRead(A3) == LOW)

and when d is 2 it is equivalent to

if (digitalRead(A1) == LOW)

Could you tell me the RGB color code for the colors purple, orange, and red?

Do you mean RGB LEDs or programmable LEDs ?

UKHeliBob:
Do you mean RGB LEDs or programmable LEDs ?

I mean those RGB leds that you can use to change color

You can change the colour of both normal RGB LEDs and programmable LEDs such as WS2812

I suspect that you mean the latter, but please be more specific

This is my code for now. But it isn't working for me. The sensors aren't even activating for me. I will keep going and tell my result.

struct dataLayout
{
  byte sensorPin;
  byte ledPin;
  boolean active;
};

dataLayout data[4] =
{
  {A3, 3, true},
  {A2, 5, true},
  {A1, 6, true},
  {A0, 9, true}
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 4; d++)
  {
    pinMode(data[d].ledPin, OUTPUT);
    digitalWrite(data[d].ledPin, LOW);  //turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}

void loop()
{
  for (int d = 0; d < 4; d++)
  {
    if (data[d].active)
    {
      Serial.print(d);
      if (digitalRead(data[d].sensorPin) == LOW)
      {
        digitalWrite(data[d].ledPin, HIGH);  //turn off the LED
        data[d].active = false;  //stop further action for this LED
      }
    }
  }
  Serial.println();
}

UPDATE: :slight_smile:

Message to UKHeliBob...

It works! Thanks to HeliBob. it works! the leds turn off forever, and each have their own sensor! I don't know why it took this long, honestly. But Thanks HeliBob! and also thanks to the other people who helped me! (I dont know why I'm talking like I finished the project).

Now it's time to figure out the audio. I will attempt to do so by myself and see what I can do. Thanks everyone!

k well, I can't figure it out. Can someone help? now that I got the lights to turn off forever, can someone let me know how i play a sound every time one goes out? here is the code so you can get an Idea
myDFPlayer.volume(30); //Set volume value. From 0 to 30 myDFPlayer.play(4); //Play the first mp3

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(11, 10); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

struct dataLayout
{
  byte sensorPin;
  byte ledPin;
  boolean active;
};

dataLayout data[4] =
{
  {A3, 3, true},
  {A2, 5, true},
  {A1, 6, true},
  {A0, 9, true}
};

void setup()
{
    mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 4; d++)
  {
    pinMode(data[d].ledPin, OUTPUT);
    digitalWrite(data[d].ledPin, HIGH);  //turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}
void loop()
{
  for (int d = 0; d < 4; d++)
  {
    if (data[d].active)
    {
      if (digitalRead(data[d].sensorPin))
      {
        digitalWrite(data[d].ledPin, LOW);  //turn off the LED
        Serial.println("Motion Detected");
        data[d].active = false;  //stop further action for this LED
      }
    }
  }
}

Insert

myDFPlayer.volume(30); //Set volume value. From 0 to 30 
myDFPlayer.play(d); //Play the first mp3

after setting data[d].active to false and it should play the sound file corresponding to the value of d at that point. This assumes that you have tested that the commands work and actually play a sound. If you always want the same sound then replace d with the sound file number

However, if playing a sound blocks code operation until it is finished playing then you may have problems

Where exactly Do I put that mp3 string of code? Cause I tried, but wherever I put the code, the lights just go bad. some go dim, some don't turn off and others just don't do anything, and it doesn't ever play the sound. Here is the code

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(11, 10); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

struct dataLayout
{
  byte sensorPin;
  byte ledPin;
  boolean active;
};

dataLayout data[4] =
{
  {A3, 3, true},
  {A2, 5, true},
  {A1, 6, true},
  {A0, 9, true}
};

void setup()
{
    mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 4; d++)
  {
    pinMode(data[d].ledPin, OUTPUT);
    digitalWrite(data[d].ledPin, HIGH);  //turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}
void loop()
{
  for (int d = 0; d < 4; d++)
  {
    if (data[d].active)
    {
      if (digitalRead(data[d].sensorPin))
      {
        digitalWrite(data[d].ledPin, LOW);  //turn off the LED

        Serial.println("Motion Detected");
        data[d].active = false;  //stop further action for this LED
        
      }
    }
  }
}

Where do I put it?

Where exactly Do I put that mp3 string of code?

        data[d].active = false;  //stop further action for this LED
       >>>>>>>>>>>>>>>  HERE <<<<<<<<<<<<<<<
      }

Ok I put it there but now the lights dim and won't turn off. The sound doesn't play either but I'll worry about that later. Here is the code

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(11, 10); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

struct dataLayout
{
  byte sensorPin;
  byte ledPin;
  boolean active;
};

dataLayout data[4] =
{
  {A3, 3, true},
  {A2, 5, true},
  {A1, 6, true},
  {A0, 9, true}
};

void setup()
{
    mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 4; d++)
  {
    pinMode(data[d].ledPin, OUTPUT);
    digitalWrite(data[d].ledPin, HIGH);  //turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}
void loop()
{
  for (int d = 0; d < 4; d++)
  {
    if (data[d].active)
    {
      if (digitalRead(data[d].sensorPin))
      {
        digitalWrite(data[d].ledPin, LOW);  //turn off the LED

        Serial.println("Motion Detected");
        data[d].active = false;  //stop further action for this LED
         myDFPlayer.volume(30);  //Set volume value. From 0 to 30
  myDFPlayer.play(3);  //Play the first mp3
      }
    }
  }
}

Forget your project for now. Have you managed to play an MP3 file using a simple example from the library ?

With this code, i have been able to. there are 4 sounds in the sd card that are subject to change, but their there for now. This the the code that can play the sound

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(11, 10); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

void setup()
{
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));
 
 myDFPlayer.volume(30);  //Set volume value. From 0 to 30
  myDFPlayer.play(3);  //Play the first mp3
}
 

void loop(){

}

For a start, the code in reply#34 does not have myDFPlayer.begin() in it

Add

  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));

to it