3

Problem: Have an LED strip do some animation (say it takes 15 seconds for that line of code to run) meanwhile, have a sensor (tried methods: PIR sensor and ultrasonic sensor) watching to see if an object passes by. If it senses an object, stop the light animation and make the lights do something else.

Issues: The PIR sensor takes too long to reset... I have the timing turned all the way to minimum and I am still able to count 12 seconds before it starts sensing motion again. (Is there anyway to override this in the code?) The other issue is the ultrasonic sensor, as far as I know, can't reliably run the first animation and watch for a change in distance then call an interrupt since an interrupt can only happen on FALLING, RISING, LOW, or CHANGE. This is an issue because the ultrasonic sensor is continuously sending LOW to HIGH then back to LOW for its pulses.

OVERALL QUESTION: How can I effectively sense if an object passes by a sensor, run new LED animation then back to where it left off without having to wait 12 seconds every time. Is there some other hardware I could try? Please help!?

2
  • PIRs are controlled by their hardware. May 17, 2014 at 1:58
  • 2
    Can you post your code please.
    – sachleen
    May 17, 2014 at 5:24

1 Answer 1

1

It seems as you are using the delay(); function for your code. The reason that it doesn't run anything is your Arduino does nothing during the function. The easiest option IMHO is using millis();. This will "return" a long datatype. Some example code (modified from example sketches in Arduino IDE):

/*
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 5/16/14 by Anno. Penguin
 This example code is in the public domain.
*/

const int ledPin =  13;      // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  //Sensor code
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Obviously, your code will be more complex than that. This one only blinks it, but you'll have to do some creative logic here. I don't know what the animation is, but most likely you'll need to add a few more variables (make sure to declare them outside loop() so they aren't disposed of each iteration!).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.