I want to make a remote to control a light array. I want 6 modes: pattern 1, pattern 2, pattern 3, accelerometer controlled, always on, and off. I am using two HC-05 Bluetooth modules to control the system. I want to be able to send a signal with the remote, the remote turns off after I've sent the message to save power, and be able to switch modes at any time (ie, if it's in the middle of the first pattern, I can still switch to accelerometer). I have tested every mode and bluetooth, and they all work independently. Bluetooth also tested with most modes. However, when I put it all together, it seems to have issues. My Bluetooth, the on in the light system, repeats the signal continuously in the serial monitor during debugging, even though this did not happen before. I can select one mode but can't switch until I turn off the system to fix it. I'm not sure if the mode switches are possible, because I know I'm trying to access the BTSerial data in the middle of the loop. I tried to fix this with a while loop, but after trying several different variations, I can't seem to get it to switch when I press a new button. I am pretty new to Arduino, so I'm not sure how to write "continue mode if number is not #2, #3, etc." into the code, or if a while loop is what should be used in this situation. I was running the mode changes off of this tutorial: The correct way to use buttons with FastLed library, Arduino and WS2812 NEOPIXEL - YouTube
I think a big difference that's causing issues is the fact that my functions are running continuously, while the videos I think ends the loop even while the lights continue, which is why I switched the code around a bit.
Here are the fritzing diagrams of the light and remote systems, and both their codes. I have a 2K resistor on the HC-05 voltage splitter section, but couldn't find 2K in fritzing, so a 2.2K resistor is noted. I also plan to expand light patterns 2 and 3, so those are just place holders until I can make sure the system works.
Light System Code:
```cpp
#include <Adafruit_Sensor.h>
#include <Adafruit_MMA8451.h>
#include <Wire.h>
#include <FastLED_NeoPixel.h>
#include <SoftwareSerial.h>
//Setup Bluetooth
SoftwareSerial BTSerial(11, 12); //RX,TX
int micdata = 0;
//Accelerometer
Adafruit_MMA8451 mma = Adafruit_MMA8451();
#define MAX_G 3.0
#define MAX_G_RESULTANT 3.0
#define EVENTS_LIMIT 3
#define SAMPLE_RATE 5000
#define CHK_DEADTIME 100
#define LOGGING_RATE 100
#define LED_ONTIME 5000ul
#define LED_IDLE 0
#define LED_TIME 1
char
szStr[50];
float
xg_f,
yg_f,
zg_f,
xg_sum,
yg_sum,
zg_sum;
unsigned long
sample_count;
bool
bGLimitExceeded;
//Setup Reedswitch
const int reed = 5;
//Neopixels
const int LAPin = 6;
const int RAPin = 7;
const int LLPin = 8;
const int RLPin = 9;
const int LALeds = 30;
const int RALeds = 30;
const int LLLeds = 30;
const int RLLeds = 30;
FastLED_NeoPixel<LALeds, LAPin, NEO_GRB> LAstrip;
FastLED_NeoPixel<RALeds, RAPin, NEO_GRB> RAstrip;
FastLED_NeoPixel<LLLeds, LLPin, NEO_GRB> LLstrip;
FastLED_NeoPixel<RLLeds, RLPin, NEO_GRB> RLstrip;
String but1 = "Oran";
String but2 = "Pecha";
String but3 = "Chesto";
String but4 = "Cheri";
String but5 = "Razz";
String but6 = "Pinap";
void setup() {
pinMode(reed, INPUT);
LAstrip.begin();
RAstrip.begin();
LLstrip.begin();
RLstrip.begin();
BTSerial.begin(38400);
Serial.begin(9600);
mma.setRange(MMA8451_RANGE_4_G);
bGLimitExceeded = false;
xg_sum = 0.0;
yg_sum = 0.0;
zg_sum = 0.0;
sample_count = 0;
}
void loop() {
if (BTSerial.available() > 0) { // Checks whether data is comming from the serial port
micdata = BTSerial.read(); // Reads the data from the serial port and store it in dataFromMaster variable
}
// Controlling the led
if (micdata == '1') {
LightPat1();
Serial.print(but1);
delay(150); // LED ON
while (micdata == !('2') || !('3') || !('4') || !('5') || !('6'))
;
} else if (micdata == '2') {
LightPat2();
Serial.print(but2);
delay(150);
while (micdata == !('1') || !('3') || !('4') || !('5') || !('6'))
;
} else if (micdata == '3') {
LightPat3();
Serial.print(but3);
delay(150);
while (micdata == !('2') || !('1') || !('4') || !('5') || !('6'))
;
} else if (micdata == '4') {
TakeSamples();
strike();
Serial.print(but4);
delay(150);
while (micdata == !('2') || !('3') || !('1') || !('5') || !('6'))
;
} else if (micdata == '5') {
flow();
Serial.print(but5);
delay(150);
while (micdata == !('2') || !('3') || !('4') || !('1') || !('6'))
;
} else if (micdata == '6') {
darkencheck();
Serial.print(but6);
delay(150);
while (micdata == !('2') || !('3') || !('4') || !('5') || !('1'))
;
}
}
void clear() {
LAstrip.clear();
RAstrip.clear();
LLstrip.clear();
RLstrip.clear();
LAstrip.show();
RAstrip.show();
LLstrip.show();
RLstrip.show();
}
void flash() {
flashcheck();
delay(125);
darkencheck();
}
void flashcheck() {
if (digitalRead(reed) == HIGH) {
LAstrip.fill(CRGB::White, 0, LALeds);
RAstrip.fill(CRGB::White, 0, RALeds);
LLstrip.fill(CRGB::White, 0, LLLeds);
RLstrip.fill(CRGB::White, 0, RLLeds);
LAstrip.show();
RAstrip.show();
LLstrip.show();
RLstrip.show();
} else {
LAstrip.fill(CRGB::Purple, 0, LALeds);
RAstrip.fill(CRGB::Purple, 0, RALeds);
LLstrip.fill(CRGB::Purple, 0, LLLeds);
RLstrip.fill(CRGB::Purple, 0, RLLeds);
LAstrip.show();
RAstrip.show();
LLstrip.show();
RLstrip.show();
}
}
void flow() {
if (digitalRead(reed) == HIGH) {
colorWipe(LAstrip.Color(CRGB::White), RAstrip.Color(CRGB::White), LLstrip.Color(CRGB::White), RLstrip.Color(CRGB::White), 20);
} else {
colorWipe(LAstrip.Color(CRGB::Purple), RAstrip.Color(CRGB::Purple), LLstrip.Color(CRGB::Purple), RLstrip.Color(CRGB::Purple), 20);
}
delay(50);
}
void LightPat1() {
flash();
delay(1100);
flow();
darkencheck();
delay(1600);
flow();
darkencheck();
delay(1650);
flash();
delay(1050);
flow();
darkencheck();
delay(1550);
flow();
darkencheck();
delay(1350);
flow();
delay(1400);
darkencheck();
delay(500);
flash();
delay(500);
flow();
darkencheck();
delay(300);
flash();
delay(500);
flash();
delay(500);
flow();
darkencheck();
delay(500);
flash();
delay(300);
flow();
delay(1500);
darkencheck();
delay(600);
}
void LightPat2() {
flow();
darkencheck();
delay(100);
}
void LightPat3() {
flow();
darkencheck();
delay(2000);
}
void colorWipe(uint32_t color1, uint32_t color2, uint32_t color3, uint32_t color4, int wait) {
int numPixels = LAstrip.numPixels();
if (RAstrip.numPixels() > numPixels) numPixels = RAstrip.numPixels();
if (LLstrip.numPixels() > numPixels) numPixels = LLstrip.numPixels();
if (RLstrip.numPixels() > numPixels) numPixels = RLstrip.numPixels();
for (int i = 0; i < numPixels; i++) {
if (i < LAstrip.numPixels()) LAstrip.setPixelColor(i, color1);
if (i < RAstrip.numPixels()) RAstrip.setPixelColor(i, color2);
if (i < LLstrip.numPixels()) LLstrip.setPixelColor(i, color3);
if (i < RLstrip.numPixels()) RLstrip.setPixelColor(i, color4);
LAstrip.show();
RAstrip.show();
LLstrip.show();
RLstrip.show();
delay(wait);
}
}
void strikecheck() {
flow();
darkencheck();
delay(50);
}
void darkencheck() {
if (digitalRead(reed) == HIGH) {
darkenwhite();
} else {
darkenpurple();
}
}
void darkenwhite() {
uint16_t i, j;
int numPixels = LAstrip.numPixels();
if (RAstrip.numPixels() > numPixels) numPixels = RAstrip.numPixels();
if (LLstrip.numPixels() > numPixels) numPixels = LLstrip.numPixels();
if (RLstrip.numPixels() > numPixels) numPixels = RLstrip.numPixels();
for (j = 255; j > 0; j--) {
for (i = 0; i < LAstrip.numPixels(); i++) {
LAstrip.setPixelColor(i, j, j, j);
}
}
for (j = 255; j > 0; j--) {
for (i = 0; i < RAstrip.numPixels(); i++) {
RAstrip.setPixelColor(i, j, j, j);
}
}
for (j = 255; j > 0; j--) {
for (i = 0; i < LLstrip.numPixels(); i++) {
LLstrip.setPixelColor(i, j, j, j);
}
}
for (j = 255; j > 0; j--) {
for (i = 0; i < RLstrip.numPixels(); i++) {
RLstrip.setPixelColor(i, j, j, j);
}
}
LAstrip.show();
RAstrip.show();
LLstrip.show();
RLstrip.show();
}
void darkenpurple() {
uint16_t i, j;
int numPixels = LAstrip.numPixels();
if (RAstrip.numPixels() > numPixels) numPixels = RAstrip.numPixels();
if (LLstrip.numPixels() > numPixels) numPixels = LLstrip.numPixels();
if (RLstrip.numPixels() > numPixels) numPixels = RLstrip.numPixels();
for (j = 255; j > 0; j--) {
for (i = 0; i < LAstrip.numPixels(); i++) {
LAstrip.setPixelColor(i, j, i, j);
}
}
for (j = 255; j > 0; j--) {
for (i = 0; i < RAstrip.numPixels(); i++) {
RAstrip.setPixelColor(i, j, i, j);
}
}
for (j = 255; j > 0; j--) {
for (i = 0; i < LLstrip.numPixels(); i++) {
LLstrip.setPixelColor(i, j, i, j);
}
}
for (j = 255; j > 0; j--) {
for (i = 0; i < RLstrip.numPixels(); i++) {
RLstrip.setPixelColor(i, j, i, j);
}
}
LAstrip.show();
RAstrip.show();
LLstrip.show();
RLstrip.show();
}
void TakeSamples() {
static byte
gLimitDeadTime = 0;
float
intermediate,
resultant;
static unsigned long
timeSample = 0;
unsigned long
timeNow;
timeNow = micros();
if ((timeNow - timeSample) < SAMPLE_RATE)
return;
timeSample = timeNow;
mma.read();
xg_f = (float)mma.x / 2048.0;
yg_f = (float)mma.y / 2048.0;
zg_f = (float)mma.z / 2048.0;
xg_sum = xg_sum + xg_f;
yg_sum = yg_sum + yg_f;
zg_sum = zg_sum + zg_f;
sample_count = sample_count + 1;
#ifndef TEST_RESULTANT
resultant = 0.0;
#else
resultant = sqrt(pow(xg_f, 2) + pow(yg_f, 2) + pow(zg_f, 2));
#endif
if (gLimitDeadTime)
gLimitDeadTime--;
if (gLimitDeadTime == 0) {
if ((abs(xg_f) > MAX_G) || (abs(yg_f) > MAX_G) || (abs(zg_f) > MAX_G) || resultant > MAX_G_RESULTANT) {
{
bGLimitExceeded = true;
gLimitDeadTime = CHK_DEADTIME;
}
}
}
}
void strike() {
static int
nEvents = 0;
static byte
stateLED = LED_IDLE;
static unsigned long
timeLED;
unsigned long
timeNow;
switch (stateLED) {
case LED_IDLE:
if (bGLimitExceeded) {
timeNow = millis();
bGLimitExceeded = false;
nEvents++;
if (nEvents >= EVENTS_LIMIT)
strikecheck();
timeLED = millis();
stateLED = LED_TIME;
}
break;
}
}
Remote Code:
//Include library
#include <SoftwareSerial.h>
//Set up button pin
const int oran = 12;
const int pecha = 11;
const int chesto = 10;
const int cheri = 9;
const int razz = 8;
const int pinap = 7;
int status = false;
String but1 = "Oran";
String but2 = "Pecha";
String but3 = "Chesto";
String but4 = "Cheri";
String but5 = "Razz";
String but6 = "Pinap";
//Define Bluetooth
SoftwareSerial BTSerial(3, 2); //RX, TX
int dataFromComputer = 0;
void setup() {
//Set buttons to input
pinMode(oran, INPUT);
pinMode(pecha, INPUT);
pinMode(chesto, INPUT);
pinMode(cheri, INPUT);
pinMode(razz, INPUT);
pinMode(pinap, INPUT);
//Start Bluetooth
BTSerial.begin(38400);
Serial.begin(9600);
}
void loop() {
//Begin The Bluetooth
if (Serial.available() > 0) {
dataFromComputer = Serial.read();
}
//Prepare Button Read
int orval = digitalRead(oran);
int peval = digitalRead(pecha);
int chesval = digitalRead(chesto);
int cherval = digitalRead(cheri);
int raval = digitalRead(razz);
int pival = digitalRead(pinap);
//Each button transmits a different number
if (orval == HIGH) {
status = !status;
BTSerial.write('1');
Serial.print(but1);
delay(10);
status = status;
while (status = !status);
} else if (peval == HIGH) {
status = !status;
BTSerial.write('2');
Serial.print(but2);
delay(10);
status = status;
while (status = !status);
} else if (chesval == HIGH) {
status = !status;
BTSerial.write('3');
Serial.print(but3);
delay(10);
status = status;
while (status = !status);
} else if (cherval == HIGH) {
status = !status;
BTSerial.write('4');
Serial.print(but4);
delay(10);
status = status;
while (status = !status);
} else if (raval == HIGH) {
status = !status;
BTSerial.write('5');
Serial.print(but5);
delay(10);
status = status;
while (status = !status);
} else if (pival == HIGH) {
status = !status;
BTSerial.write('6');
Serial.print(but6);
delay(10);
status = status;
while (status = !status);
}
}
