As title say, I want to send data (i.e. text later .bmp file) from one Arduino to another by laser turn ON and OFF respectively HIGH and LOW and receive it from LDR.
I have to use Matlab as Sender and Python ( Visual Studio Code) as Receiver side.
So far, I have managed the task successfully when I use Matlab for both sides.
Video in Link : Short Video
In the video, there might be shown some other electronic components, but I deactivated all of them so I don't need them. The circuit is very simple:
Sender: Laser Module, S goes to Arduino Digital 3, GND to Arduino GND
Receiver: There is a resistor serial to LDR where LDR reader pin goes to Arduino Digital 3 Pin, GND-GND. That's all. Remaining components are irrelevant.
Sender Code:
a = arduino("COM5","Uno");
foku = fopen('test.txt','r');
if true
writeDigitalPin(a,'D3',1);
while ~feof(foku)
byte_content = fread(foku, 1, 'uint8');
if byte_content>-1
b1 = bitget(byte_content, 1:8);
for j=1:1:8
fprintf("%d",b1(j));
writeDigitalPin(a,'D3',b1(j));
pause(1/100);
end
end
end
fclose(foku);
end
Reciever Code:
b = arduino("COM6","Uno");
fyaz = fopen("new123.txt","w");
if true
while readDigitalPin(b,'D3')~=1
fprintf('waiting-');
end
for x=1:1:26
dizi=(8);
for j=1:1:8
dizi(j)=readDigitalPin(b,'D3');
fprintf('%d',dizi(j));
pause(1/100);
end
sayi=0;
for c=1:1:8
sayi=sayi+(dizi(c)*power(2,c-1));
end
fwrite(fyaz,sayi,'uint8');
end
fclose(fyaz);
end
What I'm asking, I couldn't make it work when I use Python as receiver. It starts well for first 3-4 bytes and then I get wrong characters due to wrong bits pattern. As you all assume, at some point there is a synchronize problem sender and receiver.
Are they give same delay time, Matlab pause(1/100) function and Python time.sleep(1/100)?
Here is my draft code in Python:
import pyfirmata
import time
board = pyfirmata.Arduino('COM6')
f = open("demo.txt", "w")
f2 = open("demo2.txt", "w")
button = board.digital[3]
it = pyfirmata.util.Iterator(board)
it.start()
button.mode = pyfirmata.INPUT
time.sleep(3)
while button.read() != 1:
print("waiting-")
for i in range(7):
dizi = [0,0,0,0,0,0,0,0]
for j in range(8):
dizi[j] = board.digital[3].read()
f2.write(str(int(dizi[j])))
time.sleep(1/100)
sayi=0
for c in range(8):
sayi = sayi+(dizi[c]*int(pow(2,c-1)))
print(str(sayi))
f.write(chr(sayi))
f.close()
f2.close()
I'm going to record and upload a video for this code soon. So, you all may get know well where I'm missing something. I would be happy if you teach me better way to handle this task.
Thank you all for your interests.