Last active
September 15, 2019 15:29
-
-
Save potaycat/74edb1f953951c5f223fdbc00b4e5efd to your computer and use it in GitHub Desktop.
bluetooth controlled 4 motors driver with python/pygame on pc client, and arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial, platform, glob | |
# A function that tries to list serial ports on most common platforms | |
def list_serial_ports(): | |
system_name = platform.system() | |
if system_name == "Windows": | |
# Scan for available ports. | |
available = [] | |
for i in range(256): | |
try: | |
portname = 'COM%s'%i | |
s = serial.Serial(portname) | |
available.append(portname) | |
s.close() | |
except serial.SerialException: | |
pass | |
return available | |
elif system_name == "Darwin": | |
# Mac | |
return glob.glob('/dev/tty*') + glob.glob('/dev/cu*') | |
else: | |
# Assume Linux or something else | |
return glob.glob('/dev/ttyUSB*') + glob.glob('/dev/ttyACM*') | |
def portSelect(): | |
print("finding ports...") | |
port = list_serial_ports() | |
print (port) | |
number = int( input("Which port? ") ) -1 | |
arduino = serial.Serial(port[number], 1200 , timeout = 0.01) | |
return arduino | |
import arduinoSender | |
victim = arduinoSender.portSelect() | |
import pygame, time | |
# Define some colors | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
GREEN = (0, 255, 0) | |
RED = (255, 0, 0) | |
PINK = (255, 192, 203) | |
def draw_arrows(screen): | |
pygame.draw.polygon(screen, PINK, [ (1,6) , (20,70) , (60,9) ], 4) | |
pygame.draw.polygon(screen, PINK, [ (30,6) , (60,60) , (120,90) ], 4) | |
pygame.init() | |
size = [400, 200] | |
screen = pygame.display.set_mode(size) | |
pygame.font.init() # you have to call this at the start, | |
# if you want to use this module. | |
myfont = pygame.font.SysFont('Comic Sans MS', 30) | |
#This creates a new object on which you can call the render method. | |
textsurface = myfont.render('Phan mem dieu khien o to do ', False, BLACK) | |
textsurface2 = myfont.render('choi (best UI design award)', False, BLACK) | |
pygame.display.set_caption("Điều khiển") | |
done = False | |
clock = pygame.time.Clock() | |
#some parameters | |
pressedKey = 0 | |
# -------- Main Program Loop ----------- | |
while not done: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
done = True | |
elif event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_LEFT: | |
victim.write('l'.encode()) | |
elif event.key == pygame.K_RIGHT: | |
victim.write('r'.encode()) | |
elif event.key == pygame.K_UP: | |
victim.write('u'.encode()) | |
elif event.key == pygame.K_DOWN: | |
victim.write('d'.encode()) | |
elif event.type == pygame.KEYUP: | |
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN: | |
victim.write('a'.encode()) | |
screen.fill(WHITE) | |
draw_arrows(screen) | |
#This creates a new surface with text already drawn onto it. At the end you can just blit the text surface onto your main screen. | |
screen.blit(textsurface,(0,50)) | |
screen.blit(textsurface2,(0,75)) | |
pygame.display.flip() | |
clock.tick(15) | |
pygame.quit() | |
import serial, platform, glob | |
# A function that tries to list serial ports on most common platforms | |
def list_serial_ports(): | |
system_name = platform.system() | |
if system_name == "Windows": | |
# Scan for available ports. | |
available = [] | |
for i in range(256): | |
try: | |
portname = 'COM%s'%i | |
s = serial.Serial(portname) | |
available.append(portname) | |
s.close() | |
except serial.SerialException: | |
pass | |
return available | |
elif system_name == "Darwin": | |
# Mac | |
return glob.glob('/dev/tty*') + glob.glob('/dev/cu*') | |
else: | |
# Assume Linux or something else | |
return glob.glob('/dev/ttyUSB*') + glob.glob('/dev/ttyACM*') | |
def portSelect(): | |
print("finding ports...") | |
port = list_serial_ports() | |
print (port) | |
number = int( input("Which port? ") ) -1 | |
arduino = serial.Serial(port[number], 1200 , timeout = 0.01) | |
return arduino | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
SoftwareSerial HC05(4, 5); // RX | TX | |
char incomingByte = 0; | |
void setup() { | |
pinMode(9, OUTPUT); | |
pinMode(10, OUTPUT); | |
pinMode(11, OUTPUT); | |
pinMode(6, OUTPUT); | |
pinMode(2, OUTPUT); | |
pinMode(3, OUTPUT); | |
Serial.begin(9600); | |
HC05.begin(1200); | |
digitalWrite(2, HIGH); | |
digitalWrite(3, HIGH); | |
} | |
void loop() { | |
// send data only when you receive data: | |
if (HC05.available() > 0) { | |
//if (Serial.available() > 0) { | |
incomingByte = HC05.read(); | |
Serial.println(incomingByte); | |
if (incomingByte == 'u') { | |
analogWrite(9, 255); | |
analogWrite(10, 0); | |
} | |
else if (incomingByte == 'd') { | |
analogWrite(9, 0); | |
analogWrite(10, 255); | |
} | |
else if (incomingByte == 'l') { | |
analogWrite(11, 255); | |
analogWrite(6, 0); | |
} | |
else if (incomingByte == 'r') { | |
analogWrite(11, 0); | |
analogWrite(6, 255); | |
} | |
else{ | |
analogWrite(9, 0); | |
analogWrite(10, 0); | |
analogWrite(11, 0); | |
analogWrite(6, 0); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arduinoSender | |
victim = arduinoSender.portSelect() | |
import pygame, time | |
# Define some colors | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
GREEN = (0, 255, 0) | |
RED = (255, 0, 0) | |
PINK = (255, 192, 203) | |
def draw_arrows(screen): | |
pygame.draw.polygon(screen, PINK, [ (1,6) , (20,70) , (60,9) ], 4) | |
pygame.draw.polygon(screen, PINK, [ (30,6) , (60,60) , (120,90) ], 4) | |
pygame.init() | |
size = [400, 200] | |
screen = pygame.display.set_mode(size) | |
pygame.font.init() # you have to call this at the start, | |
# if you want to use this module. | |
myfont = pygame.font.SysFont('Comic Sans MS', 30) | |
#This creates a new object on which you can call the render method. | |
textsurface = myfont.render('Phan mem dieu khien o to do ', False, BLACK) | |
textsurface2 = myfont.render('choi (best UI design award)', False, BLACK) | |
pygame.display.set_caption("Điều khiển") | |
done = False | |
clock = pygame.time.Clock() | |
#some parameters | |
pressedKey = 0 | |
# -------- Main Program Loop ----------- | |
while not done: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
done = True | |
elif event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_LEFT: | |
victim.write('l'.encode()) | |
elif event.key == pygame.K_RIGHT: | |
victim.write('r'.encode()) | |
elif event.key == pygame.K_UP: | |
victim.write('u'.encode()) | |
elif event.key == pygame.K_DOWN: | |
victim.write('d'.encode()) | |
elif event.type == pygame.KEYUP: | |
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN: | |
victim.write('a'.encode()) | |
screen.fill(WHITE) | |
draw_arrows(screen) | |
#This creates a new surface with text already drawn onto it. At the end you can just blit the text surface onto your main screen. | |
screen.blit(textsurface,(0,50)) | |
screen.blit(textsurface2,(0,75)) | |
pygame.display.flip() | |
clock.tick(15) | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment