Last active
August 29, 2015 14:18
-
-
Save michvaldes001/24bc574be5ed0c7b145e to your computer and use it in GitHub Desktop.
Python + Arduino motion to LED display
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
//setup pins (note, using all PWM pins) | |
int led1 = 3; | |
int led2 = 5; | |
int led3 = 6; | |
int led4 = 9; | |
int led5 = 10; | |
int led6 = 11; | |
//Setup serial strings and substrings | |
String Signal; | |
String sub1; | |
String sub2; | |
String sub3; | |
String sub4; | |
String sub5; | |
String sub6; | |
int subInt; | |
//Start serial connection and set LED pins to output | |
void setup() { | |
Serial.begin(115200); | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(led3, OUTPUT); | |
pinMode(led4, OUTPUT); | |
pinMode(led5, OUTPUT); | |
pinMode(led6, OUTPUT); | |
} | |
void loop() { | |
//run loop whole serial connection present | |
while (Serial.available()==0) { | |
//break down main string | |
Signal = Serial.readString(); | |
sub1 = Signal.substring(0,3); | |
sub2 = Signal.substring(3,6); | |
sub3 = Signal.substring(6,9); | |
sub4 = Signal.substring(9,12); | |
sub5 = Signal.substring(12,15); | |
sub6 = Signal.substring(15,18); | |
//convert string sections to intigers | |
//vrite set intiger values as brightness for LEDs | |
subInt = sub1.toInt(); | |
analogWrite(led1, subInt); | |
subInt = sub2.toInt(); | |
analogWrite(led2, subInt); | |
subInt = sub3.toInt(); | |
analogWrite(led3, subInt); | |
subInt = sub4.toInt(); | |
analogWrite(led4, subInt); | |
subInt = sub5.toInt(); | |
analogWrite(led5, subInt); | |
subInt = sub6.toInt(); | |
analogWrite(led6, subInt); | |
} | |
} |
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
#!/usr/bin/env python | |
import cv2, serial, time | |
#motion detect | |
def diffImg(t0, t1, t2): | |
d1 = cv2.absdiff(t2, t1) | |
d2 = cv2.absdiff(t1, t0) | |
return cv2.bitwise_and(d1, d2) | |
#initiate serial connection | |
ser = serial.Serial('/dev/ttyACM0', 115200) | |
#multiply brightness value | |
ard_mult = 10 | |
#initiate camera | |
cam = cv2.VideoCapture(0) | |
#create window | |
winName = "Movement Indicator" | |
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE) | |
# Read three images first: | |
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
#main loop | |
while True: | |
#crop image to sections | |
main_image = diffImg(t_minus, t, t_plus) | |
cr_image1 = main_image[1:480, 1:106] | |
cr_image2 = main_image[1:480, 106:212] | |
cr_image3 = main_image[1:480, 212:318] | |
cr_image4 = main_image[1:480, 318:424] | |
cr_image5 = main_image[1:480, 424:530] | |
cr_image6 = main_image[1:480, 530:636] | |
#calculate mean brightness of image sections | |
cr_image1_val = cv2.mean(cr_image1) | |
cr_image1_val_int = (int(cr_image1_val[0]) * ard_mult) | |
cr_image2_val = cv2.mean(cr_image2) | |
cr_image2_val_int = (int(cr_image2_val[0]) * ard_mult) | |
cr_image3_val = cv2.mean(cr_image3) | |
cr_image3_val_int = (int(cr_image3_val[0]) * ard_mult) | |
cr_image4_val = cv2.mean(cr_image4) | |
cr_image4_val_int = (int(cr_image4_val[0]) * ard_mult) | |
cr_image5_val = cv2.mean(cr_image5) | |
cr_image5_val_int = (int(cr_image5_val[0]) * ard_mult) | |
cr_image6_val = cv2.mean(cr_image6) | |
cr_image6_val_int = (int(cr_image6_val[0]) * ard_mult) | |
#write values to serial port (Arduino) | |
serial_send = str(str(cr_image1_val_int).zfill(3) + str(cr_image2_val_int).zfill(3) + str(cr_image3_val_int).zfill(3) + str(cr_image4_val_int).zfill(3) + str(cr_image5_val_int).zfill(3) + str(cr_image6_val_int).zfill(3) + "\n") | |
ser.write(serial_send) | |
#add delay (Arduinio hardware needs a second to update) | |
time.sleep(1) | |
#display image in window | |
cv2.imshow( winName, main_image) | |
# Read next image | |
t_minus = t | |
t = t_plus | |
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
#terminate program on ESC key | |
key = cv2.waitKey(10) | |
if key == 27: | |
cv2.destroyWindow(winName) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment