This is bad code:
i = 0
y = 1
while True:
data = (85)
print(data)
if button()==0 and y == 1:
y = 0
while i < 230: # 229 + a 0 makes 230
data = r7[i]
print(data)
i += 1
time.sleep_ms (1)
if i == 230: # 229 passed the while argument then i += 1 makes 230
data = (170)
print(data)
i = 0
y = 1
Is y supposed to be a button debouncer? It doesn't work. You should wait for the button to be released before checking again if it is pressed.
A bigger problem is "while i < 230:". Looping a fixed number of times forces r7 to always be the same size. If you ever change the length of r7 you need to remember that the count must be updated in the while. You also have to count, which people are notoriously bad at. Let python count how many numbers are in r7 and loop through the numbers. Like this:
while True:
if button() == 0:
print(85)
for number in r7:
print(number)
print(170)
while button() == 0: # wait for button release
pass
Why do you sleep between printing the numbers in r7? There is no reason for this. The PC program is not going to read any bytes until 1000 bytes have been written.
You do not set a timeout, so serial_connection.read(1000) waits until it has read 1000 bytes. Your pico program only sends (prints) around 820 bytes (calculated using r7 as example data). The PC program must wait for multiple sends before it runs. The length of the printed data will vary depending on the values in r7, so using a fixed length read is also a problem. You print at least 3 bytes for each number (1 digit and \r\n) and at most 5 bytes (3 digits and \r\n), and you print 232 numbers (230 in r7 plus 85 at the start and 170 at the end). This means the pico may print anywhere between 699 and 1159 bytes. The pico needs to tell the PC program when to stop reading bytes.
You could use a special marker to indicate the end of the transmission. This lets you use Serial.read_until(marker) to read until it sees the marker. Pick a marker that will not otherwise appear in the data you are printing (like "|"). I would also stop printing the numbers one at a time and instead print everything using one print statement, making a string that contains the prefix (85), the suffix(170), all the numbers (r7) and the end of message marker ("|"). Something like this:
from machine import Pin
button = Pin(22, Pin.IN, Pin.PULL_DOWN)
end_of_message = "|"
r7 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while True:
if button() == 0:
msg = ",".join(map(str, [85] + r7 + [170]))
print(msg, end=end_of_message)
while button() == 0: # wait for button to be released.
pass
On the PC side the program would look like this:
import serial
end_of_message = b"|"
serial_connection = serial.Serial("com9", 115200)
while True:
data = serial_connection.read_until(end_of_message) # Read until we get end of message marker
print(data)
numbers = [int(x) for x in data.split(b",")] # convert bytes to array of int.
print(numbers)
I don't know if the serial_connection.read() will return the end of message marker. If so, it must be removed before converting the bytes to an array of int. The default marker for Serial.read_until() is \n. As long as we don't include any newlines in the message, we can use that as the end of message marker and use rstrip() to remove it from the data.
pico program
from machine import Pin
button = Pin(22, Pin.IN, Pin.PULL_DOWN)
r7 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while True:
if button() == 0:
msg = ",".join(map(str, [85] + r7 + [170]))
print(msg)
while button() == 0: # wait for button to be released.
pass
Program on PC
import serial
serial_connection = serial.Serial("com9", 115200)
while True:
data = serial_connection.read_until() # Read until we get end of message marker, \n
print(data)
numbers = [int(x) for x in data.rstrip().split(b",")] # convert bytes to array of int.
print(numbers)
I'm unclear about how to write the data to a file, so I left it off. But it would probably be one of these.
# do you really want a binary file? This writes the numbers as binary data.
with open(destination_file, "ab") as file:
destination(file.write(numbers))
# or write the csv string since it is a .txt file?
with open(destination_file, "a") as file:
destination(file.write(data.decode("utf8")))
Instead of leaving the file open, open the file, append data, close file. If you want the file to be empty at the start of the program. This writes the values to a CSV format file.
import serial
destination_file = "/Users/Beheerder/Dropbox/documenten arjo/proj raspberry pico/pico sending data to PC/store.txt"
with open(destination_file, "w") as file: # Create empty file or empty existing file.
pass
serial_connection = serial.Serial("com9", 115200)
while True:
data = serial_connection.read_until() # Read until we get end of message marker, \n
with open(destination_file, "a") as file:
file.write(data.decode("utf8"))