Need Help Flashing LED then stopping

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Need Help Flashing LED then stopping

Post by APSIProps »

Hey! I'm at propmaker working on a prop that has an led package in it. We have a trinket m0, bluefruit le uart friend, and 2 neopixels. I am trying to adapt the example code "controller" for the LE UART Friend so that is what we will need to use. When button 1 is pressed, I need the first led to begin flashing red and do so continuously until button 2 is pressed. When button 2 is pressed, the flashing red led will turn off and the second led will turn on as green. Finally, i need to be able to seamlessly transition between these 2 states(flashing red and solid green). Here is my code minus the bluefruit config and packet parser as they are stock.

Code: Select all

/*********************************************************************
 This is an example for our nRF51822 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

#include <Adafruit_NeoPixel.h>

#include "BluefruitConfig.h"

#if SOFTWARE_SERIAL_AVAILABLE
  #include <SoftwareSerial.h>
#endif

/*=========================================================================
    APPLICATION SETTINGS

    FACTORYRESET_ENABLE       Perform a factory reset when running this sketch
   
                              Enabling this will put your Bluefruit LE module
                              in a 'known good' state and clear any config
                              data set in previous sketches or projects, so
                              running this at least once is a good idea.
   
                              When deploying your project, however, you will
                              want to disable factory reset by setting this
                              value to 0.  If you are making changes to your
                              Bluefruit LE device via AT commands, and those
                              changes aren't persisting across resets, this
                              is the reason why.  Factory reset will erase
                              the non-volatile memory where config data is
                              stored, setting it back to factory default
                              values.
       
                              Some sketches that require you to bond to a
                              central device (HID mouse, keyboard, etc.)
                              won't work at all with this feature enabled
                              since the factory reset will clear all of the
                              bonding data stored on the chip, meaning the
                              central device won't be able to reconnect.
    MINIMUM_FIRMWARE_VERSION  Minimum firmware version to have some new features
    MODE_LED_BEHAVIOUR        LED activity, valid options are
                              "DISABLE" or "MODE" or "BLEUART" or
                              "HWUART"  or "SPI"  or "MANUAL"
    -----------------------------------------------------------------------*/
    #define FACTORYRESET_ENABLE         1
    #define MINIMUM_FIRMWARE_VERSION    "0.6.6"
    #define MODE_LED_BEHAVIOUR          "MODE"
    #define LED_PIN   2
    #define LED_COUNT 2

/*=========================================================================*/
Adafruit_NeoPixel strip(2, 2, NEO_GRB + NEO_KHZ800);
// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);

Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
                      BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/

/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
 Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);

/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
//                             BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
//                             BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);


// 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[];




/**************************************************************************/
/*!
    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
  strip.begin();
  strip.show();

  //while (!Serial);  // required for Flora & Micro
  delay(500);

  Serial.begin(115200);
  Serial.println(F("Adafruit Bluefruit App Controller 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(500);
  }

  Serial.println(F("******************************"));

  // LED Activity command is only supported from 0.6.6
  if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
  {
    // Change Mode LED Activity
    Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
    ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
  }

  // Set Bluefruit to DATA mode
  Serial.println( F("Switching to DATA mode!") );
  ble.setMode(BLUEFRUIT_MODE_DATA);

  Serial.println(F("******************************"));

}

/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
  /* Wait for new data to arrive */
  uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
  if (len == 0) return;

  /* Got a packet! */
  static bool isFlashing = true;
  static bool buttonPressed = false;
  
  while (Serial.available()) {  // Read incoming serial data
    uint8_t packetbuffer[64];   // Buffer to hold incoming packets
    uint8_t packetSize = Serial.readBytesUntil('\n', packetbuffer, 64); // Read packet data
    
    if (packetbuffer[0] == 'B') {  // If the packet is a button press
      uint8_t buttnum = packetbuffer[1] - '0';
      boolean pressed = packetbuffer[2] - '0';
      
      if (buttnum == 1) {  // If the button is the one you want to use
        if (pressed) {  // If the button is pressed
          buttonPressed = true;
          isFlashing = false; // Stop the flashing
          strip.setPixelColor(0, strip.Color(0, 255, 0)); // Set the color of the NeoPixel to green
          strip.show(); // Update the NeoPixel strip
        } else {  // If the button is released
          buttonPressed = false;
          isFlashing = true; // Start the flashing again
        }
      }
    }
  }
  
  if (isFlashing) {
    strip.setPixelColor(0, strip.Color(255, 0, 0)); // Set the color of the NeoPixel to red
    strip.show(); // Update the NeoPixel strip
    delay(500); // Wait for 500 milliseconds
    strip.setPixelColor(0, strip.Color(0, 0, 0)); // Turn off the NeoPixel
    strip.show(); // Update the NeoPixel strip
    delay(500); // Wait for 500 milliseconds
  }
}


Please please help me out, everything I have tried hasn't worked and I have tried many different solutions. Thanks in advance!
-Harris

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help Flashing LED then stopping

Post by dastels »

How do you have things connected? Photos of the wiring and connections, please.

Dave

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Need Help Flashing LED then stopping

Post by APSIProps »

I don't have access to the wiring itself but I have a diagram I drew. I'm 99% sure wiring is not the issue.
IMG_0422.jpg
IMG_0422.jpg (798.95 KiB) Viewed 185 times

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Need Help Flashing LED then stopping

Post by APSIProps »

And feel free to change code however you see fit. I'm not sure if this is the best way to go about it.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help Flashing LED then stopping

Post by dastels »

It looks like you are trying to read data from the BLE using Serial. It should be using Serial1 (pisn 3 & 4). See https://learn.adafruit.com/adafruit-tri ... es-2910532

Dave

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Need Help Flashing LED then stopping

Post by APSIProps »

The bluetooth connection and data transfer works fine. The issue is knowing coding well enough to be able to create the desired result. I want the red led to flash until button 2 on the bluefruit connect app is pressed. At which point, the second led will turn on green and the red led will stop flashing. Also, when i press button 1, the red led should begin flashing indefinitely until button 2 is pressed again.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help Flashing LED then stopping

Post by dastels »

OK.. that's weird. The pin docs says 3&4 are on Serial1 which makes sense since the USB connection is Serial.

A state machine would suit your application sell, with transitions triggered by button presses:

RED state (red LED flashes, green LED off)
- pressing button 2 -> GREEN state
- turn off green LED
- track time to toggle the red LED as appropriate
GREEN state (red LED off, green LED on)
- pressing button 1 -> RED state
- turn off red LED
- turn on green LED

I've done similar things in a coffee/tea timer project (based on a CircuitPlayground Classic): states, buttons to change state, NeoPixel fiddling based on timing. See https://github.com/dastels/coffee_tea_timer

Dave

User avatar
Aliya88
 
Posts: 1
Joined: Thu Apr 06, 2023 6:58 am

Re: Need Help Flashing LED then stopping

Post by Aliya88 »

APSIProps wrote: Thu May 04, 2023 3:46 pm The bluetooth connection and data transfer works fine. The issue is knowing coding well enough to be able to create the desired result. I want the red led to flash until button 2 on the bluefruit connect app is pressed. At which point, the second led will turn on green and the red led will stop flashing. Also, when i press button 1, the red led should begin flashing indefinitely until button 2 is pressed again.
Hi guys

It sounds like you need to write a custom script to control the behavior of the LED lights based on the input from the Bluefruit Connect app.Free Fire

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”