Author Topic: Return value is not updated in Arduino?  (Read 2010 times)

0 Members and 1 Guest are viewing this topic.

Offline Mr1234kaki

  • Contributor
  • Posts: 11
Return value is not updated in Arduino?
« on: September 08, 2012, 12:02:52 pm »
Hi there, please help me,   :)
ive been working on this for a week already,  :-\

//The circuit will have 3 buttons, one for change mode, one for set the value and another one will count the number of button pushes,
//The circuit will initially start at 25, after change mode button is pushed once, the program will jump to fucntion count
//the initial value will be added with the number of button being pushes
//The set button is push so we can return the new value to the main loop function
//If we push the button mode once more, the program will jump to function count2
//at count2, the function suppose to decrease the value as we set before corresponding to the number of button push being pushes

// eg. (initial Y=25, >>>> set to Y=30 >>>> decrease back to Y=23 from Y=30)

//problem happens after executes function counter,
//once we updated the final value Y, we return to main loop and display the final value on the lCD
//but when we move to counter2, the final value Y is not updated from what we have done before, instead it will decrease from the intial value

//eg. initially Y=25 >>>>>> press buttonmode to HIGH >>>> counter (add value to initial value 25) >>>> (press buttonset to HIGH) set value to Y=30 >>>> press buttonmode to HIGH again
//>>>> counter2 (should decrease value from Y=30 but Y is not updated and decrease Y from intial 25 instead) >>> (press buttonset to HIGH) set value Y=27 for example)

Thank you very much, Cheers! ;)
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Return value is not updated in Arduino?
« Reply #1 on: September 08, 2012, 12:21:44 pm »
Seems to me your using too many variables. Try removing x and y.
You should be able to do this with just pushcounter and pushcounter2.

Regards
Testing one two three...
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 4260
  • Country: nl
Re: Return value is not updated in Arduino?
« Reply #2 on: September 08, 2012, 07:24:02 pm »
In counter2() you are doing y=x - pushcounter2; which uses the original value of 25 again since you are not changing it anywhere. I understand that you want to display the new value each time the button is pressed, so:

 - make both x and y volatile and initial 25

 - start both counter loops with x = y;

 - use the x to display the results when the button is pressed:
   x = x + pushcounter;  (or - for counter2)
   delay(500);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print(x);


- then when exiting the counter loop do
  y = y + pushcounter; (or - for counter2) 

That should work.

And I agree with PeterG that you are using too many variables  ;)

Keyboard error: Press F1 to continue.
 

Online grumpydoc

  • Super Contributor
  • ***
  • Posts: 2701
  • Country: gb
Re: Return value is not updated in Arduino?
« Reply #3 on: September 08, 2012, 08:22:47 pm »
Quote
And I agree with PeterG that you are using too many variables  ;)
Not to mention too many (almost) identical functions.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf