Go Down

Topic: array of Bounce objects ? (Read 4593 times) previous topic - next topic

Z.K.

Is it possible to create an array of Bounce objects?  I have tried several ways, but I always get this huge error list.  I would like to iterate through the buttons and LEDs without having to have individual variables for each.


code:
Code: [Select]


#include <Bounce.h>
#define BUTTON1 8;
#define BUTTON2 9;
#define BUTTON3 10;
#define BUTTON4 11;
#define BUTTON5 12;

Bounce bouncer[5];

bouncer[0] = Bounce( BUTTON1,5 );
bouncer[1] = Bounce( BUTTON2,5 );
bouncer[2] = Bounce( BUTTON3,5 );
bouncer[3] = Bounce( BUTTON4,5 );
bouncer[4] = Bounce( BUTTON5,5 );

int vals[5];


void setup()
{
 
 
}

void loop()
{
    for(int i=0; i<100; i++)
   {
//bouncer[i].update();
//vals[i] = bouncer[i].read();
   }
}





errors:
Quote


LEDswitchbouncetest6:7: error: no matching function for call to 'Bounce::Bounce()'
/home/zk/sketchbook/libraries/Bounce/Bounce.h:38: note: candidates are: Bounce::Bounce(uint8_t, long unsigned int)
/home/zk/sketchbook/libraries/Bounce/Bounce.h:34: note:                 Bounce::Bounce(const Bounce&)
LEDswitchbouncetest6:9: error: expected constructor, destructor, or type conversion before '=' token
LEDswitchbouncetest6:9: error: expected unqualified-id before ',' token
LEDswitchbouncetest6:9: error: expected unqualified-id before numeric constant
LEDswitchbouncetest6:10: error: expected constructor, destructor, or type conversion before '=' token
LEDswitchbouncetest6:10: error: expected unqualified-id before ',' token
LEDswitchbouncetest6:10: error: expected unqualified-id before numeric constant
LEDswitchbouncetest6:11: error: expected constructor, destructor, or type conversion before '=' token
LEDswitchbouncetest6:11: error: expected unqualified-id before ',' token
LEDswitchbouncetest6:11: error: expected unqualified-id before numeric constant
LEDswitchbouncetest6:12: error: expected constructor, destructor, or type conversion before '=' token
LEDswitchbouncetest6:12: error: expected unqualified-id before ',' token
LEDswitchbouncetest6:12: error: expected unqualified-id before numeric constant
LEDswitchbouncetest6:13: error: expected constructor, destructor, or type conversion before '=' token
LEDswitchbouncetest6:13: error: expected unqualified-id before ',' token
LEDswitchbouncetest6:13: error: expected unqualified-id before numeric constant


jackokring

I think you need an array of size five of POINTER to bounce. Ten fill it by addressing each new bounce via & and assigning it to an array element. Then dereference via * before using an element of the array. Object arrays are filled with default bounce objects, and there is no default no argument constructor.

AWOL

Code: [Select]
Bounce bouncer[5];

bouncer[0] = Bounce( BUTTON1,5 );
bouncer[1] = Bounce( BUTTON2,5 );
bouncer[2] = Bounce( BUTTON3,5 );
bouncer[3] = Bounce( BUTTON4,5 );
bouncer[4] = Bounce( BUTTON5,5 );

Executable code is not allowed outside of functions.
Either make it an initialised array, or put the assignments into "setup".
"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.

dangerdave

Hey Guys,
I'm having the same Problem.
I m working on a Project with at least 20-30 Buttons, and i would love to use the Bounce library, but not with single varibles.

im ok with pointers, but i could not get my Bounce-POINTer-Array-Construct to work...

Can someone post a small code excerpt, or did you, Z.K. find a solution???

thanks a lot!



AWOL

Quote
ok with pointers, but i could not get my Bounce-POINTer-Array-Construct to work...

You need to post your code, otherwise we'd be doing your programming, and that would be no fun for you.
"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.

WizenedEE

try this:
Code: [Select]

Bounce bouncer[5] = {
  Bounce( BUTTON1,5 ),
  Bounce( BUTTON2,5 ),
  Bounce( BUTTON3,5 ),
  Bounce( BUTTON4,5 ),
  Bounce( BUTTON5,5 )
};

dangerdave

Weird. I tried this declaration WizenedEE earlier my self, but it did not work.

Now i copied yours and it worked!!!
maybe some stupid syntax error :)

Quote
You need to post your code, otherwise we'd be doing your programming, and that would be no fun for you.

@ AWOL you are completely right, but it was really rubish in the end, now it works anyway!

Thanks guys

ccerrito

Hi,

I recently came across this post, and have a related question:

Would it be possible to use some sort of for loop to create an array of bounce objects? I'm trying to do something like this:

Code: [Select]

int BUTTON[] = {2,3,4,5,6};  //button pins
const int numButtons = 5; 


Bounce bouncer[5];

for( int i = 0; i < numButtons; i++){
   Bounce bouncer[i] = (BUTTON[i], 5);
}


But keep getting a:

"no matching function for call to 'Bounce::Bounce()'"

error...it seems to be choking on the object array declaration? Any advice on this would be great...

Thanks!


WizenedEE


Would it be possible to use some sort of for loop to create an array of bounce objects? I'm trying to do something like this:


