Int declarations

Hi people, New to the Arduino but not programming although I am not used to this language.

At the start I have declared and integer, during the initial setup I ask for a question for time required (1-35mins, 2-40mins or 3-45mins). I then have the timer counting down in the void setup. Problem I am having is when I try to set the Mcount in the setup phase, the value is never followed through to the timer. What am I doing wrong? Thanks in advanced.

int Mcount = 00;

Void setup()

lcd.cursor();
lcd.setCursor(0,0);
lcd.print("Select Time:");
delay(3000);
lcd.setCursor(0,0);
lcd.print("1(35) 2(40) 3(45)");
delay(1000);

//Code Input Array for 1 numbers
while (currentLength2 <1)
{
lcd.setCursor(currentLength2, 1);
lcd.cursor();
char key3 = customKeypad.getKey();
key3 == NO_KEY;
if (key3 != NO_KEY)
{
if (key3 == '1'){
int Hcount=00;
int Mcount=35;
lcd.print(Mcount);
delay(500);
}
else
if (key3 == '2') {
int Hcount=00;
int Mcount=40;
lcd.print(Mcount);
delay(500);
}
else
if (key3 == '3') {
int Hcount=00;
int Mcount=45;
lcd.print(Mcount);

Void setup()

lcd.cursor();

void is spelled all lowercase.
You need more {} braces.

Please remember to use code tags when posting code.

The language is C/C++.

Hi awol, have annotated some of the more relevant lines of code. It's simply an exercise in setting a timer, that a code can terminate the timer. You can set the code and select the time you want. Once the timer is counting, you can press * and enter the code for it to stop.

#include <LiquidCrystal.h>
#include <Keypad.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(32, 34, 36, 38, 40, 42);

int Scount = 01; // count seconds
int Mcount = 00; // count minutes
int Hcount = 00; // count hours
long secMillis = 0; // store last time for second add
long interval = 1000; // interval for seconds
char password[6]; // number of characters in our password
int currentLength = 0; //defines which number we are currently writing for reset code
int currentLength2 = 0; //defines which number we are currently writing for selecting time
char entered[6]; //This keep track of what is entered for the code

void setup() {
lcd.cursor();
lcd.setCursor(0,0);
lcd.print("Select Time:");
lcd.setCursor(0,0);
lcd.print("1(35) 2(40) 3(45)"); // Select 1 for 35 mins etc
delay(1000);

//Code Input Array for 1 numbers

while (currentLength2 <1) // I am using this to ensure that only 1 press is detected
{
lcd.setCursor(currentLength2, 1);
lcd.cursor();
char key3 = customKeypad.getKey(); // This is my call for detecting what is pressed
key3 == NO_KEY;
if (key3 != NO_KEY)
{
if (key3 == '1'){ //So if '1' is pressed on the kepad
int Hcount=00; // I want to set the global Hour Count to 0
int Mcount=35; // and the minute count to 35.
delay(500); //I did have an lcd.print(Mcount)
}
else //if '1' isn't pressed
if (key3 == '2') { // if '2' is pressed
int Hcount=00;
int Mcount=40;
delay(500);
}
else
if (key3 == '3') { //and if 3 is pressed
int Hcount=00;
int Mcount=45;
delay(500);
}

void loop()
{

timer(); //This calls the timer countdown

char key2 = customKeypad.getKey(); // get the key input for the countdown code.

if (key2 == '*')
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Code: "); //ready for code input

while (currentLength < 6)
{

timer();

char key2 = customKeypad.getKey();
if (key2 == '#')
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Code: ");
}
else
if (key2 != NO_KEY)
{

lcd.setCursor(currentLength + 7, 0);
lcd.cursor();

lcd.print(key2);

entered[currentLength] = key2; // Array to keep track of the 6 digit code
currentLength++;
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}

if (currentLength == 6)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3]&& entered[4] == password[4]&& entered[5] == password[5])
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Timer stopped. Reset?");
currentLength = 0;
digitalWrite(ledPin2,HIGH); //Switch Off Green Part of LED
digitalWrite(ledPin6,LOW); //Switch Off Green Part of LED
digitalWrite(ledPin2,LOW); //Switch Off Green Part of LED
digitalWrite(ledPin6,LOW); //Switch Off Green Part of LED
delay(2500);
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Code!");

if (Hcount > 0)
{
Hcount = Hcount - 1;
}

if (Mcount > 0)
{
Mcount = Mcount - 59;
}
if (Scount > 0)
{
Scount = Scount - 59;
}
delay(1500);
currentLength = 0;

}
}
}
}

void timer() //Start the Timer countdown
{

unsigned char i;
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Timer Completed! ");

///Show RED LED
digitalWrite(ledPin2,LOW); //Switch Off Green Part of LED
digitalWrite(ledPin3,HIGH); // switch On Red Part of LED
digitalWrite(ledPin4,LOW); // switch on LED
digitalWrite(ledPin5,HIGH); // switch on LED
digitalWrite(ledPin6,HIGH); // switch on LED

while (Mcount < 0)
{
currentLength = 0;
// digitalWrite(ledPin3, HIGH);
delay(2500);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Reset timer");
currentLength = 0;
}
}
}

