Hello!
First time to this forum, forgive me if I've done anything procedurally incorrect.
So, I've created a circuit using a Bleufruit Feather nRF52 as a controller, and I'm having minor issues with my code.
I wrote my code so that when I press a button on my phone, a green LED will turn on for a couple seconds and a neopixel ring will simultaneously pulse green before turning off. The same occurs with red. The problem is that the neopixel ring pulses after the green or red LED has completed its temporary flash. I've posted a query on adafruit and I was told it was due to the 64 step sequence I wrote within the neopixel code.
I was directed to this link: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Unfortunately, I'm hardly savvy when it comes to coding, and I still don't understand what I must write in order to sync up my LEDs and my neopixels.
Thanks in advance for any help; the code is included!
-J
#include <bluefruit.h>
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
BLEUart bleuart;
// Function prototypes for packetparser.cpp
uint8_t readPacket (BLEUart *ble_uart, uint16_t timeout);
float parsefloat (uint8_t *buffer);
void printHex (const uint8_t * data, const uint32_t numBytes);
// Packet buffer
extern uint8_t packetbuffer[];
//Constants
const int ledPinON = A1;
const int ledPinOFF = A3;
#define PIN 15
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_RGBW + NEO_KHZ800);
void setup(void)
{
pinMode(ledPinON, OUTPUT);
pinMode(ledPinOFF, OUTPUT);
digitalWrite(ledPinON, LOW);
digitalWrite(ledPinOFF, LOW);
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit52 Controller App Example"));
Serial.println(F("-------------------------------------------"));
Bluefruit.begin();
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4);
Bluefruit.setName("Bluefruit52");
bleuart.begin();
startAdv();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
Serial.println();
strip.begin();
strip.show();// Initialize all pixels to 'off'
strip.setBrightness(64);
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addService(bleuart);
Bluefruit.ScanResponse.addName();
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void loop(void)
{
uint8_t len = readPacket(&bleuart, 500);
if (len == 0) return;
if (packetbuffer[1] == 'B') {
uint8_t buttnum = packetbuffer[2] - '0';
boolean pressed = packetbuffer[3] - '0';
if (pressed)
{
if(buttnum == 5) {
//Serial.println("button1");
digitalWrite(ledPinON, HIGH);
delay(1000);
digitalWrite(ledPinON,LOW);
brightenON();
darkenON();
}
if(buttnum == 6){
//Serial.println("button2");
digitalWrite(ledPinOFF, HIGH);
delay(1000);
digitalWrite(ledPinOFF,LOW);
brightenOFF();
darkenOFF();
}
}
}
}
void brightenON() {
uint16_t i, j;
for (j = 0; j < 64; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, j, 0, 0, 0);
}
strip.show();
delay(15);
}
}
void darkenON() {
Serial.begin(9600);
uint16_t i, j;
for (j = 64; j > 0; j--) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, j, 0, 0, 0);
}
strip.show();
delay(15);
Serial.println(j);
}
}
void brightenOFF() {
uint16_t i, j;
for (j = 0; j < 64; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, j, 0, 0);
}
strip.show();
delay(15);
}
}
void darkenOFF() {
Serial.begin(9600);
uint16_t i, j;
for (j = 64; j > 0; j--) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, j, 0, 0);
}
strip.show();
delay(15);
Serial.println(j);
}
}