我用的是樹莓派3b,想控制一個5v八路繼電器,代碼邏輯是讓八個繼電器依次切換為高電平,以順序循環(huán)。
但是我接好線后,繼電器的In1 In2兩個小燈長亮。
運行腳本,只聽得到另外六個繼電器的打開和關(guān)閉的噠噠聲,而且只能控制一輪,之后就會失效,也無法控制后續(xù)電路工作。
排查一天了,不知道問題出在哪里....希望有人能發(fā)現(xiàn)問題
##################################代碼如下###################################
import RPi.GPIO as GPIO
import time
import numpy as np
# Set the BPM (speed) here
bpm = 120
# Each line represents a beat. Each 0 or 1 is a solenoid
sequence = [
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
]
gpio_map = [2, 3, 4, 17, 27, 22, 10, 9]
# The Lenth of time each solenoid should be activated
active_duration = 0.01
############################################# The main script#############################################
# Sets pin number to BCM mode
GPIO.setmode(GPIO.BCM)
# Calculate the time period between the solenoid being deactivated and the next beat starting
beat_gap = (float(60) / float(bpm)) - float(active_duration)
# Generator to infinitely loop around the sequence
def infinite_generator(n):
i = 0
while True:
if i >= len(n):
i = 0
yield n[ i]
i = i + 1
# Loop through each pin and set the mode and state to 'low'
for i in gpio_map:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
# Run the infinite loop
try:
for beat in infinite_generator(sequence):
# Get active drum numbers
active = np.where(beat)[0]
# Get pin numbers for active drums
pins = [gpio_map[ i] for i in active]
print('Activating Pins ', pins)
GPIO.output(pins, GPIO.LOW)
time.sleep(active_duration)
GPIO.output(pins, GPIO.HIGH)
print('Sleep ', beat_gap)
time.sleep(beat_gap)
# End
except KeyboardInterrupt:
print ('Quit')
# Reset GPIO settings
GPIO.cleanup() |