Problem incrementing a value!

Hi, I have a value that is an INT. This is the value I need to increment.

I have something like this.

If Number1 == Number2
{

value1 =value1+1;

}

But because the if statement is true the value just increments for an amount which I think
is the program scan time.

Can anybody point me in the right direction.

Thanks

Andy

Hello and welcome,

I don't understand your problem or your question. Please post all of your program.

Where are you putting that code? Is there anything around it? I assume there must be, else it wouldn't compile.

Is Number1 supposed to be the same variable as value1?

What exactly is it doing that's not correct?

DrAzzy:
Where are you putting that code? Is there anything around it? I assume there must be, else it wouldn't compile.

Is Number1 supposed to be the same variable as value1?

What exactly is it doing that's not correct?

Actually, an if statement without parentheses won't ever compile.

Thanks for your reply.

I will try and post my code.

Number1 and number2 are declared as bytes.
Number1 is a byte returned from a keypad
Number2 is a byte that is used to compare against and execute code in the brackets
after the IF statement when num1 and 2 are the same.

In the code in the brackets have a Value which is an Int.
The int is set to 10 before setup like this

int value = 10;

then the code to run when the IF is true is simply to increment Value by 1.

Value = Value+1;

OR

Value++

But the value just increments by a silly amount because I think that the IF
statement is still true.

Hope this makes sense.

Code to follow,

Sorry for all my spelling mistakes and grammar. It has been a long day.

My code follows:

#define C1 13 /* DIO for keypad column 1 */
#define C2 10 /* DIO for keypad column 2 */
#define C3 9  /* DIO for keypad column 3 */
#define C4 8  /* DIO for keypad column 4. Delete if there is no column 4! */
#define C5 7  /* DIO for keypad column 5. Delete if there is no column 5! */
#define C6 6  /* DIO for keypad column 6. Delete if there is no column 6! */

#define R1 A1 /* DIO for keypad row 1 */
HCMatrixKeypad Keypad(DEBOUNCE, C1, C2, C3, C4, C5, C6, ROWMARKER, R1); /* Uncomment for 6x1 keypad */
byte key = 0;
byte Left = 4;
byte Right = 3;
float setpoint = 10;

void setup()
{
    Serial.begin(9600);
}

/* Main program */
void loop()
{
  
 
  /* Scans the keypad once. This line needs to be run repeatedly */
  Keypad.Scan();
  andy();
  /* Has a new key been pressed  */ 
  if(Keypad.New_Key()) 
  {
    /* If so the send the key to the serial port */
        
    key = (Keypad.Read() % 10); /* 1's column is the keypad column number */
    Serial.print("Key number");
    Serial.print (key);
    delay(2000);
    Serial.print("setpoint");
    Serial.print (setpoint);
     
}

}

 void andy()
 {
   
   if (Left == key) {
   
    setpoint = setpoint+1;
    
   return;  
   }
 
 }

When you press the key you assign it's value to variable "key". But you never reset this variable after the key has been released or whenever you need it to be reset, so this variable will keep the same value, so your if statement will be true, until you reset it.

You probably want to do something like this:

if (Left == key) {
   key = 0;
   setpoint = setpoint+1;

Why is count a float?

If you are counting keypresses you need to set key to zero inside andy()

You're calling andy() before checking Keypad.New_Key() to see if there is a new key press. Move the call to andy() to inside the if loop where you test that (a few lines down). See if that helps.

Also, (left == key) is technically correct, but the best style is to follow the subject-object convention in English. It makes it easier to read.

Would I say, "if the key is 8", or "if the 8 is key"?

That is just what I wanted will test and post back.
Thanks for taking the time to clearly explain.

guix:
When you press the key you assign it's value to variable "key". But you never reset this variable after the key has been released or whenever you need it to be reset, so this variable will keep the same value, so your if statement will be true, until you reset it.

You probably want to do something like this:

if (Left == key) {

key = 0;
  setpoint = setpoint+1;

Kieth, Thanks is not a count it is a float because I need to increment it by .25 or 1 or whatever.

KeithRB:
Why is count a float?

If you are counting keypresses you need to set key to zero inside andy()

aarg you are right, because I have to press the key twice when the program starts for the first time before
it starts running. I knew about it.

aarg:
You're calling andy() before checking Keypad.New_Key() to see if there is a new key press. Move the call to andy() to inside the if loop where you test that (a few lines down). See if that helps.