Hi all,
Brand new here. Very new to Arduino.
I'm trying to help my son build a little project where an LED strip (WS2812B) has animations activated by a motion trigger (HCSR-04).
We can get the two separate functions to work on an Arduino Uno separately, but I just don't seem to be able to combine them both into one sketch.
I'm really new to C++ and just seem to be confusing myself endlessly.
Any help would be massively appreciated.
Here's my code thus far. I've tried to comment as much as I can so it's easier to understand. Also, the last action (to fade out LEDs 3 mins after last motion detected, I haven't got yet.
#include <FastLED.h>
// The Art Illuminator 5000 LED controller
// Initial code component uses the HC-SR04 to detect motion and trigger the LED sequence
// Second component uses FastLED to control the WS2812B LED Strip to produce required illuminations
// Definitions MOTION
// Definitions ILLUMINATION
#define DATA_PIN 2
//#define CLK_PIN 4
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 78
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 120
// MOTION SENSING
// define pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 3;
// define variables
long duration;
int safetyDistance;
int distance;
int pinStateCurrent = LOW; // current state of pin
int pinStatePrevious = LOW; // previous state of pin
void setup() {
// MOTION SENSING
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT); // Sets ledPin as output to trigger illumination
Serial.begin(9600); // Starts the serial communication
// ILLUMINATION
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
//SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
SimplePatternList gPatterns = { sinelon, illuminate };
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
void loop() {
// MOTION SENSING LOOP
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
safetyDistance = distance;
if (safetyDistance <= 300) { //distance (cm) of sensor coverage
digitalWrite(ledPin, HIGH); //turn on LED
}
else {
digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(400);
// LED ILLUMINATION LOOP
//
pinStatePrevious = pinStateCurrent; // store old state
pinStateCurrent = digitalRead(ledPin); // read new state
if (pinStatePrevious == LOW && pinStateCurrent == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion detected!");
// Trigger illumination sequence ... here
gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000 / FRAMES_PER_SECOND);
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) {
gHue++; // slowly cycle the "base color" through the rainbow
}
EVERY_N_SECONDS( 10 ) {
nextPattern(); // change patterns periodically
}
}
else if (pinStatePrevious == HIGH && pinStateCurrent == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion stopped!");
// Wait 3 mins then fade out Illumination ... here
delay(4000);
}
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
// Illumination Patterns
void nextPattern() {
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void sinelon() {
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
leds[pos] += CHSV( gHue, 255, 192);
}
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds, NUM_LEDS, 20);
uint8_t dothue = 0;
for ( int i = 0; i < 8; i++) {
leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}
void illuminate() {
// all LEDs illuminated for the artwork to be seen
fill_solid( leds, NUM_LEDS, CRGB(127, 127, 127));
}