Multiplexer implementation

OK, to be clear, that's not real code, yet

I guess that I have to put it in a for cycle 

No. And no switch/case either. You are only doing one of the ten cases in your current switch.

The value of X in the pseudo-code would just be

       searchButton(button)

Here's that section of code closer to real code:

// First we need the bidder names. At the top of the sketch:

String bidderName[10] = {
	"Pino",
	"Manuel",
	"Luca",
	"Giuseppe",
	"Simone",
	"Danilo",
	"Daniele",
	"Mario",
	"Pino",
	 "Manuel"
};

// And the button addresses (pin numbers). At the top of the sketch:

const int BUTTON_PIN[10] = {2, 3, 28, 29, 30, 31, 32, 33, 34, 35};

// And the LED pins. Yes, at the top of the sketch:

const int LED_PIN[10] = {13, 8, 20, 21, 22, 23, 24, 25, 26, 27};

//
// And, finally, we can write (replacing your entire switch/case ) 
//

    int theWinner = searchButton(button);

    winner = bidderName[theWinner];
    lastBidder = BUTTON_PINS[theWinner];
    Serial.println(String("Actual winner: ") + winner);
    digitalWrite(LED_PIN[theWinner], HIGH);
  

I can't test this, and I don't use Capital S Strings (and neither should you, for much longer), so I hope an array of them works like it should, I would bet it's fine.

Go to your favorite learning source and look into arrays and indexing in C/C++ and see if you can make sense of the above.

I reiterate - any time you are copy, pasting and editing code, or have variable names made by appending a digit to a name, like cat_0, cat_1, cat_2… you should get a clue that an array would be useful.

HTH and see you on the learning curve.

a7

That is a short time, how is the hardware construction going? From your pin assignments, I guess that you might have a Mega 2560. If so, you have more than enough pins to drive all the LEDs and push buttons, without any shift registers or multiplexers.

In fact, doing so is much, much easier than using either of those, and the code will also be simpler. But, you've painted yourself into a corner by copy and pasting cookie cutter code that uses interrupts. Well, that avenue is not open to you because the Mega only has 6 input interrupt capable pins. You can simply poll the pins, as most people do, as the IDE examples show, and as I suggested back in reply #6.

Anything else that you attempt will likely result in missing your deadline, because you are off to a huge false start.

From the first moment that i posted this argument, i had Arduino Uno, but now i ordered a Mega, just in case😅, i saw that it has 16 analog inputs and 42 digital outputs, I thoughr it could work easier to me


Here's the schematic that is in my mind, based on tutorials i found around, if can be helpful.

Yes that looks plausible.

But you should plan to scan the buttons, as often as necessary, to see and react to those that got pressed. Not try to use interrupts. Either way will be hard enough, interrupts prolly quite a bit more if a challenge.

And multiplxing the LEDs won't work at all if you want to have more than one on at a time. There you would be better off with the shift register.

Or as @anon57585045 is so far gently suggesting, just get a Arduino board with enough real i/o pins… and use them for both LEDs and buttons.

I like these

https://www.amazon.com/SongHe-ATMEGA2560-16AU-Pinheaders-Compatible-Mega2560/dp/B07TGF9VMQ

shop around, I got three for $30 w/ shipping.

a7

Thanks for your suggestion, but I already bought this ELEGOO Mega R3 Board Microcontrollore con Cavo USB Compatibile con UNOR3 Blu https://amzn.eu/d/bOQPjnb
Will arrive tomorrow.
Anyway, I found out that Arduino Uno has just two pins that work with interrupts, while Mega has 6,but they still aren't enough, what do you think I could do to scan my inputs?
About the leds, I need just the one linked to the button of the actual winner to be on, so it won't be a problem.

Hi,

Wise advice from @alto777. Forget Interrupts.

You have your code scan, or read each button in turn, and set a flag to which ever button the scan finds ON.
Then stop the scan and update the LED display.

In other words your code JUST spends most if not all of its time checking the state of each of the buttons in turn.
This will be fast enough for your purpose and be worth you getting this working first.

So I suggest when you get the hardware, begin by writing code to communicate with the button MUX and read the button states and show them on the IDE serial monitor.
NOTHING ELSE.

Try not to over think the program before you have tried some coding solutions.

Thanks for the diagram.

You will need to now come forward with a schematic of your button MUX circuit, showing exactly how you will connect the buttons, please include gnds and supplies.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

I am looking at that board. can't find in Arduino ide 2.0. want board did you select?

I'm not in the lab so I can prove it, but I am quite sure it was

     Arduino Mega or Mega 2560

That's IDE 1.8.7, where I am stuck perfectly happily. In any case, I certainly did not have to go very far to get it to work.

Also I see above that the board I mentioned has the CH340G USB<->Serial chip, so in addition to selecting the board you might have to download a driver.

Google

        arduino ch340g driver

and take your pick.

HTH

a7

Hi,

Use the IDE 1.8.19, the PC based IDE, the other is still in development.

Tom... :smiley: :+1: :coffee: :australia:

Software | Arduino

Hi,
Try the "Legacy" IDE...

Tom... :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.