PLEASE HELP Using '' Micros'' for timing

Hi Guys!!

I am trying to code using ''micros'' instead of ''delay''. This is what the code is suppose to do: I have to read a signal from a sensor(3.3V). when this sensor is deactivated(0V), I want a delay in micro seconds. The sensor must stay deactivated(0V) until the limit switch(NC) is pressed.

So please, can someone help me??

This is the code:

const int sensor = 7;
const int testOutput = 8;
const int LimitSwitch = 9;

void setup(){
pinMode(sensor, INPUT);
pinMode(testOutput, OUTPUT);
pinMode(LimitSwitch, INPUT);
digitalWrite(testOutput, HIGH);

}
void loop(){
int homeSw = digitalRead(LimitSwitch);
if(digitalRead(sensor) == LOW && digitalRead(LimitSwitch) == HIGH)
{
delay(820000);
digitalWrite(testOutput, LOW);

}
else if(digitalRead(LimitSwitch) == HIGH)
digitalWrite(testOutput, HIGH);

}

test.ino (535 Bytes)

The sensor must stay deactivated(0V) until the limit switch(NC) is pressed.

I don't understand that. A sensor is an input, and is activated or deactivated by some external stimulus, maybe light vs dark or whatever. It will remain in that state according to that stimulus.

Please post what you tried.

Have you read Using millis() for timing. A beginners guide ? Although it describes the use of millis() for timing the principles are the same when using micros()

neiklot:
I don't understand that. A sensor is an input, and is activated or deactivated by some external stimulus, maybe light vs dark or whatever. It will remain in that state according to that stimulus.

Yes, the sensor is activated by mechanism. On goes activated and deactivated.

UKHeliBob:
Please post what you tried.

Have you read Using millis() for timing. A beginners guide ? Although it describes the use of millis() for timing the principles are the same when using micros()

I've tried, but i got confused more

neiklot:
I don't understand that. A sensor is an input, and is activated or deactivated by some external stimulus, maybe light vs dark or whatever. It will remain in that state according to that stimulus.

The sensor is a photo interrupter sensor

I've tried, but i got confused more

Post what you tried.

The principle is simple.

Save the millis() value at the time that the start action happens. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().

pdgceo:
Yes, the sensor is activated by mechanism. On goes activated and deactivated.

So where does the limit switch fit in to the story?

I think this is one of those cases where you need to explain what you're actually trying to do, not how you're trying to code it.