0

Memory allocation error
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Memory allocation error

by lburnham on Mon Jan 14, 2019 6:41 pm

Hi I'm using a circuit playground express along with a crikit to do some neopixel lighting, gauge control and speedometer control for a project. The problem I'm running into is when I get to a certain point in my code as a far as building the code, when I save it to the cpx I get the memory allocation error. Each of them will work fine individually, when i try to add them together I get the memory allocation error. Here is the code I've been trying. I separated them here with the dashed lines just for the post to show where the individual 'programs' are broken up. Any help would be greatly appreciated. BTW this code is mostly just for testing and proving it will work and no where near a finished product, I have a lot let to add. Is this too much for the memory of the cpx or am I doing something incorrectly?

Code: Select all | TOGGLE FULL SIZE
# FORD SIGN CODE REV 1 : 1/2/19
# BUTTON_A: RED & GREEN will illuminate
# BUTTON_B Police Lights | Temp controlls "pixels"
import time
import adafruit_thermistor
import board
import neopixel
from analogio import AnalogIn
from simpleio import map_range
from digitalio import DigitalInOut, Direction, Pull
from adafruit_crickit import crickit

num_npixels = 48  # Number of pixels driven from Crickit NeoPixel terminal
thermistor = adafruit_thermistor.Thermistor(
    board.TEMPERATURE, 10000, 10000, 25, 3950)
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.2)
npixels = neopixel.NeoPixel(board.A1, 48, brightness=.3, auto_write=False)
analogin = AnalogIn(board.LIGHT)
buttonA = DigitalInOut(board.BUTTON_A)
buttonA.direction = Direction.INPUT
buttonA.pull = Pull.DOWN
buttonB = DigitalInOut(board.BUTTON_B)
buttonB.direction = Direction.INPUT
buttonB.pull = Pull.DOWN
crickit.drive_1.frequency = 1000

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
OFF = (0, 0, 0)

while True:
    temp_c = thermistor.temperature
    temp_f = thermistor.temperature * 9 / 5 + 32
    print("Temperature is: %f C and %f F" % (temp_c, temp_f))
    time.sleep(.25)
    if temp_f > (83):
        COLOR = RED
    if temp_f < (83):
        COLOR = BLUE
    pixels.fill(COLOR)
    npixels.fill(CYAN)
    npixels[47] = ((0, 255, 0))
    npixels[42] = ((0, 255, 0))
    pixels.show()
    npixels.show()
    peak = map_range(analogin.value, 2000, 62000, 0, 9)
    print(analogin.value)
    print(int(peak))
    time.sleep(.25)
    #if peak > 0:
    #    npixels.fill(GREEN)
    #    npixels.show()

    if buttonA.value:
        cycles = 0
        while cycles < 5:
            cycles = cycles + 1
            npixels[10] = ((0, 255, 0))
            npixels.show()
            time.sleep(.25)
            npixels[10] = ((0, 0, 0))
            npixels.show()
            time.sleep(.25)

    if buttonB.value:
           while True:
                print("1 Drive demo!")
                crickit.drive_1.fraction = 1.0  # all the way on
                time.sleep(2)
                crickit.drive_1.fraction = 0.0  # all the way off
                time.sleep(10)
                crickit.drive_1.fraction = 0.5  # half on/off
                time.sleep(5)
                # and repeat!
                if buttonB.value:
                    break


---------------------------Separate Code-----------------------------------------

# FORD SIGN CODE REV 1 : 1/2/19
# BUTTON_A: RED & GREEN will illuminate
# BUTTON_B Police Lights | Temp controlls "pixels"
import time
import adafruit_thermistor
import board
import neopixel
from analogio import AnalogIn
from simpleio import map_range
from digitalio import DigitalInOut, Direction, Pull
from adafruit_crickit import crickit

num_npixels = 48  # Number of pixels driven from Crickit NeoPixel terminal
thermistor = adafruit_thermistor.Thermistor(
    board.TEMPERATURE, 10000, 10000, 25, 3950)
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.2)
npixels = neopixel.NeoPixel(board.A1, 48, brightness=.3, auto_write=False)
analogin = AnalogIn(board.LIGHT)
buttonA = DigitalInOut(board.BUTTON_A)
buttonA.direction = Direction.INPUT
buttonA.pull = Pull.DOWN
buttonB = DigitalInOut(board.BUTTON_B)
buttonB.direction = Direction.INPUT
buttonB.pull = Pull.DOWN
crickit.drive_1.frequency = 1000
crickit.drive_2.frequency = 1000
crickit.drive_3.frequency = 1000
crickit.drive_4.frequency = 1000
motor_1 = crickit.dc_motor_1

