Hi all, I feel like I have a very simple problem. Or rather I have a problem that probably has a more simple solution than I realize. I would like any help sorting this out.
Below is code I’ve written to enable me to do a few things:
- cycle through a number of different LED illumination patterns with PWM
- enable communication via bluetooth application
- control neopixels via that application
The problem I’m running into is that the code only executes when the serial monitor is open, which is bothersome as its meant to be a component in a prop I’m building disconnected from any monitor. what I’m seeing is that when I power on the unit and then press the button, nothing happens and nothing continues to happen until I turn on the serial monitor. Once the monitor is open the code runs as it should and continues to function even after I close the serial monitor window. However, if I reset the board and power it back on, the code once again no longer runs until I open a monitor again. How can I change the code below to allow the device to work without the monitor window open?
Thanks in advance, I posted a thread yesterday which lead me to do some digging and sort out a problem I was running into. Very happy with the community here!
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_ATParser.h>
#include <Adafruit_BLE.h>
#include <Adafruit_BluefruitLE_SPI.h>
#include <Adafruit_BluefruitLE_UART.h>
#if SOFTWARE_SERIAL_AVAILABLE
#include <SoftwareSerial.h>
#endif
#include "BluefruitConfig.h"
//neopixel and BLE definitions
#define FACTORYRESET_ENABLE 0
#define PIN 9
#define NUMPIXELS 1
//create neopixel and bluefruit objects
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN);
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
//LED status pin variables
const byte ledPin = 5;
int ledState = 0;
//button pin variables
const byte buttonPin = 6;
int buttonState;
int reading;
int lastButtonState = LOW;
//switch mode variables
int mode = 0; //selector state
//debounce check variables
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
//variables for blinking function
const long blinkInterval = 1000; //blink without delay interval for case 1
unsigned long currentMillis=0;
unsigned long blinkingMillis=0;
// A small helper
void error(const __FlashStringHelper*err) {
//Serial.println(err);
while (1);
}
// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);
// the packet buffer
extern uint8_t packetbuffer[];
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin,INPUT);
pinMode(ledPin,OUTPUT);
//set initial LED state
analogWrite(ledPin, ledState);
//neopixel and BLE initialization
while (!Serial); // required for Flora & Micro
delay(10);
// turn off neopixel
pixel.begin(); // This initializes the NeoPixel library.
for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(0,0,0)); // off
}
pixel.show();
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit Neopixel Color Picker Example"));
Serial.println(F("------------------------------------------------"));
// Initialise the module
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
// Perform a factory reset to make sure everything is in a known state
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}
// Disable command echo from Bluefruit
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
// Print Bluefruit information
ble.info();
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();
ble.verbose(false); // debug info is a little annoying after this point!
// Wait for connection
/* while (! ble.isConnected()) {
delay(10);
}
*/
Serial.println(F("***********************"));
// Set Bluefruit to DATA mode
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
Serial.println(F("***********************"));
// Wait for new data to arrive
}
void loop() {
// put your main code here, to run repeatedly:
//confirm we made it to loop()
Serial.println("Well hello there loop()");
int reading = digitalRead(buttonPin);
if (reading != lastButtonState){
//reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
//debounce check
if (reading != buttonState){
buttonState = reading;//update buttonState
if (buttonState == HIGH){
//print button confirmation
Serial.println("button toggled");
mode++;//cycle the case on button press
}
}
}
if (mode <= 3){
switch (mode) {
case 0:
//confirm we made it to case 0
Serial.println("Hey look ma I made it to case 0");
//case 0 is everything shut off
ledState = 0;
analogWrite(ledPin, ledState);
break;
case 1:
Serial.println("Now we're in case 1");
pixelBlinking();
break;
case 2:
Serial.println("and case 2");
// case 2 is led solid
ledState = 255;
analogWrite(ledPin, ledState);
break;
case 3:
Serial.println("case 3, trying to connect to BLE");
//case 3 enables neopixel color picking via BLE app
//it currently sets the led at half brightness to show activity
ledState = 127;
analogWrite(ledPin, ledState);
if (ble.isConnected()){
Serial.println("BLE connected, running neopixel code");
pixelColorPicker();
break;
// god I fucking hope this works, a long hold of the button will break the board out of the neopixel loop
if (reading != lastButtonState){
//reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
//debounce check
if (reading != buttonState){
buttonState = reading;//update buttonState
if (buttonState == HIGH){
//print button confirmation
Serial.println("button toggled");
mode++;//cycle the case on button press
}
}
}
}
else{
Serial.println("No BLE, looping");
break;
}
}
}
else {
mode = 0;
}
lastButtonState = reading;
}
void pixelColorPicker(){
Serial.println("we in the neopixel world now");
/* Wait for new data to arrive */
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
if (len == 0) return;
/* Got a packet! */
// printHex(packetbuffer, len);
// Color
if (packetbuffer[1] == 'C') {
uint8_t red = packetbuffer[2];
uint8_t green = packetbuffer[3];
uint8_t blue = packetbuffer[4];
//Serial.print ("RGB #");
//if (red < 0x10) Serial.print("0");
//Serial.print(red, HEX);
//if (green < 0x10) Serial.print("0");
//Serial.print(green, HEX);
//if (blue < 0x10) Serial.print("0");
//Serial.println(blue, HEX);
for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(red,green,blue));
}
pixel.show(); // This sends the updated pixel color to the hardware.
}
}
void pixelBlinking(){
currentMillis = millis();
if (currentMillis - blinkingMillis >= blinkInterval) {
// save the last time you blinked the LED
blinkingMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == 0) {
ledState = 255;
} else {
ledState = 0;
}
// set the LED with the ledState of the variable:
analogWrite(ledPin, ledState);
}
}