I need a code

Hi,
For project Im doing I need a code which says: if (for example fotoresistor) is high in one moment (so not all the time), then do something, please help.
Thank you!

How long is "one moment"?

One second or less

Decide what criterion defines your start event. Decide what criterion defines your end event.

If the first criterion is met, record the time from millis(). When the second criterion is met, record the time from millis()

Subtract the first time from the second, and if the difference is 1000 (or less, whatever) do what it is you have to do.

AWOL: Decide what criterion defines your start event. Decide what criterion defines your end event.

Im not sure how to do it, which command i should use for define that?

digitalRead, or analogRead?

How do you mean start and end event? Sorry im beginner

@luco19, what you want is relatively simple. I suggest you take an hour or two or three and study several of the example programs that come with the Arduino IDE. I suspect you will find one or more that is very close to your requirement.

...R

Just the conditions that decide when whatever it is you want to time, begin and end

AWOL:
Decide what criterion defines your start event.
Decide what criterion defines your end event.

If the first criterion is met, record the time from millis().
When the second criterion is met, record the time from millis()

Subtract the first time from the second, and if the difference is 1000 (or less, whatever) do what it is you have to do.

I think i understand, please correct me if im wriong:
-first i define that fotoresistor on start is high
-then i measure time he is high in millis (mil1)
-then i cover fotoresistor,so he become low
-i measure how long is in millis(mil2) and paralel with that calculate mil1-mil2
-so if mil1-ml2 in that moment is for example 6000(FR was high 6sec)-1000(FR was low 1sec)
-so i put condition fi mil2-mil1 <= 5000 ,then my program do what he needs to do if that condition is true
Am i right or not ?

That sounds about right. Your sketch will be a little shorter than the explanation.

AWOL: That sounds about right. Your sketch will be a little shorter than the explanation. ;) ;)

I wrote the code, but its not working, how terrible is my code? :stuck_out_tongue:

int led = 8;
int millis1 = millis();
int millis2 = millis(); 

void setup() {
  pinMode(3, INPUT);
  pinMode(led, OUTPUT);
}

void loop() 
{
int rf = digitalRead(3);

while(rf == HIGH){
 millis1; 
}

while(rf == LOW){
 millis2; 
}
  

 if (millis1 - millis2 > 0)
  { 
    digitalWrite (led, HIGH);
  } 
  else
  {
    digitalWrite (led, LOW);
  }
    
}
void loop()
{
int rf = digitalRead(3);

while(rf == HIGH){
 millis1;
}

It's sort-of close, but if after your digitalRead, "rf" happens to have the value HIGH, the following while loop will never exit.

"millis1" is a variable (of the wrong type) that is never updated, so I don't know what that's meant to achieve.

Maybe you could have a look at some of the examples provided in the IDE.

Let's imagine you're waiting for the signal on pin 8 to go HIGH;while (digitalRead(8) == LOW) ; Now, when that loop exits, we know that the signal on pin 8 was/is HIGH. So, record the time.

unsigned long startTime = millis();

Now wait for the signal on pin 8 to go LOW

while (digitalRead(8) == HIGH) ;

When that loop exits, we know our HIGH pulse has ended, so take the time againunsigned long endTime = millis ();

Still dont works, led must blink twice when i cover FR for short moment, but nothing happens, i know im boring but i need more assistance :)

int led = 8;
long startTime;
long endTime;

void setup() {
  pinMode(3, INPUT);
  pinMode(led, OUTPUT);
}

void loop() 
{
int rf = digitalRead(3);

while(rf == HIGH){
 unsigned long startTime = millis();
}

while(rf == LOW){
 unsigned long endTime = millis(); 
}
  

 if (startTime-endTime > 0)
  { 
    digitalWrite (led, HIGH);
    delay (5);
    digitalWrite (led, LOW);
    delay (5);
    digitalWrite (led, HIGH);
    
  } 
  else
  {
    
  }
    
}
while(rf == HIGH){
 unsigned long startTime = millis();
}

while(rf == LOW){
 unsigned long endTime = millis();
}


 if (startTime-endTime > 0)

I'm amazed that even compiles. Edit: You have multiple variables with the same name - don't do that.

long startTime;
long endTime;

while(rf == HIGH){
 unsigned long startTime = millis();
}

while(rf == LOW){
 unsigned long endTime = millis();
}
while(rf == HIGH){
 unsigned long startTime = millis();
}

I didn't write my code like that - you weren't paying attention

It worked :) :) :) Thank you for your asisstance!