how to program with time variabel?

Hello everyone, i am programming something to send text with an LED and receive it with a phototransistor.

everything seems to work fine, however I have one problem. If 2 zeros are sent in a row, the receiver wont see any difference in it.

I programmed it to store a zero if the readout value is below a certain value, and store a one if it is above that value. But in that way the arduino won't see any difference between one zero of multiple zeros.

I was thinking of something like:

if(LDRReading > 400) for (200 milliseconds) {
...

But that isn''t a working function ofcourse
Is there anyone who has an idea to fix this?
I already thought of using the Millis function, but I don't know how to implement it.

Thanks in advance,
Nick

set an interrupt to be triggered whenever it get's a high input (high light) , then wait for a specific time duration , then use the same time D to receive 8 bits after the first sync signal .

so you need to send 9 bits , the first one for sync and then 8 to carry data .
now for every bit , you send the bit signal (whether high light or no light) and then wait for a specific time period , that the receiver knows , for the receiver to be able to distinguish the bits

first a sync , then constant time intervals .

amine2:
set an interrupt to be triggered whenever it get's a high input (high light) , then wait for a specific time duration , then use the same time D to receive 8 bits after the first sync signal .

so you need to send 9 bits , the first one for sync and then 8 to carry data .
now for every bit , you send the bit signal (whether high light or no light) and then wait for a specific time period , that the receiver knows , for the receiver to be able to distinguish the bits

first a sync , then constant time intervals .

Thank you!
I had something like this in my mind, but now I understand how I need to do it, thank you very much!

Check out the Manchester library.

Mitsubishi Labs White Paper on Led to Led Wireless Communication.

For bits they used either a short or long pulse in a fixed time frame longer than either. Using the same led to send and receive they were able to achieve 250 bits/sec both ways at the same time.

Lots of lessons in that paper, be sure.

Thanks for the help aarg and GoForSmoke, I will read the paper and check out the Manchester library

The paper has a lot of information about leds and use but the parts I think you can get the most out of for this project is the short and long pulses of light to make 0 and 1 rather than absence and presence of light on the detector. They read sensor 50x per milli to see X-many reads HIGH (or was that LOW?) as 0 and Y-many as 1 that lets them be sure what they get without false bits. Never just one read to establish the bit. It must work since they say 'rock solid' communication.

Since you are not using a led as detector but rather phototransistor (fast, unlike LDR.. photocell) the chances are that you can match or beat their baudrate at least in one direction.

or you can also use an other principle , you can set the same interrupt to be triggered whenever the pulse is high , then start incrementing a counter until it's low again it's similar to PWM .
make sure the divisor of the input value is higher than the time interval of the interrupt calling and the starting of the counting process until the end , that way you can avoid noise and miss-calculations of values .

I understand what you mean amine2 and GoForSmoke, but I don't know at all how to program my arduino to match their baudrates, do you maybe have a short example code so I can understand it?

and for amine2's idea, the counter you are talking about, do you need the Millis function for that?

I am starting to get a better view of what to do, but I still don't really know how.

Thanks for your advice!

Take a look at this and see if it makes sense to you.

It is a many-things-at-once tutorial with explanations of Why to do along with How and What to do.

For example, the point about code that blocks execution (delay function is one) and code that does not, called non-blocking. It is a simple fundamental and he tells the how and why of it simply.

But what if you want to blink the two LEDs at different rates? Like, once a second for LED 1 and twice a second for LED 2?

This is where the delay function doesn't really help.

Let's look at an analogy. Say you want to cook breakfast. You need to cook:

Coffee - takes 1 minute
Bacon - takes 2 minutes
Eggs - takes 3 minutes

Now a seasoned cook would NOT do this:

Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.

The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:

Start frying eggs. Look at watch and note the time.
Glance at watch from time to time. When one minute is up then ...
Start cooking bacon. Look at watch and note the time.
Glance at watch from time to time. When another minute is up then ...
Put coffee on. Look at watch and note the time.
When 3 minutes are up, everything is cooked. Serve it all up.

In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

henkbert:
Hello everyone, i am programming something to send text with an LED and receive it with a phototransistor.

. . .

There is another, very simple alternative of using the softwareSerial library library to achieve this, so you don't have to invent your own coding.
I've used this: http://www.zolalab.com.br/references/serial2ir.php as a basis in one of my projects. I had to use a slightly different configuration of the phototransistor, however.

I wrote some code, it worked, but when I uploaded it the second time it didn't showed anything on my serial monitor...

int LDR_Pin = A0; //analog pin 1
unsigned long inter1;
unsigned long inter2;
unsigned long y = (inter2 - inter1);


void setup(){
  Serial.begin(9600); 
}

void loop(){
  int LDRReading = analogRead(LDR_Pin); 
  if (LDRReading > 400){
  inter1 = micros();
  Serial.println(inter1);
}
while (LDRReading >400){}
inter2 = micros();
Serial.println(inter2);

if ((y > 2500000) && (y < 3500000))
{
  Serial.println(y);
  Serial.println("0");
}

else if ((y > 5500000) && (y < 6500000))
{
  Serial.println(y);
  Serial.println("1");
}
}

When my LED is burning, the value of the LDR is going above 400 (I already ordered a photosensor, but for testing I used a LDR now)

Anyone who can spot a mistake in my code?

henkbert:
Hello everyone, i am programming something to send text with an LED and receive it with a phototransistor.

Using a "phototransistor"?
Or a LDR (photoresistor)?

Phototransistors are relatively fast.
Photoresistors are relatively slow.

I think the achievable data rate with photoresistors (LDR) is much slower than with a phototransistor.

So what do you have? Phototransistor or photoresistor?

henkbert:
I wrote some code, it worked, but when I uploaded it the second time it didn't showed anything on my serial monitor...

int LDR_Pin = A0; //analog pin 1

unsigned long inter1;
unsigned long inter2;
unsigned long y = (inter2 - inter1);

void setup(){
 Serial.begin(9600);
}




When my LED is burning, the value of the LDR is going above 400 (I already ordered a photosensor, but for testing I used a LDR now)

Anyone who can spot a mistake in my code?

The line:
unsigned long y = (inter2 - inter1);
is executed only once and at program start. I guess this should be in your loop. That may not be your only problem.

partly also in answer to the point raised by Jurs . . .
When I did something similar, it was with a phototransistor which was balanced for IR, and I had the best results with one which had a daylight blocking filter. It looked line a small, black LED and had only 2 wires (emitter and collector) eg VISHAY TEFT4300 or Osram Components SFH 309 FA .

The analog read is a mistake. That takes too long, over 100 usecs per bit.

A digital input will switch from LOW to HIGH at about 3 volts and from HIGH to LOW at 1.1 volt.
LOW can be 0V to under 3V but once it switches, HIGH can be 1.2V to 5V. It takes a definite change in voltage to change pin state which is how digital pins stay stable.
Less than 1.1V is always LOW, more than 3V is always HIGH, partway between causes no change.

Your analog input just controls how many volts the pin gets.

Digital read happens fast. With pinMode() set, you can read the port and get the pin value in about half a microsecond if that.

The LDR.... try 1200 300 baud first.

Other thing is that if you don't use IR or red led and an IR detector (phototransistor with black bulb), any visible light can interfere with your comms.

I've bought 2 phototransistors, But in this test I still used an LDR, which I bought earlier. I will try to attach the phototransistors to a digital input. Thanks for the help, I learn a lot from your help