Unsure if this has been addressed (as I have looked, albeit not deeply, into similar topics. I have two separate things I am trying to do from the same call time. When the PIR is activated, I am wanting the Servo process to start (and the red and blue LEDs to activate at the appropriated times) and also the NeoPixel LED strip to start. They both work, just the Strip will start after the red LED goes off. When I try to rearrange the coding to accommodate both, I get definition errors.
As I am fairly new to coding terms, is there a way to have both the Servo/DigitalWrite process and the ColorWipe/Fade of the Strip to both activate at the same time when the PIR Sensor state ==1?
If I have posted the code wrong, please be gentle as I thought I followed protocol by using the appropriate item above.
// C++ code
//
#include <Servo.h>
#include <Adafruit_NeoPixel.h>
//Which pin is the Servo connected
Servo servo_9;
// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN 11
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 20
// Fading interval
#define ledFadeTime 5
//Initial PIR Sensor state
int pirState = 0;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pinMode(4, INPUT); //PIR Sensor
pinMode(5, OUTPUT); // Blue LED
pinMode(6, OUTPUT); // Red LED
servo_9.attach(9, 0, 180); // Servo motor
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
}
void loop()
{
pirState = digitalRead(4); //PIR is idle
digitalWrite(5, LOW); //Blue LED off
digitalWrite(6, LOW); //Red LED off
if (pirState == 1) // PIR is triggered
{
servo_9.write(180); //servo goes in motion CW
digitalWrite(6, HIGH); //Blue LED is on
delay(2000); // Wait for 2000 millisecond(s)
servo_9.write(0); //servo goes in motion CCW
delay(2000); // Wait for 2000 millisecond(s)
servo_9.write(180); //servo goes in motion CW
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(6, LOW); // Blue LED is OFF
digitalWrite(5, HIGH); // Red LED is ON
servo_9.write(0); //servo goes in motion CCW
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(5, LOW); // Red LED is OFF
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
// Fill along the length of the strip in various colors...
colorWipe(strip.Color(255, 100, 0), 100); // Amber
delay(500); // Pause for a moment
rgbFadeInAndOut(0, 160, 255, ledFadeTime); // Blue
//colorWipe(strip.Color( 0, 165, 255), 50); // Light Blue
}
}
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
for (uint8_t b = 0; b < 255; b++) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
};
for (uint8_t b = 255; b > 0; b--) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
};
for (uint8_t b = 0; b < 255; b++) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
};
for (uint8_t b = 255; b > 0; b--) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
};
strip.setBrightness(200);
for (uint8_t b = 0; b < 255; b++) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
};
};