Coding problem with LED/PIR/SERVO

Hi all,

Thanks for opening up this thread.

I'm building a project with PIR sensor / 2 LED ring / Continuous servo

below is the code that I'm using, and the circuit for reference as well

#include <Adafruit_NeoPixel.h>
//#include <VarSpeedServo.h> 
#include <Servo.h> 

#define PIN            10

#define NUMPIXELS      24

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//VarSpeedServo myservo;
Servo myservo;

int pos = 0;
int brightness = 0;
int int_flag = 0;

int val = 0;
int cnt = 0;

const int servoPin = 9;

void setup() {
  
  pixels.begin(); // This initializes the NeoPixel library.
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  //myservo.write(0,255,true);
  myservo.write(pos);
  attachInterrupt(digitalPinToInterrupt(2), int_pir, CHANGE);
  
//  for(int k=0;k<255;k++)
//  {
//    one_funtion();
//  }
}

void loop() {
//one_funtion();
  if(int_flag == 1){
    
    for(int m=0;m<255;m++){
       one_funtion();
    }
    int_flag = 0;
    cnt =0;
  }
 
}

void int_pir(){
  cnt++;
  if(cnt == 1){
    int_flag = 1;
  }
}

void one_funtion(){

  brightness++;
  pixels.setBrightness(brightness);
  val = map(brightness, 0, 255, 0, 180);
  //myservo.write(val,10);
  myservo.write(val);
  delay(15);
  
  for(int i=0;i<NUMPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
  
    if(brightness == 255){
      //brightness = 0;
      for(int j=0;j<255;j++){
        brightness--;
        pixels.setBrightness(brightness);
        val = map(brightness, 0, 255, 0, 180);
        //myservo.write(val,10);
        myservo.write(val);
        delay(15);
        
        for(int i=0;i<NUMPIXELS;i++){
          pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.
          pixels.show(); // This sends the updated pixel color to the hardware.
        }
        delay(10);
      }
    }
    
  delay(10);
}

my logic for this project is,

it not works normally - someone appears - motion sensed by PIR - 2 led ring and servo runs at the same time - for 10 seconds, it gradually get faster and brighter but when it reaches 10sec, servo will get slower gradually and stop - than from 11sec servo turns backward and gradually get faster and led ring starts to get weaker- on 20sec light will be out and servo will gradually slow down and stop
so.. one loop is 20 seconds

when pir does not sense any thing it will not run anything, but once sensed, it runs for only one loop which is 20 seconds.

seems quiet complex but, basically i want to synchronize led ring and servo.

Thanks guys! and happy new year :slight_smile:

You've got multiple variables ccalled "flag", some with very limited scope.
You probably don't want that.

Hi AWOL,

Thanks for reaching out,

can u be more specific about it?

Sorry, on my phone, I couldn't see the uunderscores.

Here's the image attached to the top post.

How to insert image.

I couldn't find a question in the original post. Was there a question?

First thing I noticed in the above diagram is all the LEDs and the servo are powered from the Arduino. The Arduino's 5V regulator isn't intended to source a lot of current. You should be using a separate 5V supply for the servo and NeoPixels.

leeepd:
. . .
seems quiet complex but, basically i want to synchronize led ring and servo.

I've done a few servo projects with the Arduino, and my strategy has been to control the servo directly at 50Hz. Don't rely on the servo library to move the servo as some speed, set the speed yourself.

Here's one recent servo project. The servos are moved with a constant acceleration. IMO, this gives the servos much smoother motion than just setting the desired end position. The "VarSpeedServo" library will let you control the speed which the servo moves, but if you're accelerating the servo, the servo will rarely have the same speed two refresh cycles (20ms) in a row.

I think you're better off just using the normal servo library and controlling the speed and position of the servo directly. The video I linked above has a link to the source code in the description. You could use the same acceleration algorithm in your project.

Hi duanedagn

Thnx great for your response.

I really appreciate it.

One thing i wonder is how do i separate 5v?

And also i think the link u gave which has servo speed thing is broken. Can u give it again?

Thanks!

leeepd:
One thing i wonder is how do i separate 5v?

You use a 5V power supply. Either one which plugs into the wall or a battery pack with a regulator which can output 5V.

Adafruit has a NeoPixel Uberguide with lots of great information about powering NeoPixels.

leeepd:
And also i think the link u gave which has servo speed thing is broken. Can u give it again?

Sorry about the link. It should be fixed. I just noticed you had the library commented out in your code.