Go Down

Topic: Need code for shifting numbers. (Read 582 times) previous topic - next topic

miro2424

I have 10 pushbuttons connected to my Arduino numbered from 0 to 9 and I need a 3-digit variable that's changing as one presses the pushbuttons;

ex.      Press 4         Variable: 004
          Press 6         Variable: 046
          Press 7         Variable: 467
          Press 3         Variable: 673    etc.

Please help.

sy kristina

What you are looking for is arrays.
First you declare your arrays (call them array[2] and lastarray[2].

In your loop, you first make lastarray equal to array (for i= 0 to 2)

Then you look for button changes. If a button is changed, the number will be the new array[2], array[1] will be lastarray[2] and array[0] will be lastarray[1]

There are probably more elegant ways to do this, but if you are not running too much other calculations, it should work.

Joachim

PaulS

You don't need an array at all. Define an int, value. Set it to 0.

When a button is pushed:

Code: [Select]
value *= 10;
value += btnNumber;
value %= 1000;

TchnclFl

#3
Dec 19, 2009, 10:56 pm Last Edit: Dec 19, 2009, 11:06 pm by Tchnclfl Reason: 1
Quote
You don't need an array at all. Define an int, value. Set it to 0.

When a button is pushed:


Code: [Select]
value *= 10;
value += btnNumber;
value %= 1000;



Smart way to do it.

Personally, I'd get lazy and do this:

Code: [Select]
if (digitalRead(button1) == HIGH) {
first = second
second = third
third = 1
}

and repeat the code for all of the buttons.

Don't forget to debounce though.

AWOL

Quote
But you'd also need a function that deletes the first number after every three presses


"% 1000" ?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TchnclFl

Quote

"% 1000" ?


Whoops. Didn't notice that the first time :P.  I'll modify my original post. ;D

Go Up