I need BIG help with this project of mine! MK II

Hello Everyone! I made a forum post a while ago addressing a problem of mine, but since we finished the basics of the project, I thought it was only necessary if I make a part 2 to my problem. Anyways, I need help with the rgb lights. The rgbs are not responding when I activate their sensor, Oh by the way, what I am trying to do is have 3 rgbs (Final amount will be 30), each have their own sensors hooked up, so when a sensor is activated, the rgb connected to that sensor will activate. However the rgbs won't turn trigger, instead the sensor is triggered Via a sound playing, but the lights won't change color. i want the lights, when the sensor is triggered, to change from a purple color, to a white color. Here is the code. What am I doing wrong?

#include “Arduino.h”
#include “SoftwareSerial.h”
#include “DFRobotDFPlayerMini.h”

int redPin= 7;
int greenPin = 6;
int bluePin = 5;
int redPin2= 4;
int greenPin2 = 3;
int bluePin2 = 2;
int redPin3= 34;
int greenPin3 = 32;
int bluePin3 = 30;

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

struct dataLayout
{
byte sensorPin;
byte redPin;
byte greenPin;
byte bluePin;
byte redPin2;
byte greenPin2;
byte bluePin2;
byte redPin3;
byte greenPin3;
byte bluePin3;
boolean active;
};

dataLayout data[9] =
{
{A3, 7, true},
{A3, 6, true},
{A3, 5, true},
{A2, 4, true},
{A2, 3, true},
{A2, 2, true},
{A1, 30, true},
{A1, 32, true},
{A1, 34, true},
};

void setup()
{
mySoftwareSerial.begin(9600);
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.”));

mySoftwareSerial.begin(9600);

Serial.begin(115200);
while (!Serial);
for (int d = 0 ; d < 10; d++)
{
pinMode(data[d].redPin, OUTPUT);
pinMode(data[d].greenPin, OUTPUT);
pinMode(data[d].bluePin, OUTPUT);
pinMode(data[d].redPin2, OUTPUT);
pinMode(data[d].greenPin2, OUTPUT);
pinMode(data[d].bluePin2, OUTPUT);
pinMode(data[d].redPin3, OUTPUT);
pinMode(data[d].greenPin3, OUTPUT);
pinMode(data[d].bluePin3, OUTPUT);
digitalWrite(data[d].redPin, LOW);
digitalWrite(data[d].greenPin, HIGH);
digitalWrite(data[d].bluePin,LOW);//turn on the LED
digitalWrite(data[d].redPin2, LOW);
digitalWrite(data[d].greenPin2, HIGH);
digitalWrite(data[d].bluePin2,LOW);
digitalWrite(data[d].redPin3, LOW);
digitalWrite(data[d].greenPin3, HIGH);
digitalWrite(data[d].bluePin3,LOW);
pinMode(data[d].sensorPin, INPUT_PULLUP);
data[d].active = true;
}
}
void loop()
{
for (int d = 0; d < 9; d++)
{
if (data[d].active)
{
if (digitalRead(data[d].sensorPin))
{
digitalWrite(data[d].redPin, LOW);
digitalWrite(data[d].greenPin, LOW);
digitalWrite(data[d].bluePin, LOW);//turn off the LED
digitalWrite(data[d].redPin2, LOW);
digitalWrite(data[d].greenPin2, LOW);
digitalWrite(data[d].bluePin2, LOW);
digitalWrite(data[d].redPin3, LOW);
digitalWrite(data[d].greenPin3, LOW);
digitalWrite(data[d].bluePin3, LOW);

    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(6); //Play the first mp3
}
}
}
}

At a glance:

You aren't initializing your data array of dataLayout structs correctly.

data[0] will be, e.g.

sensorPin set to A3
redPin set to 7
greenPin set to true <-- ?

and all the rest of the struct elements set to 0. You are only supplying 3 values for the 11 elements.

a7

Stepped in the middle of this.. Why are you not using NeoPixels?

-jim lee

Like This?

#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 redPin;
byte greenPin;
byte bluePin;
byte redPin2;
byte greenPin2;
byte bluePin2;
byte redPin3;
byte greenPin3;
byte bluePin3;
boolean active;
};

dataLayout data[9] =
{
{A3, 7, true},
{A3, 6, true},
{A3, 5, true},
{A2, 4, true},
{A2, 3, true},
{A2, 2, true},
{A1, 30, true},
{A1, 32, true},
{A1, 34, true},
};

