1

I'm pretty new to Python and am trying to do something a bit tricky. So essentially I'm trying to send data over light; just a small amount of text. So I have a line of code to encode the ASCII to binary, and that works fine and leaves me with a list with the binary as strings like

n=['0','b','1','0','1'] 

and so on. I have one raspberry pi set up to send with the code below, and one to receive with the code further down. It all seems to work but I think the timing is off between the two and the list at the received end is shifted and sometimes has random 0's where there shouldn't be. (I think it's reading faster than sending). Is there any way to fix this that you can easily see? The for loops both start at the same time via pushbutton.

Sending:

For x in range(2,130):
   If myList[x] != '1':
       Led.off()
       Sleep(.5)
       Led.off()
   Elif myList == '1':
       Led.on()
       Sleep(.5)
       Led.off()

Receiving:

For x in range(2,130):
   If gpio.input(14) == True:
       myList[x] = '1'
       Sleep(.5)
   Elif gpio.input(14) ==False:
       myList[x] = '0'
       Sleep(.5)

The gpio.input(14) is connected to a photodiode that is receiving the signals from an led. I'm assuming the code for receiving runs faster than the sending code and why timing is off but I don't know how to fix it.

1 Answers1

0

I imagine the problem is that writing to a list may take slightly more or less time to complete as an action than setting a GPIO pin. So each loop cycle it gets more and more out of sync until it's not reading the right things. What I would do is add another LED and another photodiode, on the opposite raspberry pi than the first ones are on, to output a "received" signal. So your code would look something like this:

Sending:

For x in range(2,130):
  If myList[x] != '1':
    Led.off()
    Sleep(.5)
    Led.off()
  Elif myList == '1':
    Led.on()
    Sleep(.5)
    Led.off()
while GPIO.input(receivepin) == False: #wait until 'receive' pin reads a value
  Sleep(0.1)
#here you might want to also add another 
#pulse to let the reciever know that another piece of data is about to be sent

Recieving:

For x in range(2,130):
If gpio.input(14) == True:
   myList[x] = '1'
Elif gpio.input(14) == False:
   myList[x] = '0'
Led.on()
#wait until the sender puts out a pulse to signal the next piece of data

This may not be the best way to do it, but you get the idea. Basically you want to eliminate the time delays and replace them with bits of code that wait until a certain parameter is met.

Mension1234
  • 99
  • 1
  • 10