0

Struggling to make lights flash on a Bluefruit
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Struggling to make lights flash on a Bluefruit

by goodbow on Thu Apr 01, 2021 6:22 pm

Hi

My kid is trying to make a remote control Lego boat, using an Adafruit Bluefruit attached to a Crickit, a couple of DC motors and a servo. He is only learning CircuitPython, as am I with him so that I can help him when he gets stuck, and we hadn't used a Bluefruit before. We've got the servo and motors working using the arrows and buttons on the Controller on the Bluefruit Connect app. He wants to be able to use button 2 on the app to make the lights flash alternate red and blue like a police boat and then button 3 to stop them. Using button 2 we can get them to flash red/blue by adding additional lines of red/blue code alternately. But when the lines run out, the flashing stops and we can't figure out how to make them flash red/blue continuously on a loop, until button 3 is pressed to stop them.

Full code below.

Any suggestions would be appreciated thanks.

import time
import board
import digitalio
import neopixel

from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService

from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket

from adafruit_crickit import crickit

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False)

ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)

motor_1 = crickit.dc_motor_1
motor_2 = crickit.dc_motor_2

flash_demo = 1

RED = (255, 0, 0)
BLUE = (0, 0, 255)
OFF = (0, 0, 0)

print("Coolest boat")

while True:
ble.start_advertising(advertisement)
while not ble.connected:
pass

while ble.connected:
if uart.in_waiting:
packet = Packet.from_stream(uart)
if isinstance(packet, ButtonPacket):
if packet.pressed:
if packet.button == ButtonPacket.UP:
print("Move motors foward")
motor_1.throttle = 1
motor_2.throttle = 1
elif packet.button == ButtonPacket.BUTTON_1:
print("Stop motors")
motor_1.throttle = 0
motor_2.throttle = 0
elif packet.button == ButtonPacket.DOWN:
print("Move motors backwards")
motor_1.throttle = -1
motor_2.throttle = -1
elif packet.button == ButtonPacket.LEFT:
print("Move servo left")
crickit.servo_1.angle = 0
time.sleep(2)
crickit.servo_1.angle = 90
time.sleep(2)
elif packet.button == ButtonPacket.RIGHT:
print("Move servo right")
crickit.servo_1.angle = 180
time.sleep(2)
crickit.servo_1.angle = 90
time.sleep(2)
if packet.button == ButtonPacket.BUTTON_2:
print("Flash lights")
pixels.show()
time.sleep(1)
pixels.fill(BLUE)
pixels.show()
time.sleep(1)
pixels.fill(RED)
pixels.show()
time.sleep(1)
pixels.fill(BLUE)
pixels.show()
time.sleep(1)
pixels.fill(RED)
pixels.show()
time.sleep(1)
elif packet.button == ButtonPacket.BUTTON_3:
print("Lights off")
pixels.fill(OFF)
pixels.show()

goodbow
 
Posts: 9
Joined: Sun Feb 17, 2019 12:04 pm

Re: Struggling to make lights flash on a Bluefruit

by tannewt on Thu Apr 01, 2021 6:30 pm

Hi, please repost the code in [CODE] tags so that the whitespace is preserved. It is critical in python to understand how the code runs.

tannewt
 
Posts: 2349
Joined: Thu Oct 06, 2016 8:48 pm

Re: Struggling to make lights flash on a Bluefruit

by goodbow on Fri Apr 02, 2021 3:21 pm

Sorry about that - not really used to all this! Here is the code in
Code: Select all | TOGGLE FULL SIZE
 this time:

[code]

import time
import board
import digitalio
import neopixel

from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService

from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket

from adafruit_crickit import crickit

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False)

ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)

motor_1 = crickit.dc_motor_1
motor_2 = crickit.dc_motor_2

flash_demo = 1

RED = (255, 0, 0)
BLUE = (0, 0, 255)

print("Coolest boat")

while True:
    ble.start_advertising(advertisement)
    while not ble.connected:
        pass

    while ble.connected:
        if uart.in_waiting:
            packet = Packet.from_stream(uart)
            if isinstance(packet, ButtonPacket):
                if packet.pressed:
                    if packet.button == ButtonPacket.UP:
                        print("Move motors foward")
                        motor_1.throttle = 1 
                        motor_2.throttle = 1 
                    elif packet.button == ButtonPacket.BUTTON_1:
                        print("Stop motors")
                        motor_1.throttle = 0 
                        motor_2.throttle = 0 
                    elif packet.button == ButtonPacket.DOWN:
                        print("Move motors backwards")
                        motor_1.throttle = -1 
                        motor_2.throttle = -1
                    elif packet.button == ButtonPacket.LEFT:
                        print("Move servo left")
                        crickit.servo_1.angle = 0
                        time.sleep(2)
                        crickit.servo_1.angle = 90
                        time.sleep(2)
                    elif packet.button == ButtonPacket.RIGHT:
                        print("Move servo right")
                        crickit.servo_1.angle = 180
                        time.sleep(2)
                        crickit.servo_1.angle = 90
                        time.sleep(2)
                    elif packet.button == ButtonPacket.BUTTON_2:
                        print("Flash lights")
                        pixels.fill(RED)
                        pixels.show()
                        time.sleep(1) 
                        pixels.fill(BLUE)
                        pixels.show()
                        time.sleep(1)
                        pixels.fill(RED)
                        pixels.show()
                        time.sleep(1) 
                        pixels.fill(BLUE)
                        pixels.show()
                        time.sleep(1)
                        pixels.fill(RED)
                        pixels.show()
                        time.sleep(1) 
                        pixels.fill(BLUE)
                        pixels.show()
                        time.sleep(1)
                        pixels.fill(RED)
                        pixels.show()
                        time.sleep(1) 
                        pixels.fill(BLUE)
                        pixels.show()
                        time.sleep(1)


goodbow
 
Posts: 9
Joined: Sun Feb 17, 2019 12:04 pm

Please be positive and constructive with your questions and comments.