void setup()
{
mySoftwareSerial.begin(9600);
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."));

mySoftwareSerial.begin(9600);

Serial.begin(115200);
while (!Serial);
for (int d = 0 ; d < 3; d++)
{
pinMode(data[d].redPin, OUTPUT);
pinMode(data[d].greenPin, OUTPUT);
pinMode(data[d].bluePin, OUTPUT);
digitalWrite(data[d].redPin, LOW);
digitalWrite(data[d].greenPin, HIGH);
digitalWrite(data[d].bluePin,LOW);//turn on the LED
pinMode(data[d].sensorPin, INPUT_PULLUP);
data[d].active = true;
}
}
void loop()
{
for (int d = 0; d < 3; d++)
{
if (data[d].active)
{
if (digitalRead(data[d].sensorPin))
{
digitalWrite(data[d].redPin, LOW);
digitalWrite(data[d].greenPin, LOW);
digitalWrite(data[d].bluePin, 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(6); //Play the first mp3
}
}
}
}

This is a BIG hint to get quicker and better answers

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

No.

You need to have 11 initial values for each of the 9 dataLayout structs in your data array.

Each of the 9 lines should have 11 values. Obvsly I cannot know what you want, but at the very least the “true” has to be last.

Like

dataLayout data[9] =
{
    {A3, 7, 3, 4, 5, 6, 7, 8, 9, 10, true},

&c.

Each of the 9 things in the data array needs 11 initial values. Obvsly I cannot know what your intent is, so I just put integers in there.

You seem confused as what you have added is more variables, all with the same name as the members of your struct. They have nothing to do with the space you have reserved (and attempted to initialize) for nine dataLayout structs in the data array.

This is a recipe for confusion, I am confused myself.

SO CONFUSED I was commenting about your first code, sry. Removing those lines was a good step. But you still miss my point about the struct array initialization.

I suggest you learn more about structures and arrays before you try getting this code to function.

a7

Could you please Put a full example of the code? I am a little confused on what I need to put and where I need to put them.


#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 redPin;
    byte greenPin;
      byte bluePin;
        byte redPin2;
    byte greenPin2;
      byte bluePin2;
        byte redPin3;
    byte greenPin3;
      byte bluePin3;
  boolean active;
};

dataLayout data[9] = 
{
  {A3, 7, true},
  {A3, 6, true},
  {A3, 5, true},
    {A2, 4, true},
  {A2, 3, true},
  {A2, 2, true},
    {A1, 30, true},
  {A1, 32, true},
  {A1, 34, true},
};


void setup()
{
    mySoftwareSerial.begin(9600);
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."));
    
    mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 10; d++)
  {
    pinMode(data[d].redPin, OUTPUT);
        pinMode(data[d].greenPin, OUTPUT);
            pinMode(data[d].bluePin, OUTPUT);
    digitalWrite(data[d].redPin, LOW); 
        digitalWrite(data[d].greenPin, HIGH);
            digitalWrite(data[d].bluePin,LOW);//turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}
void loop()
{
  for (int d = 0; d < 9; d++)
  {
    if (data[d].active)
    {
      if (digitalRead(data[d].sensorPin))
      {
        digitalWrite(data[d].redPin, LOW);  
                digitalWrite(data[d].greenPin, LOW);
                        digitalWrite(data[d].bluePin, 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(6);  //Play the first mp3
      }
    }
  }
}

Remember I have 3 rgbs, and I want them to change from a purple color to a white color. Just so you don't get confused on what I want out of this

Hello
at least you have to implement one struct to collect all data and information wrt the RGB-led in an object. Avoid to include members to this object that as no relationship to the object, eg control buttons etc.

struct RGBLED {
  byte pinRed;
  byte pinGreen;
  byte pinBlue;
} rgbLed [] {
  {redPin,greenPin,bluePin},
  {redPin2,greenPin2,bluePin2},
  {redPin3,greenPin3,bluePin3},
};

You appear to be ad-libbing off some code you found somewhere to convert it from 1 RGB LED to 3. But you have no clue how it works, arrays or structs. So you are failing.

Post the original code you are trying to enhance as you found it, presumably working with 1 LED.

Did you get the original code working with 1 LED as intended?

Have you tried to learn about structs and arrays?

You can’t fake this stuff. You can’t expect anyone on this section of these fora to write your code.

Post the original code and ask questions about anything you can’t figure out between spending some time with it and your preferred learning sources for C/C++.

a7

Yes i did. It worked just fine. But i have not learned or tried to learn about structs and arrays. It was the solution to the problem I had earlier so i decided to just learn the more i go further. here is the code that worked with One led.


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

int redPin= 7;
int greenPin = 6;
int bluePin = 5;

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

struct dataLayout
{
  byte sensorPin;
  byte redPin;
    byte greenPin;
      byte bluePin;
  boolean active;
};

dataLayout data[3] =
{
  {A3, 7, true},
  {A3, 6, true},
  {A3, 5, true},
};


void setup()
{
    mySoftwareSerial.begin(9600);
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."));
    
    mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  while (!Serial);
  for (int d = 0 ; d < 4; d++)
  {
    pinMode(data[d].redPin, OUTPUT);
        pinMode(data[d].greenPin, OUTPUT);
            pinMode(data[d].bluePin, OUTPUT);
    digitalWrite(data[d].redPin, LOW); 
        digitalWrite(data[d].greenPin, HIGH);
            digitalWrite(data[d].bluePin,LOW);//turn on the LED
    pinMode(data[d].sensorPin, INPUT_PULLUP);
    data[d].active = true;
  }
}
void loop()
{
  for (int d = 0; d < 3; d++)
  {
    if (data[d].active)
    {
      if (digitalRead(data[d].sensorPin))
      {
        digitalWrite(data[d].redPin, LOW);  
                digitalWrite(data[d].greenPin, LOW);
                        digitalWrite(data[d].bluePin, 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(6);  //Play the first mp3
      }
    }
  }
}

Thanks, I will look at it after life stuff.

One quick thing, this

for (int d = 0 ; d < 4; d++)

will reference things past the end of your array, it is a mistake. It may not be immediately consequential, but should be fixed (3, not 4) before tinkering further as it is the kind of error that waits around to grab you when you’ve moved on to other things…

L8R

a7

Thank you. And I understand about your "Life stuff". Take your time. and I will try to figure it out

Whilst rummaging around I noticed you intend to have

30 RGB LEDs and
30 sensors

If implemented in a manner at all resembling the code(s) you have posted you would need 120 I/O pins, which AFAIK is without the capabilities of any Arduino unless you add hardware assistance.

It is time to scrap all that code, full stop.

It is a mess. I srsly doubt it ever did anything, it cannot be something you found that was promoted as complete and functional.

Describe your entire project as best you can in general terms and we can help with the thinking about the software and hardware you will need to succeed.

I'd usually sleep on a draft and review it when it isn't tired and I'm not so late. I just don't want to see you wasting time on this approach

a7

Well I do know that that it would require a lot of pins. However that is why I am using more than one arduino mega. 1 for the sensors and 2 for the lights. And I am also using bread boards so the 5v and gnd can be transmitted through all the lights and sensors. When I said I am a noob, I am a noob. This (to me) seems like a good way to start coding and electrical engineering, which are the degrees I want when I graduate, Even if it ends up not being possible (Which I doubt since arduino can do many advanced things.)

Edit: Question

Is it possible to hook up sensors to one arduino and use those to activate The rgbs on the other arduinos

OK, I see.

I would advise you to slow your roll and stop wasting your time and ours. It is not yet time for you to present code and ask for help getting it to work.

Try a few simpler projects or work through some of the examples code in the IDE.

Find an online C/C++ and Arduino study guide matching your learning style. Ppl like youtube series, I prefer old fashioned books. Whatever. You are going to need to do some work and get some experience before you can proceed with your ultimate goal, which hasn’t been made really clear. Why not tell us what, in your wildest dreams, you hope to produce here? Then our advices can steer you to hardware and software solutions that might make sense.

And please go no further with your thinking about multiple Arduinos talking to each other so you have enough pins for you LEDs and sensors and take @jimLee’s implied suggestion:

Neopixels or other so-called “smart” pixels will make this much easier. Even if you do have to learn about one more thing or two…

Edit: Answer

Of course it is. Do yourself a favor and read a few dozen posts on the “project guidance” and “programming questions” sections of these fora and you will see wonderful ambitious projects doing all kindsa crazy stuff.

But srsly, until we know more this is nothing one Arduino even not a mega could handle. You will see.

CU

a7

Ok I understand. Just to put it simply, It will be a pac man type of game. The monster will be invisible and when you step over the monster it will chase you. that kind of thing. But yes, I will slow my roll.

Edit:

For your information Though, the reason I am using the whole "(int d = 0 ;)" Stuff, is because i tried to test it with normal leds and not rgb ones. And this is the code that did work for 4 normal leds.


#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.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."));
    
    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(8);  //Play the first mp3
      }
    }
  }
}