lcd.setCursor (0,1); // sets cursor to 2nd line
lcd.print ("Timer:");

if (Hcount >= 10)
{
lcd.setCursor (7,1);
lcd.print (Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7,1);
lcd.write ("0");
lcd.setCursor (8,1);
lcd.print (Hcount);
}

lcd.print (":");

if (Mcount >= 10)
{
lcd.setCursor (10,1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10,1);
lcd.write ("0");
lcd.setCursor (11,1);
lcd.print (Mcount);

}
if (Mcount < 1)
{
lcd.setCursor (10,1);
lcd.write ("0");
lcd.setCursor (11,1);
lcd.print (Mcount);

}
lcd.print (":");

if (Scount >= 10)
{
lcd.setCursor (13,1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13,1);
lcd.write ("0");
lcd.setCursor (14,1);
lcd.print (Scount);
}

if (Hcount <0)
{
Hcount = 0;
}

if (Mcount <0)
{
Hcount --;
Mcount = 59;
}

if (Scount <1) // if 60 do this operation
{
Mcount --; // add 1 to Mcount
Scount = 59; // reset Scount
}

if (Scount > 0) // do this oper. 59 times
{
unsigned long currentMillis = millis();

if(currentMillis - secMillis > interval)
{

secMillis = currentMillis;
Scount --; // add 1 to Scount

{
digitalWrite(ledPin2,HIGH); // switch on LED
delay(200); // wait for one second
digitalWrite(ledPin2,LOW); // switch off LED
}
}
}
}

   int Mcount=35;                   // and the minute count to 35.
   delay(500);                        //I did have an lcd.print(Mcount)
      }
      else                                //if '1' isn't pressed
      if (key3 == '2') {            // if '2' is pressed
   int Hcount=00;
   int Mcount=40;

You need to read about scope rules for C/C++.

You really need to start using code tags (already noted) and use the auto format feature of the IDE

Hi Shannon, Thank you so much for your reply.

I would like to simply use the first Mcount declared at the start of the code.

Initially it is set at the beginning but I then want a user input to be able to select either 1 for 35 mins, 2 for 40 mins or 3 for 45 mins and therefore change the initial time set for Mcount with the user selected time if that makes sence.

Instead of

   int Mcount=35;                   // and the minute count to 35.
   delay(500);                        //I did have an lcd.print(Mcount)
      }

just simply

   Mcount=35;                   // and the minute count to 35.
   delay(500);                        //I did have an lcd.print(Mcount)
      }

Hi Awol,

I am using that code but it never changes the value of Mcount. I inserted the "int" just to rule that out as the problem. It simply never holds the value that Is et and I think it is because it is outside the function despite setting Mcount as a global integer.

Please post the code that you are describing in post #7

Delta, have you even read the code or are you one of those people who simply spam questions pointing out everyone's faults? :o

I am assigning a value to an INTeger ie and integer I have labelled Mcount.

I am assigning a value to an INTeger ie and integer I have labelled Mcount.

Yes, but do you know what using int in front of the variable name means ?

HINT : it does not just mean that the variable is an integer.

slaurence1:
Delta, have you even read the code or are you one of those people who simply spam questions pointing out everyone's faults? :o

I am assigning a value to an INTeger ie and integer I have labelled Mcount.

slarurence, have you even read the clearly-posted forum posting guidelines, or are you one of those people who thinks netiquette and good manners are for others?

Haha AWOL, sorry, but no, I cannot see any clearly-posted forum guidelines. I thought my posts were perfect netiquette.

As a moderator, did you think your post was any different to mine? "slarurence, have you even read the clearly-posted forum posting guidelines, or are you one of those people who thinks netiquette and good manners are for others?"

With Arduino:

int

Description

Integers are your primary data-type for number storage.

On the Arduino Uno (and other ATMega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).
On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).

int's store negative numbers with a technique called 2's complement math. The highest bit, sometimes referred to as the "sign" bit, flags the number as a negative number. The rest of the bits are inverted and 1 is added.

The Arduino takes care of dealing with negative numbers for you, so that arithmetic operations work transparently in the expected manner.

New to the Arduino but not programming

So you know about scope, even if you haven't come across it in C/C++.
You aren't new to programming, right?

Haha AWOL, sorry, but no, I cannot see any clearly-posted forum guidelines

They're here and here, at the very least.
Posted at the top of this, and other sections of the forum.
We even put their titles in bold, to make them stand out.

They're very hard to miss.
But it seems you managed it.
Ho-hum

Everything you quote in post #13 is true but there is one aspect of using a type in front of a variable name that you have missed and that is doing so creates a new variable with the variable name in the scope in which it is declared.

What amuses me is that the title of this thread has the cause of your problem in it.