1

I am trying to use NumPy matrices to pick colours and brightnesses for dotstar LEDs, but when the matrix elements are about to be picked I get a numpy.float64 error saying it can't be interpreted as an integer. I don't know where numpy.float64 has come from or why it is in my code.

#5 light pulse using matrices to pick colours and brightness:

import time
import random
import board
import adafruit_dotstar as dotstar

#numpy introduces matrices:
import numpy as np

dots = dotstar.DotStar(board.D6, board.D5, 15, brightness=1)

#finds the number of dots on the strip:
n_dots = int(len(dots))  

#matrix of colours and their brightness:

dots_matrix = np.array([[(255,0,0,0.1),(255,0,0,0.5),(255,0,0,1)],
                        [(255,50,0,0.1),(255,50,0,0.5), (255,50,0,1)],
                        [(0,255,0,0.1), (0,255,0,0.5), (0,255,0,1)]])

#matrix element picking function:

def colour_choose(dot_number):
    if dot_number < 5:
        return dots_matrix[0][2]
    elif 4 < dot_number < 10:
        return dots_matrix[1][2]
    elif 9 < dot_number < 18:
        return dots_matrix[2][2]

def dim_colour_choose(dot_number):
    if dot_number < 5:
        return dots_matrix[0][1]
    elif 4 < dot_number < 10:
        return dots_matrix[1][1]
    elif 9 < dot_number < 18:
        return dots_matrix[2][1]

def dimmest_colour_choose(dot_number):
    if dot_number < 5:
        return dots_matrix[0][0]
    elif 4 < dot_number < 10:
        return dots_matrix[1][0]
    elif 9 < dot_number < 18:
        return dots_matrix[2][0]






while True:
    for x in range(3,18):
        dots[(x-2)%n_dots]=dimmest_colour_choose((x-2)%n_dots)
        dots[(x-1)%n_dots]=dim_colour_choose((x-1)%n_dots)
        dots[(x)%n_dots]=colour_choose((x)%n_dots)
        dots[(x+1)%n_dots]=dim_colour_choose((x+1)%n_dots)
        dots[(x+2)%n_dots]=dimmest_colour_choose((x+2)%n_dots)
        dots[x-3]=off
        time.sleep(0)
zacccczn
  • 19
  • 2
  • add dtype = int in your array like: dots_matrix = np.array([[(255,0,0,0.1),(255,0,0,0.5),(255,0,0,1)], [(255,50,0,0.1),(255,50,0,0.5), (255,50,0,1)], [(0,255,0,0.1), (0,255,0,0.5), (0,255,0,1)]], dtype = int) – SM Abu Taher Asif Jul 12 '19 at 12:07
  • @asif Thankyou, that has removed the error, but now when my code runs only one LED turns on at a time, when there should be five. – zacccczn Jul 12 '19 at 12:13
  • @Asif: Doing that makes the fourth element of each component truncate to `int`. They're trying to mix types, which `numpy` arrays don't support. – ShadowRanger Jul 12 '19 at 12:18
  • @ShadowRanger But what about the [structured arrays](https://docs.scipy.org/doc/numpy/user/basics.rec.html)? They do support different types. – Georgy Jul 12 '19 at 12:32
  • @Georgy: Good point. You can see how much I know about `numpy`. You should make that into an answer. – ShadowRanger Jul 12 '19 at 13:45
  • @zacccczn Could you, please, provide full error traceback? – Georgy Jul 12 '19 at 13:54

0 Answers0