Sep-19-2018, 02:26 PM
First of all, I am new to python.
I have arduino connected to my PC. I have a few button interfaced with my arduino. When a button is pressed I want an image to be displayed on my windows PC. Here's my arduino code. At the moment I am only testing button on pin 9, as you can see in code.
#define data0 5
#define data1 6
#define data2 7
#define data3 8
#define data4 9
int data[5];
void setup() {
// put your setup code here, to run once:
pinMode(data0, INPUT);
pinMode(data1, INPUT);
pinMode(data2, INPUT);
pinMode(data3, INPUT);
pinMode(data4, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
data[0] = digitalRead(data0);
data[1] = digitalRead(data1);
data[2] = digitalRead(data2);
data[3] = digitalRead(data3);
data[4] = digitalRead(data4);
if(data[4]==1){
Serial.println(1);
}
delay(100);
}
And here's my python code
I have arduino connected to my PC. I have a few button interfaced with my arduino. When a button is pressed I want an image to be displayed on my windows PC. Here's my arduino code. At the moment I am only testing button on pin 9, as you can see in code.
#define data0 5
#define data1 6
#define data2 7
#define data3 8
#define data4 9
int data[5];
void setup() {
// put your setup code here, to run once:
pinMode(data0, INPUT);
pinMode(data1, INPUT);
pinMode(data2, INPUT);
pinMode(data3, INPUT);
pinMode(data4, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
data[0] = digitalRead(data0);
data[1] = digitalRead(data1);
data[2] = digitalRead(data2);
data[3] = digitalRead(data3);
data[4] = digitalRead(data4);
if(data[4]==1){
Serial.println(1);
}
delay(100);
}
And here's my python code
import serial import time ser = serial.Serial('COM6', baudrate = 9600, timeout =1) def showPIL(pilImage): root = tkinter.Tk() w, h = root.winfo_screenwidth(), root.winfo_screenheight() root.overrideredirect(1) root.geometry("%dx%d+0+0" % (w, h)) root.focus_set() root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit())) canvas = tkinter.Canvas(root,width=w,height=h) canvas.pack() canvas.configure(background='black') imgWidth, imgHeight = pilImage.size if imgWidth > w or imgHeight > h: ratio = min(w/imgWidth, h/imgHeight) imgWidth = int(imgWidth*ratio) imgHeight = int(imgHeight*ratio) pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS) image = ImageTk.PhotoImage(pilImage) imagesprite = canvas.create_image(w/2,h/2,image=image) root.mainloop() while 1: arduinoData = ser.readline().decode('ascii') if arduinoData == 49: pilImage = Image.open("sample.jpg") showPIL(pilImage)Above python function to display the image works, I have tested it. The code to receive the arduino data also works fine, because when I do
print(arduinoData)I can see the data. But when I run the above code and press the button, nothing happens.