Hi! Im currently trying to complete a project which, when a button is pressed, runs a function for a given amount of time. The time should be constant, no matter when the button is pressed. I am seeking guidance on how to do this, since i have done a fair amout of research, yet i have come up fairly empty-handed.
Here is my code at the time of writing this:
const int relayPin = 9;
const int relayPin2 = 10;
const int relayPin3 = 11;
const int inputPin = 3;
const int buttonPin = 2;
int time1 = 0;
int state = HIGH;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(buttonPin, OUTPUT);
digitalWrite(relayPin, HIGH);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
Serial.begin(9600);
digitalWrite(buttonPin, HIGH);
}
void loop() {
reading = digitalRead(inputPin);
if(reading == HIGH && previous == LOW && millis() - time > debounce){
if(state == HIGH){
state = LOW;
}
else {
state = HIGH;
}
time = millis();
digitalWrite(relayPin, state);
}
previous = reading;
}
void kartoffel(){
Serial.println(time);
}
I haven't written the function(void kartoffel()) which i wish to run for the given amount of time, since i'd like to know if and how, this is possible. Thanks for your help!