HELP NEEDED! Issues with PIR motion sensor with two servos/RGB LED lights.

Hi.

I’m a third year model maker about to hand in my two final major projects, one of which has a lot of coding that I admittedly don’t have a huge amount of experience with. I am crating an alien styled containment crate that has a blue pulsing light and when the motion sensor is triggered changes the lighting red and causes two servos to be triggered.

I have managed to get the motion sensor working with 14 LED strip lighting, but the moment I added an additional 12 it seemed to break the coding. The are soldered together with enough space for them to fit where needed.

The code I’m using is a mixture I found from these two websites that I was able to string together.

-http://makezine.com/projects/pir-sensor-arduino-alarm/
-Tweaking4All.com - Arduino - LEDStrip effects for NeoPixel and FastLED

I am using an Arduino UNO board, adafruit RGB LEDs and the mini PIR HC-SR505 Infrared Motion Sensor

The other issue I am having is I cannot get the same motion sensor coding work work with this coding I found for the motion. I like the way this coding is laid out as it allows me easy control of the motion.

I am using another Arduino UNO board, two Futaba S3003 and another mini PIR HC-SR505 Infrared Motion Sensor or the same one if I can splice the cables correctly as I need the lighting and servos to go off together.

I have spent weeks in between making the crate and the other sculpting project I have am an now running out of time. Any help will be beyond appreciated and any help will be credited in my research document.

A huge thank you in advance.

Here are a couple of links to videos I have taken of them working. (lighting without the additional 12 LEDs which seem to brake it and the two servos working without the motion sensor)

Lights - https://youtu.be/SC7Jz5MWhfM
Servos - https://youtu.be/fEHsYCK6pio

Motion_sencor_blue_to_red_led_code.ino (2.51 KB)

Motion_sensor_servo_1.ino (1006 Bytes)

Are either of the two programs you posted the "broken" one using the 12 additional LEDs ? If not, then please post it. The problem sounds like it may be caused by misusing an array index but without the code, who knows ?

The LED code I posted was the wrong code (thanks for noticing), the only difference is the number of LED’s.

This is the code I am using for the 26 LEDs

 #include <Adafruit_NeoPixel.h>
#define NUM_LEDS 26 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(26, 10, NEO_GRB + NEO_KHZ800);
 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
   if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
   FadeInOut(0xa6, 0x0c, 0x0c);
   delay (10);

    
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
    FadeInOut(0x05, 0xac, 0xff);
      if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
void FadeInOut(byte red, byte green, byte blue){
  float r, g, b;
      
  for(int k = 0; k < 256; k=k+1) { 
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
    delay (10);
  }
     
  for(int k = 255; k >= 0; k=k-2) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
    delay (10);}
}
void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < 26; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

This is the broken code for the lighting. The servo code works, it I simply can’t figure out adding in the motion sensor.

Thanks

Adafruit_NeoPixel strip = Adafruit_NeoPixel(26, 10, NEO_GRB + NEO_KHZ800);
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

You seem to be using pin 10 for both the NeoPixels and a speaker, or have I misunderstood what you are doing ?

I simply didn’t rename the speaker to the LEDs. Sorry my bad. I’m very new to all this and guess I was lazy with changing Pin 10 name as I was just editing the existing code to suit my needs. There is no speaker involved. Sorry for the confusion.

Thanks

I thought I would change the coding a little for it to make a little more sense. The issues with me not being able to get the motion sensor working with the two servos is the same but the issues with the LEDs I can give a little more information on.

Even when I cut the LED number back down to it’s original number, the motion sensor and LEDs seem to be running in a pattern and not only changing when triggered by the motion sensor.

If you have anything that might help solve this solution I would be more than grateful for. This is becoming a very frustrating issue for me.

Here it the corrected coding.

#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 26
Adafruit_NeoPixel strip = Adafruit_NeoPixel(26, 10, NEO_GRB + NEO_KHZ800);
 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinled = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinled, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
   if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
   FadeInOut(0xff, 0x00, 0x00);
   delay (10);

    
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
    FadeInOut(0x00, 0x00, 0xff);
      if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

void FadeInOut(byte red, byte green, byte blue){
  float r, g, b;
      
  for(int k = 0; k < 256; k=k+1) { 
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
    delay (10);
  }
     
  for(int k = 255; k >= 0; k=k-2) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
    delay (10);}
}
void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < 26; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

Thanks for your time.

class Adafruit_NeoPixel {

 public:

  // Constructor: number of LEDs, pin number, LED type
  Adafruit_NeoPixel(uint16_t n, uint8_t p=6, neoPixelType t=NEO_GRB + NEO_KHZ800);

You are STILL defining another use for the pin that the strip is attached to. Why?

Honestly…not sure. I came with the original code and being new at this I was a little timid when it came to removing parts of the code. I hate to be a pain. The code you added I can’t seem to find located in my last message code.

I’ve attached a few photo if it helps. I have also added a few images of the project I’m making. I’m not sure if this will help you understand any better what I’m trying to accomplish. The code in my last massage was the one I was using during the short video I added to my first post but it simply doesn’t seem to work now. Sorry for the rough assembly. I don’t want to fully fit them together until I know it’s working correctly.

I re-tested the lights without the additional 12 LEDs but it skill don’t want to work…that’s why some of the LEDs are not attached in the photo.

My apologizes if I’m coming across a little dim. Honestly I haven’t slept in a while trying got get my projects together in time and all the paper work finished off and work out how to get the servos moving with the motion sensor. My fault for taking on two complicated projects as a pose to just one. Guess I was trying to kill myself off before graduation.

Best,

I un-soldered and re-soldered all of the connection and have managed to get the code working ok with the 26 LEDs. So having fixed that it seem I only have to worry about getting the motion sensor to work with the two servos. If you guys have any suggestion for that it would be hugely appreciated.

Thank you for your time.

Best

I only have to worry about getting the motion sensor to work with the two servos. If you guys have any suggestion for that it would be hugely appreciated.

Just what do you want the servos to do when there is motion? Or is is when motion begins? When there is no motion? Or is it when motion ends?

You know that there will be a huge lag between motion ending and the sensor admitting that, right?