Raspberry Pi – Code Explanation

This first section imports the necessary modules / packages required to run the code and sets up the serial communications between Arduino and RPi.

The second section of the code sets up the pysimpleGUI elements in the main Menu window (table and touchscreen buttons), as well as the arrays for data stored in the table (table headers and values). The Menu window will open in fullscreen mode.

The third section of the code configures the Add Item window, for users to key in user input. The slider elements are for users to indicate the number of pills to be dispensed for each type of pills, while the spin elements are for users to indicate the time of dispense for the pills. The Add Item window will open in fullscreen mode in front of the Menu window when the “Add Row” button is pressed on the touchscreen in the Menu window. When the “Save” button is pressed in the Add Item window, the Add Item window is closed and the new entry will be appended as a new row in the table in the Menu window.

The fourth section of the code configures the Edit Item window. The Edit Item window will open in fullscreen mode in front of the Menu window when an existing row is selected and the “Edit Row” button is pressed on the touchscreen in the Menu window. The interface is exactly the same as the Add Item window, just that when the “Save” button is pressed in the Edit Item window, the Edit Item window is closed and the new entry will replace the existing selected entry in the table in the Menu window.

The fifth section of the code allows for the local time to be displayed on the RPi touchscreen in real-time.

The sixth section of the code receives bytes from Arduino and decodes them as messages of string data type.

The seventh section is the main loop of the code. This integrates all the sections before to create a comprehensive interface for users. Aside from “Add Row” and “Edit Row”, there is also a “Delete Row” button which allows for the permanent deletion of an existing entry in the table in the Menu window.

More importantly, this segment of the code accounts for the serial communications between RPi and Arduino as well, to display a pop-up informing users of the pill dispensing when the dispense time is reached and the dispense information for that specific time is sent from the RPi to the Arduino, as well as to display a pop-up upon confirmation from the Arduino to the RPi about the completion of pill dispensing for that specific dispense time.

The seventh segment of the code is the check_function, to constantly compare the real-time clock with the dispense timings stored in existing entries in the table. When the current time = dispense time for an entry in the table, there will be a pop-up to notify users that the pill dispenser is dispensing the pills according to that particular entry, and the details (quantities of each pill type) in that particular entry will be sent to the Arduino via the dispense_send function.

The last section of the code is the dispense_send function allows the sending of information in the particular entry (where current time = dispense time) to the Arduino, for the dispensing mechanism of the pill dispenser to work. The messages are sent to the Arduino in string data type.

Leave a Reply

Your email address will not be published. Required fields are marked *