help with variable?

right, so i wanna create a variable that i can print to the serial monitor,
i want it to increase by 20 each time the loop has finished,

e.g.
prints 20,
loop runs,
prints 40,
loop runs,
prints 60,

and so on
to tell me how many seconds the program has ran for

any more info needed,
feel free to ask.

thanks in advance,
Jason.

Hi there

An answer to the specific question you have asked is ...

Declare a variable and initialise it to 0:

unsigned long secondsCounter = 0;

At the end of your loop, add 20 and print it:

secondsCounter += 20;
Serial.println(secondsCounter);

BUT ...

How do you know your loop will take exactly 20 seconds to run? It would be better to use the millis() function to check how much time has passed since the last time round the loop and print that.

For example:

unsigned long lastMillis = 0;
unsigned long nowMillis = 0;
nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;

Regards

Ray

hi ray thanks for the help

it didnt take 20 secs in the end

will this work instead?

int led1 = 4;
int led2= 5;
int led3 = 6;
int led4 = 7;
int led5 = 8;
int led6 = 9;
int led7 = 10;
int led8 = 11;
int led9 = 12;
int led10 = 13;
void setup() { 
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);
  unsigned long secondsCounter = 0;}
void loop() {
  digitalWrite(led1, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led2, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led3, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led4, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led5, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led6, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led7, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led8, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led9, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led10, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led9, LOW);
  digitalWrite(led10, LOW);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led1, HIGH;
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led9, HIGH);
  digitalWrite(led10, HIGH);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter); 
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led9, LOW);
  digitalWrite(led10, LOW);
  delay(1000);
  secondsCounter += 1;
Serial.println(secondsCounter);
}

Hackscribble:
Hi there

An answer to the specific question you have asked is ...

Declare a variable and initialise it to 0:

unsigned long secondsCounter = 0;

At the end of your loop, add 20 and print it:

secondsCounter += 20;

Serial.println(secondsCounter);




BUT ...

How do you know your loop will take **exactly** 20 seconds to run? It would be better to use the millis() function to check how much time has passed since the last time round the loop and print that.

For example:



unsigned long lastMillis = 0;
unsigned long nowMillis = 0;






nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);  // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;




Regards

Ray

thanks,
Jason

Move the "unsigned long secondsCounter = 0;" to before the void setup().

Also I think you have a missing ")" in one of your statements further down.

Move the "unsigned long secondsCounter = 0;" to before the void setup().

Also I think you have a missing ")" in one of your statements further down.

yeah, ray,
ignore my last reply,

i am using this code,

int led1 = 4;
int led2= 5;
int led3 = 6;
int led4 = 7;
int led5 = 8;
int led6 = 9;
int led7 = 10;
int led8 = 11;
int led9 = 12;
int led10 = 13;
unsigned long lastMillis = 0;
unsigned long nowMillis = 0;
unsigned long Millis = 0;
void setup() { 
  Serial.begin(9600);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);
}
void loop() {
  digitalWrite(led1, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led2, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led3, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led4, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led5, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led6, HIGH);
  delay(1000);
 nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led7, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led8, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led9, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led10, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led9, LOW);
  digitalWrite(led10, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led9, HIGH);
  digitalWrite(led10, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led9, LOW);
  digitalWrite(led10, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
lastMillis = nowMillis;
}

but in the serial moitor it keeps repeating
1.0
1.0
1.0
1.0
1.0
1.0

over and over,
do you know how i can get it to go

1.0
2.0
3.0
4.0 and so on?

Sorry, my mistake. I gave you code for how long each loop took, not cumulative time since start.

In setup(), do this:

lastMillis=millis();

This will be done just once.

Then in loop(), remove all of these:

lastMillis = nowMillis;

Ray

It would be good also to tidy up the variable name.

"lastMillis" would be better called "startMillis".

Hackscribble:
It would be good also to tidy up the variable name.

"lastMillis" would be better called "startMillis".

problem sorted,......finally,
thank you so much!

int led1 = 4;
int led2= 5;
int led3 = 6;
int led4 = 7;
int led5 = 8;
int led6 = 9;
int led7 = 10;
int led8 = 11;
int led9 = 12;
int led10 = 13;
unsigned long lastMillis = 0;
unsigned long nowMillis = 1;
unsigned long Millis = 1;
void setup() { 
  Serial.begin(9600);
  lastMillis=millis();
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);
}
void loop() {
  delay (1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led1, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led2, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led3, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led4, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led5, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led6, HIGH);
  delay(1000);
 nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led7, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led8, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led9, HIGH);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led10, HIGH);
  delay(1000);
  nowMillis = millis();
  Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led1, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led2, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led3, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led4, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led5, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led6, LOW);
  delay(1000);
 nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led7, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led8, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led9, LOW);
  delay(1000);
  nowMillis = millis();
Serial.println((nowMillis - lastMillis) / 1000.0, 1);   // Forced to float to display decimal part of time, and set to 1 decimal place
  digitalWrite(led10, LOW);
}