Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Last active August 5, 2019 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IdrisCytron/6126357112feaf071aa0d61d8537d72f to your computer and use it in GitHub Desktop.
Save IdrisCytron/6126357112feaf071aa0d61d8537d72f to your computer and use it in GitHub Desktop.
Control MDDS30 In Serial Simplified Mode Using Raspberry Pi
from gpiozero import LED, Button, Buzzer
from time import sleep
import serial
led1 = LED(17)
led2 = LED(18)
sw1 = Button(21)
sw2 = Button(16)
sw3 = Button(20)
buzzer = Buzzer(26)
serialPort = serial.Serial("/dev/ttyUSB0", 9600, timeout=0.5)
def sw1Pressed():
global speed
global currentSpeed
global motorLeft
global motorRight
global motorDirection
if speed < 63:
speed = speed + 5
currentSpeed = currentSpeed + 5
if speed == 65:
speed = speed - 2
currentSpeed = currentSpeed - 2
motorLeft = currentSpeed
motorRight = currentSpeed | 128
if speed == 63:
led1.on()
buzzer.beep(0.1, 0.1, 2)
def sw2Pressed():
global speed
global currentSpeed
global motorLeft
global motorRight
global motorDirection
if speed > 0:
if speed == 63:
speed = speed - 3
currentSpeed = currentSpeed - 3
else:
speed = speed - 5
currentSpeed = currentSpeed - 5
motorLeft = currentSpeed
motorRight = currentSpeed | 128
if speed == 0:
led1.off()
buzzer.beep(0.1, 0.1, 2)
def sw3Pressed():
global speed
global currentSpeed
global motorLeft
global motorRight
global motorDirection
if speed != 0:
led2.toggle()
buzzer.beep(0.1, 0.1, 1)
motorDirection = not motorDirection
if motorDirection == False:
motorLeft = motorLeft & 191
motorRight = motorRight & 191
else:
motorLeft = motorLeft | 64
motorRight = motorRight | 64
currentSpeed = motorLeft
sw1.when_pressed = sw1Pressed
sw2.when_pressed = sw2Pressed
sw3.when_pressed = sw3Pressed
packet = bytearray()
speed = 0
currentSpeed = 0
previousSpeed = 0
motorLeft = 0
motorRight = 0
motorDirection = False
try:
while True:
if currentSpeed != previousSpeed:
print("Motor Speed: {}, Left: {}, Right: {}".format(speed, motorLeft, motorRight))
previousSpeed = currentSpeed
packet.append(motorLeft)
packet.append(motorRight)
serialPort.write(packet)
packet.clear()
except KeyboardInterrupt:
led1.off()
led2.off()
buzzer.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment