Pages: [1]   Go Down
Author Topic: Need code for shifting numbers.  (Read 552 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Moss
Offline Offline
Full Member
***
Karma: 1
Posts: 119
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

When a button is pushed:

Code:
value *= 10;
value += btnNumber;
value %= 1000;
Logged

B0100111001000011, USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1503
I'm confused.  Wait, maybe not..
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

When a button is pushed:


Code:
value *= 10;
value += btnNumber;
value %= 1000;

Smart way to do it.

Personally, I'd get lazy and do this:

Code:
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.
« Last Edit: December 19, 2009, 05:06:37 pm by Tchnclfl » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"% 1000" ?
Logged

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

B0100111001000011, USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1503
I'm confused.  Wait, maybe not..
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
"% 1000" ?

Whoops. Didn't notice that the first time smiley-razz.  I'll modify my original post. ;D
Logged

Pages: [1]   Go Up
Jump to: