This week’s assignment was to write an application that interfaces with an input and/or output device that I made!
To me, this week is going to be one that will be crucial to my final project. This week I design the game interface of the final project, and having it ready by the end of this week would be a great relief when it’s to put the project parts together. The plan for this week is not only to design the game, but also to figure out a way to connect the inputs of the sensors and all to the game coding, in a way to make it get the signals from the sensors rather than from the keyboard or other parts.
By the beginning of this week, I had already been looking into game engines that would be easy for me as beginner to design my game on. I spent some time working with the Construct 3 Software, but after some time I found it to be quite hard and complicated to use it as a beginner. I started searching again on where I can build the game, and I found that Unity engine had many beginner users, and it also had many tutorials available online, so I started the process again using it.
I had spent almost 4 weeks on this game when I realized that it isn’t going nowhere and that I have to change my final project. so for this week I started working on a python program that will control the LED on my board from weeks ago. Our instructor Nadine gave us a walkthrough the program and programming logic.
Now. I had to start the work. I first downloaded Python 2.7.14 from here. However, one can’t work solely on python, and has to download an interrupter, I downloaded PyCharm, from this link. After that, I started coding. Beside the program our instructor showed us, I looked for tutorials online, and found this video teaching how to write a Python program and an Arduino program to control an LED on an Arduino program using serial communication.
I opened PyCharm, and followed the tutorial video (and mixed in some of the code we’ve worked on with Nadine) and prepared this code:
import tkinter as tk # importing the library used for graphical interface
import serial
arduinoData = serial.Serial('com3','9600') # com number and baud rate
def led_on(): #defining a function
arduinoData.write(b'1') #sending a signal to arduino
def led_off():
arduinoData.write(b'0')
win = tk.Tk()
win.title("LED Control") #window's title
win.configure(background="lightseagreen") #to change the background colour
button=tk.Button() #defining a variable (Button() is a reserved function)
top = tk.Frame() #defining a variable as a frame
middle = tk.Frame()
bottom = tk.Frame()
top.pack() #pack is important to confirm a variable
middle.pack()
bottom.pack()
title= tk.Label(top ,text="Light on Board", font=('Bodoni MT Black',30),background="white") #title here is a variable to add a text
title.pack()
Name= tk.Label(middle, text="turn the LED on or off", font=('Arial BLack',20),background="lightseagreen")
Name.pack(side= tk.LEFT )
#port=tk.#Text(win)
#port.#pack()
led_on= tk.Button(bottom, text="On", font=('Arial Black',16),background="Green", command=led_on) #creating a button, command calls a function
led_off=tk.Button (bottom, text="Off", font=('Arial Black',16),background="red", command=led_off)
led_on.pack(side= tk.LEFT) # "left" to stack the buttons next to each other rather than make them under each others
led_off.pack(side= tk.LEFT)
win.mainloop() #to keep the window open
int pin = 13;
int serialData= Serial.read();
void setup(){
pinMode (pin,OUTPUT);
Serial.begin(9600); // setup serial
}
void loop(){
if(Serial.available() > 0 ){
serialData = Serial.read();
Serial.print(serialData);
if (serialData == '1'){
digitalWrite(pin,HIGH);
}
else if (serialData == '0'){
digitalWrite(pin,LOW);
}
}
}
.gif)