Hi! Welcome to Week 16


This week’s assignment was to write an application that interfaces with an input and/or output device that I made!

What I Did This Week


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 

Next, I followed the instructions in the video to write this Arduino code:

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);
    }
    
  }
}

Now that the codes were ready, the following steps are:
-First: burn the bootloader on the board using Arduino as ISP.
-Second: connect the FTDI to the board and unplug the ISP cable.
-Third: upload the program using the FTDI.
-Forth: head to python code and run it, it should control the light of the pin assigned, mine was 13 (build in LED).
This is a video of how the options of python’s window controlled the light of my board.



Thoughts on Python



Although I had done some research on Python in the past and was interested in learning it, I never really had the time for it. This week was a great opportunity to start with it, and I really hope to get to learn more about it in the future. Although i have been familiar with C language since I was a bachelor's degree student, I have found the using Python is easier. Python gave me more freedom in programming, I didn't need to add pointers or declare the variables' types I used, and given the very limited time I had to learn it, I believe that learning it was somehow simple. Python also had a wide library of functions. Overall, I really liked Python, and the only downside I noticed was that it was somehow slower than C, after doing some research, it turned out that it's because C has compiled language. The complete source code is converted into a machine language which is easier for a computer to understand. Python on other hand is interpreted. The interpreter reads each statement line by line. This makes python slower compared to C.

I wanted to have some fun with Python, I found a cool tutorial here online that shows how to program a connect4 game GUI on python, it’s cool to try it out and play with friends, you can download the code from here.

Last Week's Assignment

Next Week's Assignment