Trouble with millis()

Hello,

With this code (neck):

(current millis set before)

while (AB == I) {//////////////////////////
Serial.println(usf);
usf = millis() - currentMillis;
if (usf >= 1000) { ← should work, shouldnt´t it?
C = C - 1;
usf = 0;
}

AB = customKeypad.getKey();
}

It just counts up to infinite milliseconds, but it should stop at 1000 milliseconds and reset usf…
What I´m doing wrong? =(

You need to post a bit more code. You say currentMillis is set beforehand but should it also be reset in the if (usf >= 1000) { statement?

More Code:

void checkDefuse() { lcd.backlight(); if (cd == I) { cd = customKeypad.getKey(); } else {

analogWrite(R, 0); analogWrite(G, 0); analogWrite(B, 255);

currentMillis = millis(); delay(20); Serial.println(currentMillis);

lcd.clear(); lcd.setCursor(0,0); lcd.print("//CODE REQUIRED///"); delay(100); lcd.setCursor(0,1);

while (WB == I) {//////////////////////////

usf = millis() - currentMillis; delay(10); if (usf > 1000) { C--; usf - 1000; delay(10); } Serial.println(usf); WB = customKeypad.getKey(); }

lcd.print(WB); beep1();

Firstly please edit your post to use

// code tags

so we can see the indentation,

Secondly post the entire sketch - how else can we tell what types your variables are?

Thirdly you never reset anything, since setting usf to zero achieves nothing,
being re-assigned everytime round the loop.

Perhaps you want the regular event idiom:

unsigned long last_time = millis () ;

#define DELAY 1000L

void loop ()
{
  if (millis () - last_time >= DELAY)
  {
    last_time += DELAY ;

    // do stuff here
  }
}

OK, I post the code, it should be for a fake bomb... I haven´t finished it totally...

OK, I post the code, it should be for a fake bomb…
I haven´t finished it totally…

#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};
byte rowPins[ROWS] = { 12,8,7,A0 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5,4,3,2, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int R = 9;
int G = 10;
int B = 11;
int speaker = 6;

unsigned long currentMillis;
long previousMillis = 0;

int trash;
int usf;

char W;
char S;
char A;
char D;
char F;

char cd;

char WB;
char SB;
char AB;
char DB;

char NS;

int C;
int Z;
int H;

char MA;

char customKey;
char I;

void setup(){

lcd.begin(16,2);
lcd.backlight();
Serial.begin(9600);
lcd.setCursor(0,0);
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(speaker, OUTPUT);
delay(1000);

}

void(* resetFunc) (void) = 0; //declare reset function @ address 0

void loop(){
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 0);

lcd.clear();
lcd.setCursor(0,0);
analogWrite(speaker, 250);
delay(250);
analogWrite(speaker, 50);
delay(200);
analogWrite(speaker, 0);
lcd.print(“Press #”);

while (MA == I) {
MA = customKeypad.getKey();
}

if (MA == ‘A’) { //ADVANCED MODE

lcd.clear();
lcd.print("/ // MODE A // /");
delay(900);
beep1();
AMO();
}

if (MA == ‘#’) { //Go on
beep1();
}
lcd.clear();

lcd.setCursor(1,0);
analogWrite(R, 255);
analogWrite(G, 135);
lcd.print(“ENTER PASSWORD”);
lcd.setCursor(0,1);
delay(100);
while (W == I) {
W = customKeypad.getKey();
}

lcd.setCursor(0,0);
lcd.clear();
lcd.print(“SET:”);
lcd.setCursor(0,1);
lcd.print(W);
beep1();
lcd.print("-");

while (S == I) {
S = customKeypad.getKey();
}
lcd.print(S);
beep1();
lcd.print("-");

while (A == I) {
A = customKeypad.getKey();
}
lcd.print(A);
beep1();
lcd.print("-");

while (D == I) {
D = customKeypad.getKey();
}
lcd.print(D);
beep1();

lcd.clear();
lcd.setCursor(0,0);
analogWrite(R, 0);
analogWrite(G, 255);
delay(250);
analogWrite(speaker, 60);
delay(250);
analogWrite(speaker, 0);
delay(500);
lcd.print("///////ACTIVATED");
analogWrite(speaker, 222);
analogWrite(R, 255);
analogWrite(G, 0);
delay(2000);
analogWrite(speaker, 0);

C = 60; //60000ms = 1 Minute

while (C != 0) {
checkDefuse();

lcd.clear();
lcd.setCursor(0,0);
lcd.print("//Explosion in//");
lcd.setCursor(7,1);
lcd.print(C);
analogWrite(R, 255);
beep1();
lcd.print(“s”);
checkDefuse();
analogWrite(R, 0);

if (C <= 5) {
lcd.noBacklight();
currentMillis = millis();
while (millis() - currentMillis < 400) {checkDefuse(); }

analogWrite(speaker, 80);
currentMillis = millis();
while (millis() - currentMillis < 200) {checkDefuse(); }
analogWrite(speaker, 0);
lcd.backlight();
currentMillis = millis();
while (millis() - currentMillis < 400) {checkDefuse(); }
}
checkDefuse();

if (C <= 10 && C > 5) {
lcd.noBacklight();
currentMillis = millis();
while (millis() - currentMillis < 500) {checkDefuse(); }
beep1();
lcd.backlight();
currentMillis = millis();
while (millis() - currentMillis < 400) {checkDefuse(); }

}
checkDefuse();
if (C > 10) {
currentMillis = millis();
while (millis() - currentMillis < 900) {checkDefuse(); }
}
checkDefuse();

C = C - 1;

trash = 0;
}

expl();
}

void AMO() { //ADVANCED MODE
lcd.clear();
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 255);
delay(2000);

}

void checkDefuse() {
lcd.backlight();
if (cd == I) {
cd = customKeypad.getKey();
} else {

analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 255);

currentMillis = millis();
delay(20);
Serial.println(currentMillis);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("//CODE REQUIRED///");
delay(100);
lcd.setCursor(0,1);

while (WB == I) {//////////////////////////

usf = millis() - currentMillis;
delay(10);
if (usf > 1000) {
C–;
usf - 1000;
delay(10);
}
Serial.println(usf);
WB = customKeypad.getKey();
}

lcd.print(WB);
beep1();
lcd.print("-");

while (SB == I) {//////////////////////////
Serial.println(usf);
usf = millis() - currentMillis;
if (usf >= 1000) {
C = C - 1;
usf = 0;
}

SB = customKeypad.getKey();
}
lcd.print(SB);
beep1();
lcd.print("-");

while (AB == I) {//////////////////////////
Serial.println(usf);
usf = millis() - currentMillis;
if (usf >= 1000) {
C = C - 1;
usf = 0;
}

AB = customKeypad.getKey();
}
lcd.print(AB);
beep1();
lcd.print("-");

while (DB == I) {//////////////////////////
Serial.println(usf);
usf = millis() - currentMillis;
if (usf >= 1000) {
C = C - 1;
usf = 0;
}

DB = customKeypad.getKey();
}
lcd.print(DB);
beep1();
delay(100);

if (WB == W && SB == S && AB == A && DB == D) {

analogWrite(R, 0);
analogWrite(G, 255);
analogWrite(B, 0);

analogWrite(speaker, 250);
delay(500);
analogWrite(speaker, 0);
delay(250);
analogWrite(speaker, 50);
delay(1000);
analogWrite(speaker, 0);

lcd.setCursor(0,0);
lcd.clear();
lcd.print("//BOMB DEFUSED//");
delay(20000);
resetFunc();
} else {

C = 0;
expl();

}

}

}

void expl() {

while ( C <= 0) {
analogWrite(R, 255);
analogWrite(G, 0);
analogWrite(B, 0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("////EXPLODED/////");
lcd.setCursor(0,1);
lcd.print(“PRESS # TO RESET”);

while (NS == I) {

analogWrite(speaker, 80);
delay(1000);
analogWrite(speaker, 0);
delay(200);
NS = customKeypad.getKey();

}

if (NS == ‘#’) {
resetFunc();
}
}
}

void beep1() {

analogWrite(speaker, 250);
delay(100);
analogWrite(speaker, 0);
}

No, use code tags. You did read the two sticky threads before posting????

And take out all those blank lines. They add nothing and make your code more difficult to read.

Long sketches are all but impossible to read unless you use code tags.

Read the sticky post on code tags, then go edit your posts to use them.

Mindstormsnxt:
OK, I post the code, it should be for a fake bomb…
I haven´t finished it totally…

–snip! unformatted code deleted (cause nobody can read it anyway!)–