Addition program

HEy despite my arduino difficulties listed in another topic, I am still going ahead with my project.

I'm trying to create a program which reads the input value from a digital pin and I'm nut sure if I'm doing it write.

Here is the logic of the code I wish to execute.

X= pin1
Y = pin 2

If X is pressed then add to counter

(i.e. if X is pressed 5 times then it outputs a 5)

The second part would then be to

add values across pins

X + Y = total value.

I'm a total noob just trying to figure this thing out. IT would be much appreciated if someone could point out if I'm going about this right.

This sounds like a great place to start.

Some caveats that you'll fall over one way or another:

  1. switch bounce. You don't notice when switching lights on, but all mechanical switches bounce, causing the microcontroller to think you've pressed it lots of times. When you detect a button press, you should wait 100-200mS before checking that button again.

  2. remember to wait for the button to be released! The arduino runs at 16MHz, you don't want it to think that one press means a count of 1882845 or however many times it can loop before you let go.

So, you want something like:

// TODO: work out a better value for this
#define BOUNCEDELAY 32767

// this keeps track of bounce time and presses and things
int press[BUTTONCOUNT];
// this is where you define your buttons. if they will be across multiple ports, add a static volatile * const uchar array[] and use that below instead of PORTB
static const uchar button_pins[BUTTONCOUNT] = {1<<PB1, 1<<PB2, 1<<PB3, 1<<PB5};

// infinite loop
for (;:wink: {
uchar i;
// go through all the buttons
for (i=0;i<BUTTONCOUNT;i++) {
// if it's pressed
if (PORTB & button_pins*) {*

  • // increment our press counter if it's not too large*
    _ if (press < BOUNCEDELAY))_
    _ press*++;
    // if it's good,
    if (press == 1) {
    // feed it to a handler*

    * // TODO: insert your code here*
    * doStuffWithPress(i);
    // if it's not pressed*

    else if (press > 1) {
    * // but it's been pressed recently and we're still in bounce time*
    if (press < BOUNCEDELAY))
    * // increment press counter*
    // we've waited long enough for bounces to go away, and the button isn't pressed*

    if (press >= BOUNCEDELAY)
    * // then zero it*
    press = 0;
    * }

    Note that that came straight out the top of my head and probably needs some TLC before it does what you want_

Thanks a lot man!

Actually, that loop could be made smaller, simpler and easier to read with a little rearranging.

// infinite loop
for (;:wink: {
uchar i;
// go through all the buttons
for (i=0;i<BUTTONCOUNT;i++) {
// if we've had a press recently
if (press*) {*

  • // and enough time has passed*
    _ if (press >= BOUNCEDELAY)_
    * // and it's been released*
    if ((PORTB & button_pins*) == 0) {
    _ // exit debounce time*
    press = 0;
    * }
    // if still in debounce time*

    * else*
    * // count off a wait cycle*
    // we're not debouncing this button, look for a press*

    else if (PORTB & button_pins*) {*
    * // start our debounce timer*
    // and do something with the press*

    * doSomethingWith(i);

    Tell me how it goes :slight_smile:_