while True:
    crickit.drive_1.fraction = 0
    crickit.drive_2.fraction = 0
    crickit.drive_3.fraction = 0
    crickit.drive_4.fraction = 0
    time.sleep(.25)
    pixels.fill((255, 0, 0))
    npixels.fill((0, 255, 0))
    npixels[47] = ((255, 0, 0))
    npixels[42] = ((255, 0, 0))
    pixels.show()
    npixels.show()

    if buttonA.value:
        motor_1.throttle = -1  # full speed forward
        time.sleep(.25)
        motor_1.throttle = -0.7  # half speed forward
        time.sleep(5)
        motor_1.throttle = -1  # half speed forward
        time.sleep(5)

        motor_1.throttle = -0.5  # half speed forward
        time.sleep(5)

        motor_1.throttle = 0  # stopped

        time.sleep(1)

--------------------------Separate Code----------------------------------------

# FORD SIGN CODE REV 1 : 1/2/19
# BUTTON_A: RED & GREEN will illuminate
# BUTTON_B Police Lights | Temp controlls "pixels"
import time
import adafruit_thermistor
import board
import neopixel
from analogio import AnalogIn
from simpleio import map_range
from digitalio import DigitalInOut, Direction, Pull
from adafruit_crickit import crickit

num_npixels = 48  # Number of pixels driven from Crickit NeoPixel terminal
thermistor = adafruit_thermistor.Thermistor(
    board.TEMPERATURE, 10000, 10000, 25, 3950)
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.2)
npixels = neopixel.NeoPixel(board.A1, 48, brightness=.3, auto_write=False)
analogin = AnalogIn(board.LIGHT)
buttonA = DigitalInOut(board.BUTTON_A)
buttonA.direction = Direction.INPUT
buttonA.pull = Pull.DOWN
buttonB = DigitalInOut(board.BUTTON_B)
buttonB.direction = Direction.INPUT
buttonB.pull = Pull.DOWN
crickit.drive_1.frequency = 1000
crickit.drive_2.frequency = 1000
crickit.drive_3.frequency = 1000
crickit.drive_4.frequency = 1000
motor_1 = crickit.dc_motor_1

while True:
    crickit.drive_1.fraction = 0
    crickit.drive_2.fraction = 0
    crickit.drive_3.fraction = 0
    crickit.drive_4.fraction = 0
    time.sleep(.25)
    pixels.fill((255, 0, 0))
    npixels.fill((0, 255, 0))
    npixels[47] = ((255, 0, 0))
    npixels[42] = ((255, 0, 0))
    pixels.show()
    npixels.show()

    if buttonB.value:
        cycles = 0
        while cycles < 5:
            print("WORKING!")
            cycles = cycles + 1
            crickit.drive_1.fraction = 1.0  # all the way on
            crickit.drive_2.fraction = 1.0  # all the way on
            crickit.drive_3.fraction = 1.0  # all the way on
            crickit.drive_4.fraction = 1.0  # all the way on
            time.sleep(5)
            crickit.drive_1.fraction = 0  # all the way off
            crickit.drive_2.fraction = 0  # all the way off
            crickit.drive_3.fraction = 0  # all the way off
            crickit.drive_4.fraction = 0  # all the way off
            time.sleep(5)
            crickit.drive_1.fraction = 0.5  # HALF
            crickit.drive_2.fraction = 0.5  # HALF
            crickit.drive_3.fraction = 0.5  # HALF
            crickit.drive_4.fraction = 0.5  # HALF
            time.sleep(5)
            # and repeat!
Last edited by lburnham on Tue Jan 15, 2019 2:10 pm, edited 2 times in total.

lburnham
 
Posts: 23
Joined: Wed Jul 16, 2014 10:56 am

Re: Memory allocation error

by adafruit_support_mike on Tue Jan 15, 2019 4:02 am

[moved to the CircuitPython forum]

adafruit_support_mike
 
Posts: 57844
Joined: Thu Feb 11, 2010 2:51 pm

Re: Memory allocation error

by kevinjwalters on Sat Jan 19, 2019 9:57 pm

These things can be difficult to work out without a very careful look at the code and knowledge of how the libraries works. One thing I note from a quick skim is neopixel.NeoPixel when used with a brightness value which isn't 1.0 has to make a temporary buffer each time before it does the write down the bus. For testing purposes you might want to take out the brightness setting and let it default to 1 assuming that doesn't cause you any current/power problems?

Why are you using things like ((0, 255, 0))? I don't think the extra set of brackets is doing anything? If you already have green defined then why not use it?

Unrelated to memory use you appear to have some use of show() on pixels but have not set auto_write=False.

When does the error occur btw? Can you show the full error? What version of CircuitPython and libraries are you using?

kevinjwalters
 
Posts: 387
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.