Skip to content

Week 14

Interface and Application Programming


Hero Shot of Week 14


Initial Version

I started by creating a simple user interface using the tkinter library in Python.

First, I imported the library:

import tkinter as tk

This library is used to create windows, buttons, and other UI elements.

Python Code

import tkinter as tk

def send_command(cmd):
    print("Sending command:", cmd)

def forward():
    send_command("FORWARD")
    label.config(text="Boat moving forward")

def stop():
    send_command("STOP")
    label.config(text="Boat stopped")

def left():
    send_command("LEFT")
    label.config(text="Boat turning left")

def right():
    send_command("RIGHT")
    label.config(text="Boat turning right")

root = tk.Tk()
root.title("Boat Control Simulator")

label = tk.Label(root, text="Boat stopped", font=("Arial", 16))
label.pack(pady=10)

tk.Button(root, text="Forward", width=20, command=forward).pack()
tk.Button(root, text="Left", width=20, command=left).pack()
tk.Button(root, text="Right", width=20, command=right).pack()
tk.Button(root, text="Stop", width=20, command=stop).pack()

root.mainloop()

Bash the Code

python boat_control.py

Then I created several functions:

forward()

stop()

left()

right()

Each of these functions sends a command using:

send_command(cmd)

At this stage, the function only prints the command:

print("send command:", cmd)

This means I simulated sending commands as if they were going to a microcontroller. After that, I created the main window:

root = tk.Tk()

And added:

a label to display the boat status buttons to control movement

At this stage, I created a basic UI that allows controlling the boat and displaying its state.


Why I Decided to Improve the Project

After creating the basic version, I realized that the project looked too simple and did not represent a complete system.

So I decided to improve it by adding more visual feedback, system behavior simulation and elements that make it closer to a real system.

My goal was to create not just buttons, but a control system with feedback.


Improvements I Added

The Code

import tkinter as tk

log = tk.Text(root, height=8, width=40)
log.pack()

def send_command(cmd):
    log.insert(tk.END, "Sending: " + cmd + "\n")

def forward():
    send_command("FORWARD")
    label.config(text="Moving forward", fg="green")

def stop():
    send_command("STOP")
    label.config(text="Stopped", fg="red")

    def auto_stop():
    root.after(3000, stop)  # через 3 сек стоп

def forward():
    send_command("FORWARD")
    label.config(text="Moving forward")
    auto_stop()

    import random

def update_sensor():
    value = random.randint(0, 100)
    sensor_label.config(text=f"Distance: {value} cm")
    root.after(1000, update_sensor)

sensor_label = tk.Label(root, text="Distance: 0 cm")
sensor_label.pack()

update_sensor()

def send_command(cmd):
    print("Sending command:", cmd)

def forward():
    send_command("FORWARD")
    label.config(text="Boat moving forward")

def stop():
    send_command("STOP")
    label.config(text="Boat stopped")

def left():
    send_command("LEFT")
    label.config(text="Boat turning left")

def right():
    send_command("RIGHT")
    label.config(text="Boat turning right")

root = tk.Tk()
root.title("Boat Control Simulator")

label = tk.Label(root, text="Boat stopped", font=("Arial", 16))
label.pack(pady=10)

tk.Button(root, text="Forward", width=20, command=forward).pack()
tk.Button(root, text="Left", width=20, command=left).pack()
tk.Button(root, text="Right", width=20, command=right).pack()
tk.Button(root, text="Stop", width=20, command=stop).pack()

root.mainloop()
  1. Command Log

I added a text area that shows the commands being sent.

Instead of using print(), I used:

log.insert(tk.END, "Sending: " + cmd)

This simulates communication between the UI and the device.

  1. Status Indicator

I updated the label and added colors:

green → moving
red → stopped

This makes the interface more user-friendly.

  1. Timer / Auto Stop

This simulates safety logic for example, the boat stops automatically after some time.

  1. Sensor Simulation

I added a function:

update_sensor()

It generates random values:

random.randint(0, 100)

This simulates sensor data, such as distance.


Operation Modes

I considered adding is Manual mode, Auto mode and This shows that the system can be extended further.


Problem I Faced

At first, I tried to directly insert the new code into the old code.

This caused errors such as:

random is not defined root is not defined

The reason:

I forgot to import random I used root before creating it the code was not structured properly

I was adding code without understanding execution order.


What I Learned

From this mistake, I understood: Code must be structured, All libraries must be imported, Objects must be defined before use, You cannot just copy and paste code — you must understand it and This helped me better understand UI programming logic.


Final Version

After that, I removed the old code and rewrote everything in a structured way. I added UI (buttons)command, log status, indicator sensor simulation.

Final Code

import tkinter as tk
import random

root = tk.Tk()
root.title("Boat Control Simulator")

def send_command(cmd):
    log.insert(tk.END, "Sending: " + cmd + "\n")

def forward():
    send_command("FORWARD")
    label.config(text="Moving forward", fg="green")

def stop():
    send_command("STOP")
    label.config(text="Stopped", fg="red")

def left():
    send_command("LEFT")
    label.config(text="Turning left")

def right():
    send_command("RIGHT")
    label.config(text="Turning right")

def update_sensor():
    value = random.randint(0, 100)
    sensor_label.config(text=f"Distance: {value} cm")
    root.after(1000, update_sensor)

label = tk.Label(root, text="Boat stopped", font=("Arial", 16))
label.pack(pady=10)

sensor_label = tk.Label(root, text="Distance: 0 cm")
sensor_label.pack()

log = tk.Text(root, height=8, width=40)
log.pack()

tk.Button(root, text="Forward", width=20, command=forward).pack()
tk.Button(root, text="Left", width=20, command=left).pack()
tk.Button(root, text="Right", width=20, command=right).pack()
tk.Button(root, text="Stop", width=20, command=stop).pack()

update_sensor()

root.mainloop()

Now the system:

receives user input displays system state simulates communication


Conclusion

In this project, I developed a user interface to control a mini autonomous boat. Even without a physical board, I was able to simulate control, implement a UI and demonstrate communication logic. This week helped me understand, how UI systems are built, how commands are transmitted and how embedded systems can be simulated.