timer problem arduino one

Hello everybody,
i’m new in this forum and i’m not a good programmer, but i i’ve to write just a little program really easy.
I can’t do it but i am sure that for you will ridiculously easy. Also i can’t find in the forum my exact problem.

So here the situation: on an Arduino one, i have to read the status on a PIN and, if the status is high, start a timer of 5 seconds; in this time, if the status become high again, set an output to high for 15 seconds.
Here my code:

#define FIRST 13                // pin di input collegato al segnale  
#define EXIT 7              // pin di output dove è collegata l'uscita
int  val = 0;                 // si userà val per conservare lo stato del pin di input  
int uscita = 0; 
 
void setup() {  
  pinMode(FIRST,INPUT);       // imposta il pin 13 come input  
  pinMode(EXIT, OUTPUT);     // imposta il pin 7 come output  
}  
  
void loop() {  
  val = DigitalRead(FIRST);
  
  if (val == HIGH){
  time1 = millis();
	}
	if (time1<5000 && val==(HIGH)){
	time2 = millis();
		while (time2<=15000){
		DigitalWrite(EXIT,HIGH);
		}
	time2=0;
	}
	time1=0;
  }

The code looks pretty incomplete to me, but i don’t know why.
Can you give it a check and give me some hints?
Any help is appreciated.
Thanks and sorry for my bad english!

FIrst let me know. What you want to do if your val is low. your entry condition fail because inner timer is high, When time1 complete 5000ms it will not go inside loop itself. SO cross check it once.

http://playground.arduino.cc/Code/StopWatchClass I think tou should use MStimer ibrary instead of millis(); We can't reset counter of millis()

Hi,

I spotted some errors and have some questions and suggestions for you.

  1. The indentation in your sketch is all wrong. This does not affect how the sketch runs, but it is confusing for you and anyone else who tries to help you. Correct it by selecting "Auto Format" from the tools menu.

  2. The digitalRead() and digitalWrite() functions do not begin with a capital letter D. The C language is case-sensitive. I would be surprised if your sketch compiles like that.

  3. You don't need a library for this. You do not need to reset the millis() function to zero. If you had a wall clock with a second hand, you would not need a stopwatch to time 5 or 15 seconds, you would just watch the second hand. Your sketch can use millis() in the same way.

  4. It will help eveybody to help you if we understand what the porpose of the circuit is, and what components are being used. What is connected to the input and the output.

  5. What should happen while the output is high for 15 seconds. Is the input ignored for this time?

  6. In your sketch, there is no function to set the output back to LOW.

  7. I think you need to be more clever than simply detecting when the input is HIGH. You need to detect when it changes from LOW to HIGH.

  8. Is the input connected to a switch if some kind? Switches "bounce" when pressed. This will cause your sketch to detect multiple changes that you did not intend.

Hope this helps a little.

Paul

Ok i'll try to reply as i can. For AMPS-N If status of val is low i haven't to do anything. Can't understand why time1 won't loop in the next cycle... It should, no?

For Paul

  1. Ok i'll post soon another version with some changes including this.

  2. Ok thanks, i'll change it.

3.I don't get it... So you are saying i can use just one timer millis()? This could have sense but i have to reset the timer if i want to get some normal value.

4.I'll try to explain: the only input in the circuit (13), works like a button. In the output there's a small motor.

5.You said right. In the time of 15 seconds of high output, the input must be ignored.

6.The while cycle. While the timer is less than 15000 the output is high. I think that if the condition is no more satisfied (timer2>15000), the output will back to low. Is that right?

7.The input is normally low, so what do you mean?

  1. This would be a big problem. It shouldn't happen but i'm not sure. Like i said it's like a button, so when i press it's high(12V), when i release it's low (0 V).

Hope i clear the question. Sorry again for my bad english and thanks for the tips.

You need explain exact situation, What ur trying to do

list your Entry condition for loop. Just altered your code. try this which make u clear. Use serial monitor to see output

#define FIRST 13                // pin di input collegato al segnale  
#define EXIT 7              // pin di output dove è collegata l'uscita
int  val = 0;                 // si userà val per conservare lo stato del pin di input  
int uscita = 0; 
long int time1=0;
long int time2=0;

void setup() { 
 Serial.begin(9600); 
  pinMode(FIRST,INPUT);       // imposta il pin 13 come input  
  pinMode(EXIT, OUTPUT);     // imposta il pin 7 come output  
}  

void loop()
{  
  val = digitalRead(FIRST);
Serial.print("val:");
Serial.println(val);
  if (val == HIGH){
      time1 = millis();
      if(time1<=5000)
      {
          Serial.println("time1 executing");
       }else
       {
         
        time2 = millis();
                       Serial.print("TIME2:");
           Serial.println(time2);
          if(time2<=15000)
            {
             digitalWrite(EXIT,HIGH);
             Serial.print("TIME2:");
           Serial.println(time2);
             
            }else
            {
              time2==0;
              time1=0;
              Serial.print("TIME2:");
             Serial.println(time2);
            } 
       }
            
      }
           
     
delay(1000);
}

You can use millis() for any number of timings. This is what your sketch is currently doing. You take a "snapshot" of the value of millis() at the time an event/change of input happens, then later you can check if a certain time has elapsed by comparing the snapshot value to the current value of millis().

Your output will not go low unless your sketch sets it back to low with digitalWrite(x, LOW). This is not happening in your sketch right now. The while() loop does not do this, it only controls when the loop ends. Once an output has been set high, it will stay that way. There is no point setting it to high over and over in your loop.

Your input being high is not a single event in time. It will be high for many thousands of times that loop() runs. When does your 5 second timout begin? When the input goes from low to high, or from high back to low? For either answer, your sketch needs to detect the input changing, not just that it is high.

All mechanical buttons bounce. It may appear to we humans that the value goes from 0V to 12V and back again, but a microcontroller runs thousands of times faster than our senses and it will see the contacts in the switch bouncing several times before they settle to the new value. Your sketch may need to be programmed to ignore these bounces, otherwise it will appear that the switch always goes to 12V many times within your 5 second timeout. Once the change in the input has been detected, you need to ignore any further changes in the next 50ms for example.

Paul

Sorry for the late of my answer.

Here my entry condition for loop:

if the pin 13, which is the input, change its status from LOW to HIGH the loop should start; since it’s a button, the value will back to LOW, and in that case, the change from HIGH to LOW must be simply ignored.
should start a timer;
If the value of millis() is under 5000 AND the status of pin 13 change again from LOW to HIGH, the output of the pin 7 must change from LOW to HIGH and stay high for 15 seconds.
Thats all.
I checked the code you altered. I’m not sure but it seems ok… The serial monitor say that the timer works fine, as it should, but in the reality, it doesn’t do what i want: the status of pin 7 it’s always high but i don’t know why.

Use MStimer library & interrupt.

Check pin got high or not .if high start timer else stop/RESET timer.