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.