0

I'm trying to make a clock with some LEDs, so I need to light certain LEDs based on the time. My function for lighting a block of LEDs is

My main loop is

from rpi_ws281x import *
import time
import datetime
from time import sleep

 def blockfill (strip, startpixel, endpixel, color):
   for i in range (startpixel, endpixel):
        strip.setPixelColor(i, color)

#Various Parameters for LED Tape here
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
strip.begin()
while True:
    now = datetime.datetime.now()
    hour = (now.hour % 12)
    minute = (now.minute)
    second = now.second
    #Calling something here
    strip.show()
    sleep(0.2)

And if I put

blockfill(strip, 96, 100, Color(255,0,0))

under #CallSomethingHere then it's fine. However if I define

def hour2():
    blockfill(strip, 96, 100, Color(255,0,0))

Then call hour2() from my main file, I got "segmentation fault"

What am I missing?

  • `What am I missing?` - the part you aren't showing us that is causing the problem? – wwii Jul 12 '20 at 14:20
  • I see a couple of issues that could be tripping you up, but I can't tell if these issues are real or the result of leaving too much out of your example code. Please give a minimal working code that demonstrates the problem. Thx! – user10637953 Jul 12 '20 at 14:23
  • Hopefully the updates should make more sense! – Adam Davies Jul 12 '20 at 14:26
  • Thank you for that. I don't have the mod you use, so someone else may have to give you an actual answer. But I notice that your hour2() function has no parameters and does not declare strip as global. (Could this be an issue? The error you are getting is new to me.) A search for "python segmentation error" suggests an out of memory condition. Do you have a recursion issue here? Check your code to make sure it isn't calling one of your function in an endless loop. – user10637953 Jul 12 '20 at 14:32
  • Potentially. I'm trying to use hour2 to call blockfill with different parameters as there will be 12 different variants of blockfill for 12 hours. If I change the definition of hour2 to simply print("done") then it works perfectly – Adam Davies Jul 12 '20 at 14:38
  • 1
    I see. I'm afraid I must tap out of the problem at this point. It is an interesting error, so I will be watching to see if someone else can provide clarity. Let us know if you find the answer yourself. Cheers. – user10637953 Jul 12 '20 at 14:41
  • You code has an endless loop. "While True.." Do you have a break command in there somewhere? That loop keeps calling your function. Try >> run_loop == True, >> while run_loop == True, call your function, then set run_loop = False. – user10637953 Jul 12 '20 at 14:48
  • The endless loop is intentional as it constantly refreshes the time values (hour, minute and second) and will run the relevant command on the LEDs. I think the issue is to do with how I'm calling blockfill from within hour2 – Adam Davies Jul 12 '20 at 14:49
  • As an experiment, try limiting that loop to, say, 6 cycles and see if that segmentation error appears. Something is eating up your memory and it has to do with the modual. If I were wring the program, I would use a different approach. Maybe a surface with draw functions, or a pixel array (which is slow). Just a thought. Scratch that idea. It seems you have some sort of interface and so must use your mod just to access it. – user10637953 Jul 12 '20 at 14:53
  • Maybe you can contact a support group for Adafruit and ask them what is going on? The problem may be too esoteric for us if we don't have the hardware to reproduce the error. – user10637953 Jul 12 '20 at 14:59
  • This Stack Overflow question on segmentation errors is interesting. Maybe this can give you some leads. https://stackoverflow.com/questions/10035541/what-causes-a-python-segmentation-fault – user10637953 Jul 12 '20 at 15:08
  • you should send `strip` as argument `def hour2(strip):` – furas Jul 12 '20 at 15:57

1 Answers1

0

Thanks for your suggestions. I was trying to split the file out into multiple different files and I was initiating strip in the wrong place

Adam