Not like that. You could use dynamic memory management (which you don't really want to do), but other than that you have to do it like I showed.

That's one of the main reasons why it's a good idea to give every class a no-argument constructor --- you can just make an array and then assign the needed values later.

What's happening here is that the C++ standard guarantees every object will have a constructor called. So if you make an array of objects, it must call a constructor for each one of them right then and there (if it was allowed to wait you might do something with them before their constructors were called). Thus, you have to give every value it needs right then and there.

PeterH


Code: [Select]

   Bounce bouncer[i] = (BUTTON[i], 5);



That doesn't make sense. You're declaring a local array of Bounce of length i, named bouncer. And you're initialising it to the number 5.

Given that you declared a global array of Bounce, those objects now already exist. It's too late to try to construct them. If you declared the global array bouncer to be an array of pointers to Bounce then you could loop through the array and create a new Bounce and store the addresses in the array. It'd mean accessing them through pointers, but I don't suppose that will bother you.
I only provide help via the forum - please do not contact me for private consultancy.

Paul Beaudet

I think the OP here was trying to create one point of set-up to make the program expandable
(to add buttons)
I'm not sure I understand the limitation against it quite yet but here is a working example without
for those still stumbling upon this topic like I did today.

Code: [Select]
#include <Bounce.h>

// This code turns a led on/off through debounced buttons
// Buttons wired one side to pin the other 330oms to ground

//#define B0 4// i have a feeling there is
//#define B1 5// an injection issue with these first two
#define B2 6
#define B3 7
byte buttons[]={6,7,4,5};//seperate array from definitions to set up the pins
#define NUMBUTTONS sizeof(buttons)//gives size of array *helps for adding buttons

#define LED 13
int ledValue = LOW;

// I really dont see getting around doing this manually
Bounce bouncer[] = {//would guess thats what the fuss is about
  Bounce(4,5),
  Bounce(5,5),
  Bounce(B2,5),
  Bounce(B3,5)
};

void setup() {
   for (byte set=0;set<NUMBUTTONS;set++){//sets the button pins
     pinMode(buttons[set],INPUT);
     digitalWrite(buttons[set],HIGH);//<-comment out this line if not using internal pull ups
   }//-----------------------------------and change read()==to high if your set up requires
  pinMode(LED,OUTPUT);//------------------otherwise event will occure on release
}

void loop() {
// test turning on a led with any of the debounced buttons
for(int num=0;num<NUMBUTTONS;num++){
   if ( bouncer[num].update()) {
     if ( bouncer[num].read() == LOW) {
       if ( ledValue == LOW ) {
         ledValue = HIGH;
       } else {
         ledValue = LOW;
       }
       digitalWrite(LED,ledValue);
     }
   }
   
}
}

theDanimal

#11
Feb 24, 2017, 04:39 pm Last Edit: Feb 24, 2017, 04:45 pm by theDanimal
I think you need an array of size five of POINTER to bounce. Ten fill it by addressing each new bounce via & and assigning it to an array element. Then dereference via * before using an element of the array. Object arrays are filled with default bounce objects, and there is no default no argument constructor.
This seems like the best solution to what the OP wanted to do, and sorry this is an old thread but It's kind of bugging me that this is the first post that comes up in google, and there isn't a demonstration of this suggestion.

It's been a long time since I've done C++ programming, so someone please tell me if I'm mistaken but this would do it, right?

Code: [Select]

int numButtons = 5;
int debounce = 5;
   
// create array of pointers of Bounce size
Bounce *buttPtrs[numButtons];

int pins[] = {2, 3, 4, 5, 6};

void setup() {

    Serial.begin(38400);

    for (int i = 0; i < numButtons; i++) {
        int pin = pins[i];
        // any other setup, like setting pinmode
        pinMode(pin, INPUT_PULLUP);

        // create a Bounce object
        Bounce bb = Bounce(pin, debounce);
        // assign its address to the pointer in the array
        butrPtrs[i] = &bb;
    }
}

void loop() {
    for (int i = 0; i < numButtons; i++) {
        // Get the object at the address held by the pointer
        Bounce butt = *buttPtrs[i];

        butt.update();
        if (butt.fallingEdge()) {
            Serial.print("Button ");
            Serial.print(i);
            Serial.println(" has been pressed!");
        }
        if (butt.risingEdge()) {
            Serial.print("Button ");
            Serial.print(i);
            Serial.println(" has been released!");               
        }
    }
}

Jiggy-Ninja

No, that will not work. Bounce bb is created with local scope, so it is destroyed and reconstructed on every iteration. It is then destroyed for good when the loop is over. All you have accomplished was to fill the array with dangling pointers.

RayLivingston

#13
Feb 24, 2017, 06:44 pm Last Edit: Feb 24, 2017, 06:45 pm by RayLivingston
No, that will not work. Bounce bb is created with local scope, so it is destroyed and reconstructed on every iteration. It is then destroyed for good when the loop is over. All you have accomplished was to fill the array with dangling pointers.
No, that is wrong. bb, the pointer to the new object, is destroyed, but the object itself is not.  c++ does NOT do garbage collection, so the object remains allocated unless and until the code explicitly deletes the object.  That snippet will work just fine (well, assuming the obvious typo is fixed...).  And bb is created on each iteration of the loop, and destroyed at the end of each iteration of the loop.  There is no "destroyed for good when the loop is over".

Regards,
Ray L.

Jiggy-Ninja

Someone needs to learn the difference between stack and heap allocation.

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