I have a question

Lets say i put in a simple string of code to turn on an led, very simple code. And I also have a 9vlt Battery put in. If I unplug the arduino from the computer, will the code stay on the arduino because of the 9 volt battery plugged in? Or will you have to keep it plugged into a computer for it to work

A usual 9V Battery has a capacity of 180 to 220 mAh. An Arduino Uno will pull a minimum of 10mA. This means your battery will be empty after 220 mAh / 10 mAh = 22 hours of use. Each LED light up will add another 5 mA to 20 mA.

Not that much. I reommend using a wallplug for supplying your microcontroller.
There is also a limit for how much current you can pull from a single IO-pin and how much current you can pull from the chip as a whole. It will take time to learn these things. You can invest the time in two different ways

way 1: hurrying up wirng up complex stuff with no knowledge trying to proceed fast destroying your microcontroller-board multiple times through lack of knowledge
needing time to wait until a new ordered board arrives

way 2: taking time to learn the basics about the electrical part of microcontrollers then really knowing how to connect external hardware without destroying your microcontroller-board.

Which way do you prefer?

Here is a website with a lot of tutorials about electronics and coding for arduino

and here is the programming course
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Thank you! Can you send me a link to a wall plug to power arduino on amazon? It would really help if you do, So I know what I am looking for.

Amazon, is not the best choice for electronic parts.

What will be a good choice depends on how much current your full developed project will need. RGB-Leds are available with different powerage.

For an amount of 30 LEDs Neopixels where all LEDs are controlled through a single wire would be a good choice.

You have this idea of a pac-man-game. Great project. But you seem to have only a very little knowledge about electronics and programming.

If you don't find a ready to use project that fits your specs 99% there is no way around learning quite some programming and electronics. You will learn this for shure with the help of the forum. But it will become only reality through you learning programming and electronics.

best regards Stefan