# Project#1 - Pycharm, pyseril, and Arduino comms (LCD readout) # Information - In this project, we're going to import some modules, connect to the RPInoRPI and send over some commands import time # The 'time' module is included with pycharm and allows for us to create delays. import serial # The 'serial' or 'pyserial' module needs to be imported. See the project video for more info. # Note that serial/pyserial is the module that allows for us to communicate with external microcontrollers. # Remember that you need to program your RPInoRPI with the code for project#1. # The first thing that we need to do is to talk to our RPInoRPI. try: # Try to establish communication with the RPInoRPI board. if successful, only the 'try:' will be executed MCU = serial.Serial(port='COM10', baudrate=115200, timeout=0.01) # See project video for more info on this line. time.sleep(1) # Wait one second except: # If communication with the RPInoRPI could not be established, then perform the following instead: print('System Locked - Cannot communicate with MCU. Close this program, then reconnect to MCU and restart program') while True: # Forever loop. Close the program and start again. Check connection to the RPInoRPI pass # Do nothing def write_read(x): # This function acts to send data (x) to the RPInoRPI. Nothing more. MCU.write(bytes(x, 'utf-8')) # Write the value stored in 'x' to the RPInoRPI time.sleep(0.2) # Wait 0.2 seconds/200ms # Main Code x = 0 # Create a variable named x and set it to 0 counter = 0 # Create a variable named 'counter' and set it to 0 while True: # Run the following code until you close the program (loop forever) x = str(counter) # x now equals the string value in counter. Counter is an integer. We can't send integers. write_read(x) # call the write_read function and send 'x' along with it. We can only send string data counter = counter + 1 # Increment counter. Or rather, add 1 to counter. Counter is an integer. A while number. if counter == 10: # If counter is 10, reset to 0. We will send values 0 through 9. counter = 0 # Only set counter to 0 when counter equals 10 time.sleep(2) # Wait for 2 seconds then start over # This is the end of the first project. Not too bad, right? Note that your Arduino is going to read ASCII format # which means that when we send a 5, we're going to get the ASCII representation of 5 at the RPInoRPI. # More info on this in the project video. # We used two integers for this program: x and counter. We could have used only 1, but I didn't want to confuse things. # We needed to keep one as a number, and the other as a string. We can only send strings to the RPInoRPI. # A string is a character or set of characters like "Hi there" or "123ABC". Anything in quotes. # An integer is a whole number that we can use for numeric purposes. For instance, 1 or 100.