Go Down

Topic: array of Bounce objects ? (Read 2475 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


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.

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 )
};

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);
     }
   }
   
}
}

Go Up