Temporarily store value constant from millis in variable

Hey I am sitting here on a code and dont know how to go on. I would like to save the value millis () is having when you start pushing the button in an variable as long as the butten is pushed without changing the value. Is there a trick or someting to do that? Or an other way to write the programm Iam programming? In the end my programm should light up an other LED every 10 seconds when the button is pushed without using an delay.

Thank You for you`re help.

const int intervall = 10000;
int schalter = 0;
unsigned long previousTime = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(2,OUTPUT); //1
pinMode(3,OUTPUT); //2
pinMode(4,OUTPUT); //3
pinMode(5,OUTPUT); //4
pinMode(6,OUTPUT); //5
pinMode(7,OUTPUT);//warn
pinMode(8,OUTPUT); //Warn 
pinMode(10,INPUT); //Schalter
pinMode(11,OUTPUT); //Grün
pinMode(12,OUTPUT);  //Blau
pinMode(13,OUTPUT); //Rot
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  schalter = digitalRead(10);
  unsigned long zeit = millis();
if (schalter == HIGH){

 unsigned long currentTime = zeit;
 Serial.println("Timer Ein");
Serial.print("Zeit Vergangen: ");
Serial.println(previousTime);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);

if (currentTime - previousTime == intervall){
  digitalWrite(2,HIGH);
}
if (currentTime - previousTime == 2 * intervall){
  digitalWrite(3,HIGH);
}
if (currentTime - previousTime == 3 * intervall){
  digitalWrite(4,HIGH);
}
if (currentTime - previousTime == 4 * intervall){
  digitalWrite(5,HIGH);
}
if (currentTime - previousTime == 5* intervall){
  digitalWrite(6,HIGH);
}
}
else if (schalter == LOW) {
 digitalWrite(11,LOW);
 digitalWrite(12,HIGH);
 Serial.println("Timer Aus");
 digitalWrite(2,LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
}
}

You want to detect when the button changes state - not just when it’s HIGH

See the state change example

I have solved the problemnom like this

const int intervall = 10000;  //Variablen für die ZeitIntervalle wenn immer eine LED leuchten soll
const int intervallb = 20000;
const int intervallc = 30000;
const long intervalld = 40000;
const long intervalle = 50000;
const long intervallf = 60000;  //Zeit bis zum Alarm also Timer Zeit

int schalter = 0;  // Variable für den Schalter

bool istGesetzt = false;  // Variablen dafür das miilis() richtig ausgelesen wird 
unsigned long previousMillis = 0;

void setup() {

pinMode(2,OUTPUT); //1
pinMode(3,OUTPUT); //2
pinMode(4,OUTPUT); //3
pinMode(5,OUTPUT); //4
pinMode(6,OUTPUT); //5
pinMode(7,OUTPUT);//warn
pinMode(8,OUTPUT); //Warn 
pinMode(10,INPUT); //Schalter
pinMode(11,OUTPUT); //Grün
pinMode(12,OUTPUT);  //Blau
pinMode(13,OUTPUT); //Rot
Serial.begin(9600);
}

void loop() {

  schalter = digitalRead(10);  // Das der Schalter ausgelesen wird

if (schalter == HIGH){  // Wenn eingeschaltet

unsigned long currentMillis = millis();  //Sagt das mit millis()die Zeit genommen werden soll

 Serial.println("Timer Ein");
Serial.print("Zeit Vergangen: ");
Serial.println(currentMillis - previousMillis);

digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,LOW);

if (currentMillis - previousMillis  >intervall){  // Sorgt dafür das jeweils nach jedem Intervall eine weitere LED angeht
  digitalWrite(2,HIGH);
}
if (currentMillis - previousMillis > intervallb){
  digitalWrite(3,HIGH);
}
if (currentMillis - previousMillis >  intervallc){
  digitalWrite(4,HIGH);
}
if (currentMillis - previousMillis > intervalld){
  digitalWrite(5,HIGH);
}
if (currentMillis - previousMillis > intervalle){
  digitalWrite(6,HIGH);
}

if (currentMillis - previousMillis > intervallf){ //Die Maximale Zeit ist abgelaufen es gibt Alarm
digitalWrite(11,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(13,HIGH);
tone(9,330);
delay(250);
digitalWrite(8,HIGH);
digitalWrite(7,LOW);
noTone(9);
delay(250);
}
 

} 
else if (schalter == LOW) {
  previousMillis = millis();  //Sorgt dafür das der millis()Wert in previousMillis auf Null ist wenn wieder eingeschaltet wird
      istGesetzt = true; 
 digitalWrite(11,LOW);
 digitalWrite(13,LOW);
 digitalWrite(12,HIGH);
 Serial.println("Timer Aus");
 digitalWrite(2,LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 noTone(9);
}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.