Okay , you clearly mentioned you need the reason why iam doing this , so you could help me to possibly land on a easy solution, okay ! Iam sorry but this going to be a long one , but i will try to compress as much as possible and i will try my best!
poof! here goes:
Iam doing a Visible Light Communication project, where my smartphone (Redmi note 5 pro) Flashlight becomes the source. So i rooted my phone and wrote a shell script
binary sequence =[0,1,0,0,.......(blah blah , some binary sequence) .....]
for in in binary sequence:
if[i=0]:
echo "0" > /sys/class/leds/flashlight/brightness //turns off the flashlight
sleep(0.001) // delay of 1 ms
else:
echo "100" > /sys/class/leds/flashlight/brightness //turns on the flashlight
sleep(0.001) // delay of 1ms
Expectation :
So a 0 is just 0v pulse and a 1 is a 5 v pulse , just sample at the reciever side at 1 ms and you are good to go.
Reality:
As a smartphone is a General Purpose OS based system, you can't expect any guarantees that too in the milli second level, what happened is the pulse width gets streched beyond the 1ms limit , thus the reciever gets its synchronization all wrong! for eg if you sent a 110 , the reciever would record a 1 pulse for 5 ms (due to OS delay , even if i programmed it to be on for 1 ms it gets to 5 ms) , another 1 for 10 ms and a 0 for 2 ms , and it would output as 11111 1111111111 00 , instead of 110!
The picture demonstrates this . Now how would you encode data reliably?
Solution:
Encoding 1s and 0s in the form of their high-time pulse width. A 1 is any pulse that has an on time of 10 ms or higher , else it is a 0 ( A zero pulse has an on time and an off time , it has got no programmed delay , practically a zero pulse has a 256 us (mean pulse width) on time and 256 us (mean) off time , whereas a one pulse has a 20 ms on time(mean) and 256 us (mean) off time. A code would do better for you
for in in binary sequence:
if[i=0]:
echo "100" > /sys/class/leds/flashlight/brightness
echo "0" >/sys/class/leds/flashlight/brightness /* these two statements produce an on off pulse that has a mean on time of 256 us and mean off time of 256 us*/
else: // if i is 1
echo "100" > /sys/class/leds/flashlight/brightness
sleep (0.01) // 10 ms delay
echo "0" >/sys/class/leds/flashlight/brightness /* these two statements produce an on off pulse that has a mean on time of 20 ms and mean off time of 256 us .*/
Pictorially this is how it looks! as i said earlier 1 and 0 both bit's symbol has a high time and low time, i only differentiate 1 and 0 based on their respective symbol 's high time(flashlight on) , i don't even care about their low time (flashlight off) as depicted in the picture .
Reality :
This solves my problem as i can clearly differentiate between a 1 whose high time is more than 10 ms , else it is a 0 .
Problem with this method :
One Word : LOW DATA RATE!!!!! , iam differentiating 1 and 0 in time and thus knowingly reduce my data rate , i need a data rate of at least 1 Khz, this shit's just 52 Hz!!! One could go einstein and say , reduce the on-time delay for 1 to something around 5 ms and change the receiver algorithm to anything >= 5 ms as 1 else :0 , good one mr einstein, but in reality the 0 pulse on-time gets streched upto 7 or 8 ms!!! ,leading to bit error! that's why i fixed 10 ms as the threshold to seperate a 1 from a 0.
I thought, i want to increase my data rate , but if i reduce the delay for diffrentiating 1 from 0 i run a risk of bit error , now how do i solve this?
A Light Bulb on the top of my Head:
Now you get caught when you only differentiate 1 and 0 based on pulse width, why dont you also differentiate them based on voltage so that even if pulse width fails, voltage hails! sorry for the punch! Here is my code:
for i in binary sequence:
if[i=0]:
echo "1" > /sys/class/leds/flashlight/brightness // a lower intensity flashlight produces a lower voltage
echo "0" >/sys/class/leds/flashlight/brightness
else: // if i is 1
echo "200" > /sys/class/leds/flashlight/brightness // a higher intensity flashlight produces a higher voltage.
sleep (0.005) // 5 ms delay yikes!
echo "0" >/sys/class/leds/flashlight/brightness
The image that i have attached in my very first post is the output except i added the 5 ms delay for 0 rather than 1 , nothing else .
Now my receiver checks if i receive a pulse it should satisfy 2 conditions to be a 1:
- It should have a delay >=5 ms
- It should have a voltage greater > 10V.
Now for a 0 pulse, it has a risk of having high time pulse widths greater than 5 ms, but it gets caught at the second condition and rightly gets identified as 0 , , provided condition 2. is always reliable!!
I have attached my receiver code (arduino code) in my previous post , there i have applied the conditions to 0 rather than 1 .
Expectation:
This is my story, i hope you can understand why i want this approach to work desperately.
PS: AGAIN, Sorry for the extremely long post and sorry for my funny tone!! .
PS AGAIN ! : I am sorry guys, as per my code any voltage less than 2 v would be automatically 0 as it is equivalent to a digital pin's off! , so just care about 2 levels, >10 v and <10 v