Help with millis() command

I've just started coding with arduino (never coded in any program before) this school year. I#m making a reaction test with an LCD display, a button and 3 LEDS. I#m stuck on the millis() command. I#ve tried to make a sort of "stopwatch" but my arduino book doesnt help.
this is my code:
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C Lcd(0x27,16,2);

int i = 8;
long start;
long stopp;
long diff;
void setup() {

pinMode(i,INPUT_PULLUP);
pinMode(13,OUTPUT);
Lcd.backlight();
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
Lcd.init();
Lcd.backlight();
}
void loop() {
delay(10000);
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
start=millis();
if(i==0){
stopp=millis();
diff=stopp-start;
Lcd.clear();
Lcd.setCursor(4,0);
Lcd.print(diff);
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
delay(100000);
}
delay(100000);
}

I'm also german so this is also more challenging for me in terms of going around the website.

Your code is incomplete.

Please remember to use code tags when posting code

Hello
Post your sketch well formated and in code tags <"/"> to see how we can help you to be started in this great hobby.

Which of your errors do you want to fix first?

1 Like

Post all your code in code tags and could you give a bit more details as to the issue? Also describe what you are trying to do.

Thanks for the early morning laugh! +1

Sure it was meant to sound funny. However:

  • There's a reason to ask for a complete (optimally minimal) sketch
    Do you have problems with 'i' was not declared in this scope ?
    Many issues come from variable definitions, which is an important part of the code.
  • Describing the observed problem gives you better answers. Please quote the compiler message, if that's your problem, @wouldlikesomehelpthanks.

Similar comments to the identical thread the OP started...

I've just started coding with arduino (never coded in any program before) this school year. I#m making a reaction test with an LCD display, a button and 3 LEDS. I#m stuck on the millis() command. I#ve tried to make a sort of "stopwatch" but my arduino book doesnt help.
this is my code:
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C Lcd(0x27,16,2);

int i = 8;
long start;
long stopp;
long diff;
void setup() {

pinMode(i,INPUT_PULLUP);
pinMode(13,OUTPUT);
Lcd.backlight();
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
Lcd.init();
Lcd.backlight();
}
void loop() {
delay(10000);
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
start=millis();
if(i==0){
stopp=millis();
diff=stopp-start;
Lcd.clear();
Lcd.setCursor(4,0);
Lcd.print(diff);
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
delay(100000);
}
delay(100000);
}

I'm also german so this is also more challenging for me in terms of going around the website.

millis() is a builtin function. It returns the number of milliseconds elapsed since the last reset as an unsigned long int. You didn't use code tags, so it's quite hard to make sense of your code, but one thing I did notice:

What are these doing here?

You may like the Deutsch section of the forum.

mixing millis() and delay is not a good practice.

What does that mean?

That's a 20 second delay.

Hello
Take some time and describe in simple words what shall happens and this very simple.
You may post your sketch well formated and in code tags <"/">, too.
This translator will help you to work international.

What is your program supposed to do, exactly?

The variable 'i' that you are testing, is never assigned any value after it's initialized to 8.

use </> to post code

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C Lcd(0x27,16,2);

int i = 8;
long start;
long stopp;
long diff;

void setup() {
    pinMode(i,INPUT_PULLUP);
    pinMode(13,OUTPUT);

    Lcd.backlight();
    pinMode(12,OUTPUT);
    pinMode(11,OUTPUT);
    Lcd.init();
    Lcd.backlight();
}

void loop() {
    delay(10000);
    digitalWrite(13,LOW);
    digitalWrite(12,LOW);
    digitalWrite(11,LOW);

    start=millis();

    if(i==0){
        stopp=millis();
        diff=stopp-start;
        Lcd.clear();
        Lcd.setCursor(4,0);
        Lcd.print(diff);
        digitalWrite(13,HIGH);
        digitalWrite(12,HIGH);
        digitalWrite(11,HIGH);
        delay(100000);
    }
    delay(100000);
}

there are several issues

  • there's no need for delay() at the start and end of loop()
  • there's no need to clear the LEDs in each iteration of loop()
  • "start" needs to capture millis() in setup and ...
  • "i" is a pin that needs to be read using digitalRead() and compared to "0"
  • you probably want a delay after setting the LEDs inside the conditional and then need to clear them as well as well as ... recapture start = stopp

Were there any lessons before this one?

Hi @wouldlikesomehelpthanks

Is this the same subject?

Please don't keep duplicating posts as it affects the help effort.

RV mineirin

@wouldlikesomehelpthanks

I have merged your 2 topics on the same subject. Please do not start multiple topics on the same subject as they just waste the time of the volunteers trying to help you. Please read How to get the best out of this forum , especially the bit about posting code in code tags </> and the bit about us being volunteers.

Thanks,

A rare case in which an identifier is more obscure than the number that it replaces.

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