Close
I dont know if i understand you correctly, but I guess you will have to save the status of your programm in a variable and then just check it, if programm is running.. if so.. make what you want while waiting 5sec
int programmStatus = 0;
void setup () {
Serial.begin (9600);
pinMode (A0 , INPUT);
pinMode (13 , OUTPUT);
}
void loop () {
int odczyt = analogRead(A0);
if(programmStatus == 0 && odczyt > 200) {
programmStatus = 1;
digitalWrite (13,HIGH);
delay(5000);
programmStatus = 0;
} else {
digitalWrite(13, LOW);
}
}
Do you want the loop() function to only ever run once 5 seconds after the button press or can the button be pressed again ? It looks like the latter.
Save the value of millis() when the button becomes pressed, then every time through loop() check if the required period has elapsed by reading the current value of millis(). If not then keep going round loop(), otherwise execute the required code.
What does the second program do when you run it ?
Formatted properly you may see a problem with the if/else blocks of code
if (odczyt > 200)
digitalWrite (13, HIGH);
else
digitalWrite(13, LOW) ;
Serial.println (odczyt) ; //code from here on will always be executed whatever the value of odczyt
delay (10);
delay (3000);
digitalWrite(6, LOW); //power supply off
Much of the code above will always be executed whatever the value of odczyt. Is that what you intended ?
See if this does what you want:
int odczyt;
void setup() {
Serial.begin (9600) ;
while(!Serial) ;
pinMode (A0 , INPUT) ;
pinMode (13 , OUTPUT) ;
pinMode(8, OUTPUT); //first buzzer
pinMode(7, INPUT_PULLUP); //button as input
pinMode (6, OUTPUT); // POWER SUPPLY
digitalWrite(8, LOW); //first buzzer off
digitalWrite(6, LOW); //power supply off
}
void loop()
{
while(digitalRead(7) == HIGH); //if button pushed
delay (3000); //
digitalWrite(8, HIGH); //first buzzer on
delay(1000); //Czekamy 1 sekund
digitalWrite(8, LOW); //first buzzer off
digitalWrite(6, HIGH); //power supply on
delay(50);
odczyt = analogRead(A0);
if(odczyt > 200)
digitalWrite (13,HIGH);
else
digitalWrite(13, LOW) ;
Serial.println (odczyt) ;
delay (10);
delay (3000);
digitalWrite(6, LOW); //power supply off
while(digitalRead(7) == LOW);
}
Try again with this edit, what do you see in the serial monitor?
int odczyt;
void setup() {
Serial.begin (9600) ;
while(!Serial) ;
pinMode (A0 , INPUT) ;
pinMode (13 , OUTPUT) ;
pinMode(8, OUTPUT); //first buzzer
pinMode(7, INPUT_PULLUP); //button as input
pinMode (6, OUTPUT); // POWER SUPPLY
digitalWrite(8, LOW); //first buzzer off
digitalWrite(6, LOW); //power supply off
}
void loop()
{
while(digitalRead(7) == HIGH); //if button pushed
delay (3000); //
digitalWrite(8, HIGH); //first buzzer on
delay(1000); //Czekamy 1 sekund
digitalWrite(8, LOW); //first buzzer off
digitalWrite(6, HIGH); //power supply on
delay(50);
odczyt = analogRead(A0);
if(odczyt > 200)
digitalWrite (13,HIGH);
else
digitalWrite(13, LOW) ;
Serial.println (odczyt) ;
delay (10);
delay (3000);
digitalWrite(6, LOW); //power supply off
}
Try this, also what are you calling "laser".
int odczyt;
void setup() {
Serial.begin (9600) ;
while(!Serial) ;
pinMode (A0 , INPUT) ;
pinMode (13 , OUTPUT) ;
pinMode(8, OUTPUT); //first buzzer
pinMode(7, INPUT_PULLUP); //button as input
pinMode (6, OUTPUT); // POWER SUPPLY
digitalWrite(8, LOW); //first buzzer off
digitalWrite(6, LOW); //power supply off
}
void loop()
{
while(digitalRead(7) == HIGH)
digitalWrite(13,LOW); //if button pushed
delay (3000); //
digitalWrite(8, HIGH); //first buzzer on
delay(1000); //Czekamy 1 sekund
digitalWrite(8, LOW); //first buzzer off
digitalWrite(6, HIGH); //power supply on
odczyt = analogRead(A0);
if(odczyt > 200)
digitalWrite (13,HIGH);
else
digitalWrite(13, LOW) ;
Serial.println (odczyt) ;
delay (10);
delay (3000);
digitalWrite(6, LOW); //power supply off
}
I need to make that function is working only for 3 seconds after button was pushed.
I am still not clear what you want to do.
Should some code (what code would that be ?) run for 3 seconds after the button becomes pressed or should there be a delay of 3 seconds after the button becomes depressed before the code runs ? If the latter, then how long should the code run ?
Something to get you started
const byte ledPin = 13;
const byte buttonPin = A1;
byte currentButtonState;
byte previousButtonState;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long buttonWaitInterval = 3000;
unsigned long blinkInterval = 500;
boolean blinking = false;
boolean timing = false;
unsigned long startTimingMillis;
unsigned long startBlinkMillis;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.println("Waiting for button press");
}
void loop()
{
currentMillis = millis();
previousButtonState = currentButtonState;
currentButtonState = digitalRead(buttonPin);
if (!blinking && currentButtonState != previousButtonState && currentButtonState == LOW) //LED not already blinking and button has become pressed
{
if (!timing) //not waiting for a button press
{
Serial.println("Started waiting after button press");
timing = true;
startTimingMillis = currentMillis; //save current time
}
}
if (timing && currentMillis - startTimingMillis >= buttonWaitInterval) //interval after button press has passed
{
Serial.println("Started blinking LED");
timing = false;
blinking = true;
startBlinkMillis = currentMillis; //save time we started blinking LED
}
if (blinking && currentMillis - startBlinkMillis >= blinkInterval) //time to change stet of LED ?
{
Serial.println("Blink");
digitalWrite(ledPin, !digitalRead(ledPin)); //change LED state
startBlinkMillis = currentMillis; //save the time of change
}
}
It does not do exactly what you want but it will show you how to use millis() for timing.