Go Down

Topic: Monitor two sensors to do different tasks simultaneously ? (Read 1 time) previous topic - next topic

dtokez

Hi all. I want to use two PIR sensors, basically if sensor #1 is tripped, I want to turn on light #1, delay for a while then fade off. if sensor #2 is tripped I want to turn on light #2, delay for a while then fade off.

Thinking about how I would program it, if sensor #1 has tripped and the arduino is in the delay stage of the code with the light on, it wont be able to run through the rest of the code and poll the other sensor pin to see if that has tripped also?

is there a simple way around this?

thanks  XD

Nick Gammon

http://www.gammon.com.au/electronics

pluggy

IMO delay() should never have been included in the processing language.   Its a travesty and noobs can't see past it.  I have one Arduino (Duemilanove) monitoring 3 electric meters, 1 gas meter, 2 light sensors, 5 digital thermometers, blinking an led that varies by electric use, updates an LCD display every few seconds and outputs everything it knows by serial every 2 seconds, besides running the central heating in its spare time. Not a solitary delay in the whole sketch, because basically it wouldn't work if there was.
http://pluggy.is-a-geek.com/index.html

bubulindo

Delay comes with AVR-GCC and it is useful in interfaces where a few miliseconds are needed before the next operation and millis would end up making the code more complex and not better.

Delay was never meant to be used for full seconds.


Not a solitary delay in the whole sketch, because basically it wouldn't work if there was.


my LCD library has this on it:

Code: [Select]
if (! (_displayfunction & LCD_8BITMODE)) {
    // this is according to the hitachi HD44780 datasheet
    // figure 24, pg 46

    // we start in 8bit mode, try to set 4 bit mode
    write4bits(0x03);
    delayMicroseconds(4500); // wait min 4.1ms

    // second try
    write4bits(0x03);
    delayMicroseconds(4500); // wait min 4.1ms
   
    // third go!
    write4bits(0x03);
    delayMicroseconds(150);

    // finally, set to 4-bit interface
    write4bits(0x02);
  } else {


Not a single delay?
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

dtokez

Thanks for the responses. I see what you mean about noobs getting stuck and set in the 'delay' way, this is the first out of a few simple projects that I won't be able to use it.

Thanks Nick for the link, looks good I will check it out

dtokez

Hi again guys. I'm struggling here. I have cobbled together some code mainly from Nick's tutorial.

I'm trying to get the basics going for now, as I said I want to monitor two sensors but I'm just looking at one for now.

The aim of the code was to read a digital pin, and if that pin is high to set another digital pin high, and also store the time the event happened.
The i added another if statement, which would look at the millis since the event, and if they are equal or above 5000 to turn off the led.

It doesn't seem to work, the led just goes on and off when I change the pin state, but does not stay on for 5000 ms. I want it to work from the pulse output of a PIR so if the pin is low I don't want to turn off the led until the time has elapsed.

What am i doing wrong?

Code: [Select]
// Which pins are connected to which LED
const byte greenLED = 9;
const byte redLED = 10;

int const onTime = 5000;    //constant on time)

int val = 0;     // variable for reading the pin status


// Variable holding the timer value so far. One for each "Timer"
unsigned long left;
unsigned long right;


void setup ()
{
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  pinMode(2, INPUT);
  left = millis ();
  right = millis ();

}  // end of setup




void loop()
{


  val = digitalRead(2);  // read input value



  if (val == HIGH)
  {
    digitalWrite(9, HIGH);
    left = millis ();

  }


  if (left >= 5000);
  {
    digitalWrite(9, LOW);
  }


}

wildbill

You have several problems; this line:
Code: [Select]
  if (left >= 5000);
has a semi colon that should not be there. The code after it gets executed regardless, so the led will be turned off every cycle round loop.

left is set to millis earlier, that means that once the semicolon issue is fixed, once the sketch has been running for 5 seconds the condition will always be true. Compare millis()-left to 5000 instead.

Lastly, while the input remains high, left gets reset on every trip around loop, so the 5s count only starts when the input goes low. Of course, this may be what you want.

Go Up