Running two pieces of code at once

I have just figured where my problem is (arduino thinking that someone got out while it actualy got in). Problem is in this part:

    if ((analogRead(SENSOR2) > 120 ) && (prej2 == 0)){
        sensor2Time = millis();
        timeout = sensor2Time + TIMEOUT;
        prej2 = 1;
  if ((analogRead(SENSOR1) > 120) && (prej1 == 0)){
       sensor1Time = millis();
       timeout = sensor1Time+ TIMEOUT;
       prej1 = 1;

So because the code is linear the first bit of code runs before the second. So that thing thinks that someone got out. If I change the order of this two pieces the problem is reversed. So how can I get around this problem?

Hmmm I won't say that I am mad because I crossed that line a while buck. But this thing is slowly driving me super-mad. ;)

So I can't get this thing to work! It senses one direction normaly but the other is working like crap! If you move slowly thing works like expected but if you go just a little faster - you are doomed. So one direction accuracy nearly 100%, the other less than 50%. So I know where the problem is but I don't have any idea how to solve it. You just have to walk in at a right moment.

Problem is actualy quite simple. The solution to the simple problem is hard.

Hey roli,

Im stepping in from this thread here:

Can you post all your code please.

Also, can you describe the physical setup. How close are the 2 sensors to each other? Perhaps the answer to this question is to increase the distance between the 2 sensors.

[edit]Another thought: Perhaps some one can chime in here as my background in electronics is not strong but you're looking for a analog reading of over 120 which is anything over 0.59 volts. Perhaps you can build a simple circuit that will add 1.41 volts to the sensor reading. Therefore when the sensor detects motion, 1.41 volts + 0.59volts (output by sensor) = 2volts = TTL high. You can now hook the sensors upto the interrupt pins and use interrupts to drive your code instead. [/edit]

Here is the whole code again. A little modified but still probably the same as before.

The sensors are mounted like this:

| |
|9cm |
| |

  • <

→ Sesnor

  • → IR LED

The left side is around 70cm from the right. When sensor is activated the analog value goes up to 450. But before they were mountet like they are now the value vent to about 130.

Here is the sensor schematic:

The diode here is an IR reciever.

Regarding the physical setup, have you tried increasing the distance (currently 9cm) between the 2 sensor assemblies? Ideally you'd want it such that it is physically impossible for a person (You are detecting people right) to trigger both sensors during normal operation, but I understand that you might not actually be able to do this. This might still be a good test though. Increase this distance and see if your system operates in a reliable fashion.

Regarding the code:

Long program, a lot of which I didn't read but I'm looking at this right here. Tell me if its the wrong part:

void vstop()

  if ((analogRead(SENSOR1) > 110) && (prej1 == 0)){
       sensor1Time = millis();                                       
       timeout = sensor1Time+ TIMEOUT;
       prej1 = 1;
       delay(3);                                                                  //why is this delay here?

   if  ((analogRead(SENSOR2) > 110 ) && (prej2 == 0)){
        sensor2Time = millis();
        timeout = sensor2Time + TIMEOUT;
        prej2 = 1;
      //  Serial.println("DRUGI");

After looking at your entire code the answer seems a little simple to me. Your program appears to be running slowly because you're simply doing too much. void loop() is very large of which only a small fraction of time is spent actually checking the sensors. Try this, take out all the non essential things. Right now we are just going to try to test the functionality of the sensors. So take out everything (comment out) that isn't related to sensing direction of movement, and just leave yourself with the core sensing code and a serial output to let you know what the sensors detected.

If your system appears to run faster and is more reliable then you need to revise your other code to make it more efficient.

If you still want to leave everything in place then you will have to seriously consider interrupts. Interrupts will allow the sensors to be monitored practically all the time, the only problem is, interrupts are associated with digital pins, hence you will need to figure out how to trigger a TTL high (+2 volts) when your sensors exceed a threshold value.

If the analog value is 410, then that corresponds to 2 volts which will allow you to hook the sensors upto the digital pins. If you can physically modify the system so as to get the sensors to detect an obstruction at a value of 410 then you can consider the possibility of interrupts without any extra circuitry.

Don't get side tracked right now though, go back and implement what I've requested regarding taking out the non essential code and let's see if we can figure out what the problem is.

If I remove everything else and leave only the sensor code the thing works normaly. After a few tryes I got 0 errors in detection.

But I already knew that the problem was here because of the other code. Or at least I suspected that.

I can’t increase the distance between the sensors (or maybe for a cm) but I don’t think that a cm would make a difference. That delay(3) is there because I find that it helps just a little. So a person can go faster and it till detects that correctly.

I realy need that other code so I can’t do without it. And there is little room for making it faster. And then the problem gets bigger - if I understand this correctly interrupts only work with pins 2 and 3 which are already taken by an LCD screen. And I can’t plug the LCD into the analog inputs. I think that everything in the digital pin section is output. Only pin that I can disconnect is pin 3 (the backlight for LCD). And pins 0 and 1 that are not connected at all.

interrupts only work with pins 2 and 3 which are already taken by an LCD screen. And I can't plug the LCD into the analog inputs.

The analog pins also function as digital pins. Reading off the chart, if I wanted to use analog pin 3 (A3) as a digital output, I can simply use its corresponding pin number (17)

digitalWrite(17, HIGH)

More info here:


Look here under External Interrupts to find the 2 basic functions you will need. These will only work for the conventional interrupt pins 2 and 3:

This page contains some code which allows you to enable any pin to be used for interrupts:

I tryed to use that function and enable pins 17 and 18 for interrupts. What I get is a little strange. Sometimes nothing happens (the sensor gets activated - I checked with digitalread) or the whole arduino just freezes. No serial output, no change if I turn the potenciometer… Nothing.

The new codE:

If you use arduino != mega you could (soon) find yourself in a RAM shortage.

You use about 580 bytes on constant char* alone. About 230 of them are padding whitespace.

Hook up a voltmeter in parallel to the sensor assembly and read off that. Now when you obstruct the sensors under normal operating conditions, does the voltmeter read higher than 2 volts. If it does then we can continue down this path of using interrupts as this is TTL high.

Regarding PCInt. Some oversight on my part. PCInt only supports the CHANGE mode, which is not what you want. You want the RISING mode.

Forget about using PCInt for now. Hook up your sensors into pins 2 and 3 and use the conventional interrupt. This means you will have to free up the LCD pins being used, but just connect those to one of the analog pins.

In addition: When using interrupts. Any variables being modified within your interrupt service routines (in this case sens1 and sens2) should be declared as volatile.

I will try it! The voltmeter show a value from 1.9V to 3.3V (Depends on the distance from the sensor).

EDIT: This seems to work but now I get multiple activations for one pass so I can’t be 100%. I would use delay but that reference says that delay wont work in interrupt functions. I actualy tryed running and it detected me :o. So probaly only problem here are multiple activations.

EDIT2 : while this does work the other parts of code don’t. The function that needs to write to some pins for very little time and then write to naother pin again for a few miliseconds doesn’t work too good. Or it doesn’t work at all. Depends on the moment when you run it.

Ok I am realy realy annoyed. My arduino is crazy! When I want to run a piece of code that writes to some pins in sequence it gets extremly strange. These pins control the motors but every time I run that piece of code the motor starts and never stops (until I press reset) I tryed NoInterrupts() but it doesn't help. So it does write high to the pin but then doesn't turn it off after 400ms delay. And when interrupt happens the arduino freezes again - sometimes. Without the interrupts the motors work better (so they start and stop) but the timing is strange. And it gets worse - When I put some pressure on the LCD this thing starts to work. So problem is with LCD screen and interrupts. I had to modify the LCD4Bit library to use analog pins as output but I just changed the pin where the comment indicated that I can change it.