So I am working on a big personal project, essentially I have a sensor checking when someone is present, and if someone is present then it will activate a series of actions, one being playing sounds, and the other controlling lights. From this point, I have to use millis() to emulate multi-tasking, Here comes some of my confusion, I need the sensor to run most of the time, though when the music and lights are activated its fine if the sensor is idling, but I need the music and light to run at the same time. Hardware-wise I am using an Arduino Uno, I have access to multiple of them, Adafruit's Soundboard and a spool of WS2812B LED strips.
The code below behaves in the following manner, whenever the loop of the LED lights ends it can scan with the sensor, but if the music starts playing the lights will not turn on, since the loop is not running.
I am currently trying to use millis to replace the delays in the LED for loops, but I'm running into some issues, I want to run the code in the for loop (turn the light on) then wait a few milliseconds before continuing the for loop, any ideas or guidance anyone could give.
If I cannot run multiple things at the same time purely, what else can I do to be able to have the audio and lights running together (the sensor can be idling during this time). I am thinking of connecting another Arduino and communicating via serial port to achieve this multi-tasking, or is there a better way to do this?
I will try to join some functions together and see if I can achieve some sort of compromise, I will also be updating my code since this is my current stable build. Once I fix some things on the project I will put a more organized code. Thank you for your time.
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
#include <FastLED.h>
#define LED_PIN 12
#define NUM_LEDS 100
int numLED = 99;
CRGB leds[NUM_LEDS];
// Choose any two pins that can be used with SoftwareSerial to RX & TX
#define SFX_TX 5
#define SFX_RX 6
// Connect to the RST pin on the Sound Board
#define SFX_RST 4
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
int trigPin1=9;
int echoPin1=7;
#define NUM_SOUNDS 2
// The names of the found files.
// Note that they are 8 characters followed by a 3 character file
// type (.OGG or .WAV). Spaces are inserted to make up the
// 8 characters as needed.
char *soundName[NUM_SOUNDS] = {
"T00 OGG",
"NewItem WAV"
};
// these lengths are less than they really are due to the
// time it takes to get to the code where the delay() is done
int soundLength[NUM_SOUNDS] = { 1240, 1210};
// offsets of the sounds into the above arrays
#define SOUND_ITEM 0
#define SOUND_RAIN 1
int currentSound = 0;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
ss.begin(9600);
}
void loop() {
//LEDs
for (int i = 0; i <= numLED; i++) {
leds[i] = CRGB (0, 0, 255);
FastLED.show();
if (i > 40 && i < 60) {
leds[i] = CRGB (50, 0, 255);
}
//leds[i] = CRGB (20, 20, 200);
delay(20);
}
for (int i = numLED; i >= 0; i--) {
leds[i] = CRGB ( 0, 0, 0);
// leds[i] = CRGB ( 100, 100, 255);
FastLED.show();
delay(5);
}
//Sensor and Speaker functions
long duration1, distance1;
digitalWrite(trigPin1, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
if (distance1 >= 500 || distance1 <= 0){
Serial.println("Out of range");
}
else {
Serial.print ( "Sensor1 ");
Serial.print ( distance1);
Serial.println("cm");
if (distance1 < 30) {
if (! sfx.playTrack("T01 WAV")) {
Serial.println("Failed to play track?");
}
delay(soundLength[currentSound]);
currentSound++;
if (currentSound == NUM_SOUNDS)
currentSound = 0;
}
}
delay(50);
}
