How to set time

hi

I have an infrared sensor, once blocked, will lit up a LED.

However, now i would like to programme it to light up after 10 seconds. what do i need to include?

thanks!

Have a look at millis() might be enough for what you want. ;)

ok thanks! :D

but what i meant was that if the sensor was blocked for more than 10 seconds, the led will then light up.

millis() is still applicable?

Yes... millis will give you a time stamp.

You can use that to measure the time that the button was pushed, the time the light was on or the time it took to calculate something. The principle is the same.

okay thanks mate.

will try it out. :D

this is what i wrote, but it doesnt seems to be working.

once i block the sensor, the LED lights up instead of after 10 seconds.

#define THRESHOLD 100
#define SENSOR_TIME 10000


int ledPin = 10; // Setup LedPin on Digital Pin 10

unsigned long sensor_time;




void setup() 
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
}

void loop() 
{ 
  int sensorValue = analogRead(A0);
 

  
  Serial.println(sensorValue);

  if (sensorValue < THRESHOLD)
  
  {

    if (sensor_time == 0) 
    {
      sensor_time = millis(); 
    }

    else if (millis() - sensor_time > SENSOR_TIME)
    {
    digitalWrite(ledPin, LOW); // LOW Reflectance.    
    }
  }
  

  
  else 
  {
    digitalWrite(ledPin, HIGH); // HIGH Reflectance.
  }
  
}

The problem is that you aren’t reseting sensor_time back to 0 when sensor_value is above the threshold.

Here is the modified code, there is a boolean recording if the led has been triggered.

#define THRESHOLD 100 // the threshold value
#define SENSOR_TIME 10000 // the amount of time (in milliseconds) that the sensor value has to stay below the threshold (or hold value)
int ledPin = 10; // The digital pin the LED is on
boolean triggered = false; // The “state” of the LED
unsigned long sensor_time; // The last millisecond that the sensor transitioned from being above the threshold and being below it (or the sensor’s time stamp)
int sensorPin = 0; // The analog pin the sensor is on
void setup() { // Setup function
pinMode(ledPin, OUTPUT); // Set the pin mode of the LED pin to output
Serial.begin(9600); // Initialize the Serial port to a baud rate of 9600bps
}
void loop() { // Loop function
int sensorValue = analogRead(sensorPin); // Read the value of the sensor
Serial.println(sensorValue); // Print to the Serial port the value of the sensor
if (sensorValue < THRESHOLD){ // If the value of the sensor is above the threshold value
if (sensor_time == 0) { // If the sensor’s time stamp hasn’t been updated already
sensor_time = millis(); // store the current millisecond into memory
}
else if (millis() - sensor_time > SENSOR_TIME){ // If the sensor’s time stamp subtracted from the current millisecond is greater than the hold value
digitalWrite(ledPin, HIGH); // Set the LED pin high (change to LOW for active low LEDs)
triggered = true; // Record that the LED has been triggered
}
}
else {
digitalWrite(ledPin, LOW); // Set the LED pin low (change to HIGH for active low LEDs)
if(triggered) { // If the LED has already been triggered
sensor_time = 0; // Reset the sensor’s time stamp to 0
triggered = false; // Reset the state of the LEDs triggered value
}
else {
// Do nothing
}
}
}

I can make a small library if you want to make this process easier to do.

You have a Serial.begin() statement in setup(). That’s good. Now, add some Serial.print() and Serial.println() statements in loop(). Open the Serial Monitor (last icon on the right on the top row). Set the speed to the same as in setup, and see what you can learn.

After the first time, sensor_time will mo longer be 0, so you might need to rethink that bit.

The names sensor_time and SENSOR_TIME don’t mean much. LED_DELAY would be better, since that is what your are using it for. The sensor time - is that when the sensor reading dropped below the threshold, or when the light is turned on? Use a name that matches the value stored in the variable.

thanks all for the help :D it worked out perfectly fine.

now i have an arduino board with 2 xbees.

the sensor + xbee shield + xbee module are connected to one of the arduino and when the sensor is blocked for 5 seconds, i want the led to light up and the xbee to send a message to another xbee saying it is full. what do i need to add in? or any good reference material?

:D

the sensor + xbee shield + xbee module are connected to one of the arduino

What is the other XBee attached to?

The XBees simply replace a wire. If you were using Serial or a NewSoftSerial instance to exchange data over a wire, you keed using the same process to exchange data over the air, instead.

Of course, you need to have the proper XBees, and have to have properly configured them.

the other xbee would be attached to the xbee adaptor.

both xbees have already been configured properly using xctu.

so now its the programming issues that i need to solve. haha

so now its the programming issues that i need to solve.

What programming issues? Unplug the USB cable. Connect the XBees and external power supply, and Serial will still continue to send data to the PC, using the XBees instead of a wire.

I am looking for the correct commands to add to my codes so that the arduino + xbee can communicate with the other xbee + xbee adaptor attached to the computer. Sorry for being so unclear!

help?

help?

Would you like me to come read reply #9 to you?

bwahahah :D okay i got it solve already. thanks :)