15. Interface and Application Programming¶
This week Hero shot!¶
This week Checklist¶
Group assignment
- [✓] Compare as many tool options as pobbible
- [✓] Document our work
Individual assignment
- [✓] Write an application that interfaces a user with an input and/or output device that you made.
Group assignment¶
This week, our group try to do compare as many tool ptions as pobbible. Please click here to see more details of our group assignment.
Individual assignment¶
This week I write an application that interfaces a user with an input device.
█ Step 1 - Update pip and Install pyserial and pygame library first. Because when writing python programs, you will need to. P.S. After install pyserial and pygame, please restart your computer for them to take effect.
pip install --upgrade pip
pip install pyserial
pip install pygame
█ Step 2 - Connect potentiometer to the board. Then I will write an arduino program to read the value of the potentiometer and pass it to the serial.
▼ Arduino program for SAMD11C board
int inputPin = 4; // Input sensor pin number
void setup() {
pinMode(inputPin, INPUT); // Set pin mode (INPUT / OUTPUT)
Serial.begin(9600); // sets the data rate in bits per second (baud) for serial data tranmission
}
void loop() {
int inputValue = analogRead(inputPin); // read inputPin analog signal (0-1023)
Serial.println(inputValue); // output to serial monitor or plotter
delay(100); // delay 0.5 second
}
█ Step 3 - Write python program and run on my notebook computer. The program will read message from serial and create GUI to show potentiometer value.
▼ Python program for computer
import serial # import pySerial library
import pygame
COM_PORT = 'COM6' # set com port name
BAUD_RATES = 9600 # set baud rate
ser = serial.Serial(COM_PORT, BAUD_RATES) # init serial port
pygame.init()
screen = pygame.display.set_mode([1123,400])
screen.fill([255,255,255])
try:
while True:
while ser.in_waiting: # when the signal is received
data_raw = ser.readline() # read line
data = data_raw.decode() # decode raw data
print(data) # printout data
x = int(data) + 50
screen.fill([255,255,255])
pygame.draw.circle(screen, [255,128,0],[x,200],10,0) #(windows, colour, circle_xy, circle_radius, border_width)
pygame.display.flip()
except KeyboardInterrupt:
ser.close() # clear serial object
print('bye!')
pygame.quit()
▼ When I rotate the potentiometer, the circle is moving on the x axis.
Downloads¶
Arduino program for SAMD11C board week15_arduino.ino
Python program for computer to monitor MCU and draw a circle on screen week15_python.py