Go Down

Topic: Problem incrementing a value! (Read 123 times) previous topic - next topic

andyedtec

Mar 18, 2015, 10:30 pm Last Edit: Mar 18, 2015, 11:09 pm by andyedtec
Hi, I have a value that is an INT. This is the value I need to increment.

I have something like this.

Code: [Select]
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

guix

#1
Mar 18, 2015, 10:35 pm Last Edit: Mar 18, 2015, 10:36 pm by guix
Hello and welcome,

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


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?
Tiny841 + 1634 Core:http://drazzy.com/e/tiny841.shtml
ATTiny core for 1.6.x, for x4/x5/x61/x7/x8 series) https://github.com/SpenceKonde/ATTinyCore

aarg

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.

andyedtec

#4
Mar 18, 2015, 10:58 pm Last Edit: Mar 18, 2015, 11:12 pm by andyedtec
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
Code: [Select]
int value = 10;

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

Code: [Select]
Value = Value+1;
 
OR
Code: [Select]
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,

andyedtec

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

My code follows:


Code: [Select]
#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; 
   }
 
 }
 

guix

#6
Mar 18, 2015, 11:38 pm Last Edit: Mar 18, 2015, 11:40 pm by 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:
Code: [Select]

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

KeithRB

Why is count a float?

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

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.

aarg

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"?

andyedtec

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



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:
Code: [Select]

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.

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.

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy