not sure if this is the place to ask but how many events can you put into a pir to trigger for eg i want to trigger a dfplayer with neopixels does anyone know of a sketch that can outline how to incorp things you want to happen in a loop using a PIR? i have a working light script that triggers with pir and i have a sketch that triggers and plays an mp3 i want to put them together so i can have the mp3 trigger with the lights and turn off and be re triggered
Example Projects | PIR Motion Sensor | Adafruit Learning System
As the PIR represents 1 trigger, just create functions to do individual tasks and call each from loop() remembering that if your PIR state is global, all functions see the state, but if the PIR state is local to loop, you must pass that variable in the function calls.
You can trigger as many events as are limited by the size of the controller's memory.
thanx i'm kinda on the hunt for something more complex the reason is i'm trying to get a sketch to work and not sure how to write the code to have not just neopixels but also and mp3 player i have made two seperate sketches one for lights and one for mp3 but for the life of me i can't work out how to join them to one sketch to have the mp3 work with the pixels
thats what i figured but there must be a certain way to write it so it does what you want it to do?
There are lots of way to write it to do what you want it to do, but first you have to define what those things are.
PIR has 2 states: On, Off ... just like a switch/button
Make that as complicated as necessary to fulfill your requirements.
i think the simplest would be is to put the sketch here and explain what i mean and what trouble i'm having
as my last post i would be better to post the sketch and run through what the issue is
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define PIN 4
#define NUMPIXELS 8
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int delayval = 5000;
int sensor = 2;
int state = LOW;
int val = 0;
// setup() function -- runs once at startup --------------------------------
void setup() {
Serial.begin(115200);
Serial.println("Setting up software serial");
mySoftwareSerial.begin(9600);
pinMode(sensor, INPUT);
pinMode(PIN, OUTPUT);
strip.begin();
strip.show();
Serial.println("check dfplayer");
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("DFPlayer error."));
//while (true);
}
Serial.println("Setting up software serial —- done");
}
// loop() function -- runs repeatedly as long as board is on ---------------
void loop() {
val = digitalRead (sensor);
if ( val == HIGH){
if(state == LOW){
state = HIGH;
for ( int i=0;i <NUMPIXELS; i++){
strip.setPixelColor(i,strip.Color(0,255,255));
strip.show();
digitalWrite(sensor, HIGH);
delay(30);
Serial.println("Motion detected!");
// We only want to print on the output change, not state
state = HIGH;
//—-Mp3 play—-
Serial.println("Setup mp3 player settings");
myDFPlayer.volume(30);
myDFPlayer.setTimeOut(200); //Set serial communictaion time out 500ms
Serial.println("—– playMp3 —–");
myDFPlayer.play(1);
delay(500);
}
}
}
else {
for ( int i=0; i <NUMPIXELS; i++){
strip.setPixelColor( i, strip.Color (0,0,0));
delay(30);
strip.show();
}
}
}
the issue is it's working but only on hard reset if i try to put the mpplayer part anywhere else in the loop to trigger it doesn't play
the neopixel.h file i missed in the copy
I am going to respond generally, not specifically:
The loop(){ function should not have lots of extraneous iffy code; called functions should work outside loop() which is to say those routines should be independent functions.
Ex:
void loop(void)
{
Verbose = (!digitalRead(VerbosePin)); // IF pin is LOW, Verbose is TRUE, else FALSE
if (!digitalRead(forcePARISpin)) Paris();
ReadMorseKeyState();
char temp = MagicMorse();
if (temp != 0) {
if (++ConsoleCount >79 ) {
Serial << endl;
ConsoleCount = 1; }
SendMorseLCD(temp); }
} // loop()
SendMorseLCD() only requires that loop sends a single character in the function argument. Everything else that happens in loop is indepent of the LCD.
Adding another function is now easy.
i get what you are saying in real terms i need to make a function and call that small function in the loop the other alternative i was going to do was to have the mp3 player in setup and add a trigger to a relay to power up the mp3 to play leaving the loop with just the lights
I'd say to need to define what it is you want to do in unambiguous language, and ignore how you think you're going to implement it, for now.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.