首页 > 解决方案 > 'numpy.float64' 不能解释为整数

问题描述

我正在尝试使用 NumPy 矩阵为 dotstar LED 选择颜色和亮度,但是当即将选择矩阵元素时,我收到一个 numpy.float64 错误,说它不能被解释为整数。我不知道 numpy.float64 来自哪里或为什么它在我的代码中。

#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)

标签: pythonnumpymatrixlightadafruit

解决方案


推荐阅读