Hi everyone, I'm working on a project that uses a WS2182b 120 LED strip, an ultrasonic sensor and an Arduino Uno.
The sensor is used to determine how many times the hand is within 5 cm of the sensor, and when this number reaches 2 it will implement a sequence that steadily increments the leds 1 by 1 up the strip for 4 seconds, remain completely lit up for 7 seconds before turning off backwards down 1 by 1 for 8 seconds, all using the same colour. So far it kinda works, but I've encountered 2 main problems.
-
Sometimes the sensor doesn't seem to recognise that my hand is in the range, or it completely skips the first case and goes directly to the second.
-
When entering the 3rd case (when the counter = 2) for the first time it rapidly increments and decrements the leds several times before incrementing it, flashing between 2 colours (the one I've chosen and a pink) for about 10 seconds before decrementing again. The 2nd scenario then repeats. I've managed to make it work using delays and slightly altering the Cylon example in the FastLED library, but when switching to millis() it doesn't seem to function the way I want it to.
I'm quite new to Arduino, so I apologise in advance if there are some really simple things that I've overlooked! Any help or guidance will be greatly appreciated, thank you
This is the code that I'm using:
#include <FastLED.h>
//define the time intervals
#define BREATH_IN 2000
#define HOLD_BREATH 6000
#define BREATH_OUT 13000
unsigned long time_1 = 0;
unsigned long time_2 = 0;
unsigned long time_3 = 0;
//set up LED
#define DATA_PIN 3
#define NUM_LEDS 120
CRGB leds[NUM_LEDS];
// set up sensor
#define TRIG_PIN 12
#define ECHO_PIN 13
//In centimeters
#define MIN_DISTANCE 3
#define MAX_DISTANCE 25
//calculates ultrasonic sensor distance
float duration, distance;
float dist_div = (MAX_DISTANCE-MIN_DISTANCE)/NUM_LEDS;
void setup()
{
FastLED.addLeds<WS2812, 3, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
//set up strip
Serial.println("resetting");
LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
LEDS.setBrightness(30);
Serial.begin (9600); //start serial monitor for debugging if needed
}
void loop()
{
static uint8_t hue = 0;
static int counter = 0;
Serial.print("x");
//set up ultrasonic sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration*.0343)/2;
Serial.println(millis());
delay(1000);
if (distance < 5)
{
counter ++; //if the hand is within 5cm of the sensor, increment the counter by 1
Serial.print (counter);
}
if (counter > 3)
{
counter = 0;
}
switch (counter)
{
case 0:
{
FastLED.clear();
Serial.print(counter);
}
break;
case 1: //commence tactile interface pattern
{
fill_solid(leds, 120, CHSV (32,200,200)); //green
Serial.print(counter);
}
break;
case 2: //commence 4-7-8 breathing pattern
{
if (millis() >= time_1 + BREATH_IN)//start breath in sequence
{
time_1 += BREATH_IN; //update time
for(int i = 0; i < NUM_LEDS; i++) // increment led strip one led at a time
{
leds[i] = CHSV(128, 150, 200); //set the colour to aqua
// Show the leds
FastLED.show(); //show the leds
}}
if (millis()>= time_2 + HOLD_BREATH) //start the hold in sequence
{
time_2 += HOLD_BREATH; //update time
fill_solid(leds, 120, CHSV(128,150,200)); // fill the entire strip with one colour
// Show the leds
FastLED.show();
}
if (millis()>= time_3 + BREATH_OUT)//start the breath out sequence
{
time_3 += BREATH_OUT; //update time
for(int i = (NUM_LEDS)-1; i >= 0; i--)
{
leds[i].nscale8(256); //set the leds to black, incrementing backwards 1 led at a time
FastLED.show();
}
}
}
break;
}
FastLED.show();
}