13.Interface and application programming

This week is all about programming.It is to get introduced to using Code to Create Applications that Interface with input and output devices.

As I am totally new to programming the lecture made little sense to me. Neil spoke about different languages first and how to communicate with device interfaces. Then he spoke about the functionality of the programs and performance.

individual assignment:

writ an application that interfaces a user with an input &/or output device that you made

group assignment:

compare as many tool options as possible

check This link for this

What an API ?

“In computer programming, an application programming interface (API) is a set of subroutine definitions, protocols, and tools for building application software. In general terms, it is a set of clearly defined methods of communication between various software components. A good API makes it easier to develop a computer program by providing all the building blocks, which are then put together by the programmer. An API may be for a web-based system, operating system, database system, computer hardware or software library. An API specification can take many forms, but often includes specifications for routines, data structures, object classes, variables or remote calls. POSIX, Windows API and ASPI are examples of different forms of APIs. Documentation for the API is usually provided to facilitate usage and reimplementation.” There are many laguages that could be used to build applications that interface with Input and Output devices. Out of which are C++, JAVA, Python, Arduino, and Processing. In this section, we will learn on how to collect data from sensors through a microcontroller, and how to use coding to Simulate and Visualize the measurements in Graphical representations.

Group assignment

check this link

Individual assignment

For this week I ll be designing a python application to control my previous week assignment ,the stepper motor. TPython is an easy to learn, powerful programming language. I will use Tkinter i to make GUI based applications in python. Tkinter provides several GUI widgets like buttons,menu, canvas,text,frame,label etc

I have no worked with python or tkinter so at the beggining I ll try to learn about these program using Tutorial python and Tutorial Tkinter

At first , i used a simple code to run my interface application , so i added three buttoms with three different commands , for driving/ stopping the motor and to quit the application

Here it is th code

    import tkinter as tk

    class Application(tk.Frame):

    def __init__(self, master=None):
    super().__init__(master)
    self.pack()
    self.create_widgets()

    def create_widgets(self):
    self.tkButton1 = tk.Button(self)
    self.tkButton1["text"] = "Drive the motor"
    self.tkButton1["command"] = self.working
    self.tkButton1.pack()

    self.tkButton3 = tk.Button(self)
    self.tkButton3["text"] = "Stop the motor"
    self.tkButton3["command"] = self.stopping
    self.tkButton3.pack()


    self.tkEntry = tk.Entry(self)
    self.tkEntry.pack()

    self.tkButton2 = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
    self.tkButton2.pack()

    def working(self):
    print("hi there, I m working !")

    def stopping(self):
    print("hi there, I m not working !")

    root = tk.Tk()
    app = Application(master=root)
    app.master.title("Smart trapview")
    app.master.geometry("400x200")
    app.mainloop()

Because of the current situation about the covid 19 pandemic , i haven’t been able to use a servo motor as an output for my week assignment , however i have only an LED that’s why i decided to use it to work on this assignemet as explained bellow

I started by using an LED to create an interfae to put it “on” and “off” by sending a data to the arduino while typing a charachter (“L”)

import tkinteras tk
import serial

class Application(tk.Frame):

def __init__(self, master=None):
super().__init__(master)
self.pack()
self.mySerial = serial.Serial('COM4')  # open serial port
self.create_widgets()

def create_widgets(self):

self.tkLabel = tk.Label(self)
self.tkLabel.pack()

self.tkButton1 = tk.Button(self, text="On Me/Off Me!", command=self.sendData)
self.tkButton1.pack()

self.tkButton2 = tk.Button(self, text="Close Me", command=self.quit)
self.tkButton2.pack()

def sendData(self):
self.mySerial.write("L".encode())

 def quit(self):
 self.mySerial.close()
 self.master.destroy()


 root = tk.Tk()
 app = Application(master=root)
 app.master.title("LED Blink Blink ")
 app.master.geometry

Creating an application that Interface with my output device(servo motor)

As i became able to work in the fablab again , i decided to use my output card from the week output device and i will try to create an application to rotate my servo motor

Actually i decided to use the processing

It is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology. There are tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning and prototyping

So the first step is to download the software

Second Unzip the library and copy the “arduino” folder into the “libraries” sub-folder of your Processing Sketchbook. (You can find the location of your Sketchbook by opening the Processing Preferences. If you haven’t made a “libraries” sub-folder, create one.)

Now it is time to ulpoad my codes

In this case i m going to progrm my board using arduino , i just needed to make some changes

Tools-> Boards -> Arduino Pr mini

Firt Upload the Arduino sketch to the Arduino Uno. Once that is done, the sketch starts running on the Arduino. Then run the Processing sketch in Processing. Check to be sure the Processing code is using the same COM port as the Arduino is connected to on your computer. When the Processing sketch runs, that should display the Processing canvas with the graphic on it. Now press the ‘u’ or the ‘d’ keys on the computer keyboard and the graphic should animate and the servo motor should move as in the video above

Then run the Processing sketch in Processing. Check to be sure the Processing code is using the same COM port as the Arduino is connected to on your computer. i my case it is COM5 .When the Processing sketch runs, that should display the Processin canvas with the graphic on it. Now press the ‘u’ or the ‘d’ keys on the computer keyboard and the graphic should animate and the servo motor should move as in the video above

Arduino servo code

Processing code