Go Down

Topic: Bullet proof gameshow lockout for 8 players??? (Read 605 times) previous topic - next topic

NZarduino

Aug 28, 2016, 07:23 am Last Edit: Aug 28, 2016, 09:16 am by NZarduino
Hey there.

Im building a lockout style game show with 8 buttons..it reads the winners names amongst other various timed outputs

my code feels strong however the system will still jump around and do bizzare things at times.

my own efforts to resolve this have failed.

can anyone suggest the best way to handle 8 inputs coming in and the first one in locking out the other buttons?


thanks alot

Code: [Select]


#include <SoftwareSerial.h>

#include <MP3.h>
#include <SPI.h>
unsigned long intervalA = 400;
unsigned long intervalB = 600;
unsigned long startTime;
int b1 = 32;
int b2 = 33;
int b3 = 34;
int b4 = 35;
int b5 = 36;
int b6 = 37;
int b7 = 38;
int b8 = 39;

MP3 mp3;


//button status
int bu1 = 0;
int bu2 = 0;
int bu3 = 0;
int bu4 = 0;
int bu5 = 0;
int bu6 = 0;
int bu7 = 0;
int bu8 = 0;

//led outout pins(digital)
int p1 = 22;
int p2 = 23;
int p3 = 24;
int p4 = 25;
int p5 = 26;
int p6 = 27;
int p7 = 28;
int p8 = 29;
int bell = 30;
int buzz = 31;
int left = 42;
int right = 43;

//Status of whether or not a button has been pressed
int set = 0;
int hold = 0;
int time1 = 0;
int time2 = 0;
int time3 = 0;
int time4 = 0;
int time5 = 0;
int time6 = 0;
int time7 = 0;
int time8 = 0;
int reset = 0;

void setup() {                
  Serial.begin(9600);
 mp3.begin (MP3_SOFTWARE_SERIAL);




 mp3.volume(0x1F);

 mp3.set_mode(MP3::SINGLE) ;



 //Button inputs defined
 pinMode(b1, INPUT);
 pinMode(b2, INPUT);
 pinMode(b3, INPUT);
 pinMode(b4, INPUT);
 pinMode(b5, INPUT);
 pinMode(b6, INPUT);
 pinMode(b7, INPUT);
 pinMode(b8, INPUT);
 pinMode(41, INPUT);

 //LED outout Pins defined
 pinMode(p1, OUTPUT);
 pinMode(p2, OUTPUT);
 pinMode(p3, OUTPUT);
 pinMode(p4, OUTPUT);
 pinMode(p5, OUTPUT);
 pinMode(p6, OUTPUT);
 pinMode(p7, OUTPUT);
 pinMode(p8, OUTPUT);
 pinMode(bell, OUTPUT);
 pinMode(buzz, OUTPUT);
 pinMode(left, OUTPUT);
 pinMode(right, OUTPUT);
   digitalWrite(p1, HIGH);
   digitalWrite(p2, HIGH);
   digitalWrite(p3, HIGH);
   digitalWrite(p4, HIGH);
   digitalWrite(p5, HIGH);
   digitalWrite(p6, HIGH);
   digitalWrite(p7, HIGH);
   digitalWrite(p8, HIGH);

 digitalWrite(bell, LOW);
 digitalWrite(buzz, LOW);
 //The delay allows time for the voltage to stabilize (exp. because the electronics are not removed from the buttons!)
 delay(1000);
}

void loop() {

 //Read to see if a button is pushed
 if(hold == 0){
   bu1 = digitalRead(b1);
   bu2 = digitalRead(b2);
   bu3 = digitalRead(b3);
   bu4 = digitalRead(b4);
   bu5 = digitalRead(b5);
   bu6 = digitalRead(b6);
   bu7 = digitalRead(b7);
   bu8 = digitalRead(b8);
  
 }


 //If the status of any of the Buttons has changed to being pulled HIGH
 //lockout the other players and their lights so only the winners light remains on

 //player 1
 if(bu1 == 1){
   hold = 1;
   digitalWrite(p2, LOW);
   digitalWrite(p3, LOW);
   digitalWrite(p4, LOW);
   digitalWrite(p5, LOW);
   digitalWrite(p6, LOW);
   digitalWrite(p7, LOW);
   digitalWrite(p8, LOW);
   digitalWrite(bell, HIGH);
   digitalWrite(left, HIGH);
   bu1 = 0;
   time1 = 1;
   startTime = millis();
   Serial.println("Player 1");
 }


 if(bu2 == 1){
  
   hold = 1;
   digitalWrite(p1, LOW);
   digitalWrite(p3, LOW);
   digitalWrite(p4, LOW);
   digitalWrite(p5, LOW);
   digitalWrite(p6, LOW);
   digitalWrite(p7, LOW);
   digitalWrite(p8, LOW);
   digitalWrite(bell, HIGH);
   digitalWrite(left, HIGH);
   bu2 = 0;
   time2 = 1;  
   startTime = millis();  
   Serial.println("Player 2");  
 }

 if(bu3 == 1){
   hold = 1;
   digitalWrite(p2, LOW);
   digitalWrite(p1, LOW);
   digitalWrite(p4, LOW);
   digitalWrite(p5, LOW);
   digitalWrite(p6, LOW);
   digitalWrite(p7, LOW);
   digitalWrite(p8, LOW);
   digitalWrite(bell,HIGH);
   digitalWrite(left, HIGH);
   bu3 = 0;
   time3 = 1;
   startTime = millis();
   Serial.println("Player 3");
 }
 if(bu4 == 1){
   hold = 1;
   digitalWrite(p2, LOW);
   digitalWrite(p3, LOW);
   digitalWrite(p1, LOW);
   digitalWrite(p5, LOW);
   digitalWrite(p6, LOW);
   digitalWrite(p7, LOW);
   digitalWrite(p8, LOW);
   digitalWrite(bell, HIGH);
   digitalWrite(left, HIGH);
   bu4 = 0;
   time4 = 1;
   startTime = millis();
   Serial.println("Player 4");
 }
 if(bu5 == 1){
   hold = 1;
   digitalWrite(p2, LOW);
   digitalWrite(p3, LOW);
   digitalWrite(p4, LOW);
   digitalWrite(p1, LOW);
   digitalWrite(p6, LOW);
   digitalWrite(p7, LOW);
   digitalWrite(p8, LOW);
   digitalWrite(buzz, HIGH);
   digitalWrite(right, HIGH);
   bu5 = 0;
   time5 = 1;
   startTime = millis();  
   Serial.println("Player 5");
 }
 if(bu6 == 1){
   digitalWrite(p2, LOW);
   digitalWrite(p3, LOW);
   digitalWrite(p4, LOW);
   digitalWrite(p5, LOW);
   digitalWrite(p1, LOW);
   digitalWrite(p7, LOW);
   digitalWrite(p8, LOW);
   digitalWrite(buzz,HIGH);
   digitalWrite(right, HIGH);
   bu6 = 0;
   time6 = 1;
   startTime = millis();
   Serial.println("Player 6");
 }
 if(bu7 == 1){
   hold = 1;
   digitalWrite(p2, LOW);
   digitalWrite(p3, LOW);
   digitalWrite(p4, LOW);
   digitalWrite(p5, LOW);
   digitalWrite(p6, LOW);
   digitalWrite(p1, LOW);
   digitalWrite(p8, LOW);
   digitalWrite(buzz,HIGH);
   digitalWrite(right, HIGH);
   bu7 = 0;
   time7 = 1;
   startTime = millis();
   Serial.println("Player 7");
 }
 if(bu8 == 1){
   hold = 1;
   digitalWrite(p2, LOW);
   digitalWrite(p3, LOW);
   digitalWrite(p4, LOW);
   digitalWrite(p5, LOW);
   digitalWrite(p6, LOW);
   digitalWrite(p7, LOW);
   digitalWrite(p1, LOW);
   digitalWrite(buzz,HIGH);
   digitalWrite(right, HIGH);
   bu8 = 0;
   time8 = 1;
   startTime = millis();
   Serial.println("Player 8");
 }

//these are how im handling the off delay timers for the external bell and buzzer


 if (time1 && (millis() - startTime >= intervalA))
 {
   digitalWrite(bell, LOW);
 }


 if (time1 && (millis() - startTime >= intervalB))
 {
   mp3.play_usb_disk(0x0001);
   time1 = 0;

 }
 if (time2 && (millis() - startTime >= intervalA))
 {
   digitalWrite(bell, LOW);
 }


 if (time2 && (millis() - startTime >= intervalB))
 {
  
   mp3.play_usb_disk(0x0002);
   time2 = 0;

 }
 if (time3 && (millis() - startTime >= intervalA))
 {
   digitalWrite(bell, LOW);
 }


 if (time3 && (millis() - startTime >= intervalB))
 {
  
   mp3.play_usb_disk(0x0003);
   time3 = 0;

 }
 if (time4 && (millis() - startTime >= intervalA))
 {
   digitalWrite(bell, LOW);
 }


 if (time4  && (millis() - startTime >= intervalB))
 {
  
   mp3.play_usb_disk(0x0004);
   time4 = 0;

 }
 if (time5  && (millis() - startTime >= intervalA))
 {
   digitalWrite(buzz, LOW);
 }


 if (time5 && (millis() - startTime >= intervalB))
 {
  
   mp3.play_usb_disk(0x0005);
   time5 = 0;

 }
 if (time6 && (millis() - startTime >= intervalA))
 {
   digitalWrite(buzz, LOW);
 }


 if (time6 && (millis() - startTime >= intervalB))
 {
  
   mp3.play_usb_disk(0x0006);
   time6 = 0;

 }
 if (time7 && (millis() - startTime >= intervalA))
 {
   digitalWrite(buzz, LOW);
 }


 if (time7 && (millis() - startTime >= intervalB))
 {
  
   mp3.play_usb_disk(0x0007);
   time7 = 0;

 }
 if (time8 && (millis() - startTime >= intervalA))
 {
   digitalWrite(buzz, LOW);
 }


 if (time8 && (millis() - startTime >= intervalB))
 {
  
   mp3.play_usb_disk(0x0008);
   time8 = 0;
  
 }
if(digitalRead(41)){
 reset = 1;
}
//this is the reset
 if (reset == 1){
   hold = 0;
   digitalWrite(22,HIGH);
   digitalWrite(23,HIGH);
   digitalWrite(24,HIGH);  
   digitalWrite(25,HIGH);
   digitalWrite(26,HIGH);
   digitalWrite(27,HIGH);
   digitalWrite(28,HIGH);
   digitalWrite(29,HIGH);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);
   digitalWrite(42,LOW);
   digitalWrite(43,LOW);
   time1 = 0;
   time2 = 0;
   time3 = 0;
   time4 = 0;
   time5 = 0;
   time6 = 0;
   time7 = 0;
   time8 = 0;
   reset = 0;


 }




}


Wawa

No code tags, and no arrays?
Both explained to you last year here.
https://forum.arduino.cc/index.php?topic=332640.0
Leo..


NZarduino

haha oh i feel like an idiot...totally forgot about that.

iv tried using arrays but i cant quite get the handle of them.
i know i should be using them however.

any chance you could go into more detail on the older post??


any help would be really appreciated!!


OldSteve

@NZarduino, it's not too late to edit your opening post and add those [code]code tags[/code] that Wawa mentioned. Your code is painful to read, the way it is.
(It also makes it easier to copy and paste into an IDE for verification.)
Often, too, the code will be corrupted by italics/smilies when posted inline.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

OldSteve

#4
Aug 28, 2016, 08:38 am Last Edit: Aug 28, 2016, 08:45 am by OldSteve
Pin allocations in an array:-
Code: [Select]
const byte numButtons = 8;
const byte buttonPins[numButtons] = {32, 33, 34, 35, 36, 37, 38, 39};

Setting them as inputs:-
Code: [Select]
for(int i = 0; i<numButtons; i++)
    pinMode(buttonPins[i], INPUT);

LED pin allocations in an array:-
Code: [Select]
const byte numLEDs = 8;
const byte ledPins[numLEDs] = {22,23,24,25,26,27,28,29};

Setting them as outputs:-
Code: [Select]
for(int i = 0; i<numLEDs; i++)
    pinMode(ledPins[i], OUTPUT);

Setting them all high:-
Code: [Select]
for(int i = 0; i<numLEDs; i++)
    digitalWrite(ledPins[i], HIGH);

From that, you should be able to figure out the rest.

Edit: If you fix that first, and make things a little easier to read, it will be simpler to help with your problem. (And by then, you might spot it yourself. :) )

Edit2: And you can read the buttons the same way, in a 'for' loop.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

TomGeorge

Hi,
It looks like you are looking for a LOW input. 
How have you got your buttons wired, there are no software pull_up declared, so do you have external pullups?

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

NZarduino

#6
Aug 28, 2016, 08:51 am Last Edit: Aug 28, 2016, 08:56 am by NZarduino
sorry about the noob code tag fail haha.

implementing an array so that once a button has been pressed i can actually do something with the first pin that was struck is were i get stuck..



Edit. nope looking for a HIGH state as far as i can tell.. yep i have external pulldowns and hardware debouncing as that was my first concern when it showed issues..helped slightly lol


NZarduino

Note: i edited my comments in the code to make a little more sense!!

Sorry!

NZarduino

#8
Aug 28, 2016, 10:28 am Last Edit: Aug 28, 2016, 10:34 am by NZarduino
i think iv set it up right, all the leds come on when it starts up, but how do i go about getting
it too execute say.. if the first button in the array becomes true that it will turn off all others and
also start my (bu1 = 1) command...??

im sorry guys im just in a pickle at the moment!!


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

#include <MP3.h>
#include <SPI.h>
unsigned long intervalA = 400;
unsigned long intervalB = 600;
unsigned long startTime;
const byte numButtons = 8;
const byte buttonPins[numButtons] = {32, 33, 34, 35, 36, 37, 38, 39};
const byte numLEDs = 8;
const byte ledPins[numLEDs] = {22,23,24,25,26,27,28,29};
   


MP3 mp3;


//button status
int bu1 = 0;
int bu2 = 0;
int bu3 = 0;
int bu4 = 0;
int bu5 = 0;
int bu6 = 0;
int bu7 = 0;
int bu8 = 0;

//led outout pins(digital)
int p1 = 22;
int p2 = 23;
int p3 = 24;
int p4 = 25;
int p5 = 26;
int p6 = 27;
int p7 = 28;
int p8 = 29;
int bell = 30;
int buzz = 31;
int left = 42;
int right = 43;

//Status of whether or not a button has been pressed
int set = 0;
int hold = 0;
int time1 = 0;
int time2 = 0;
int time3 = 0;
int time4 = 0;
int time5 = 0;
int time6 = 0;
int time7 = 0;
int time8 = 0;
int reset = 0;

void setup() {               
   Serial.begin(9600);
  mp3.begin (MP3_SOFTWARE_SERIAL);
 



  mp3.volume(0x1F);

  mp3.set_mode(MP3::SINGLE) ;



  //Button inputs defined
 for(int i = 0; i<numButtons; i++)
    pinMode(buttonPins[i], INPUT);

for(int i = 0; i<numLEDs; i++)
    pinMode(ledPins[i], OUTPUT);

  pinMode(41, INPUT);

  //LED outout Pins defined
 
  pinMode(bell, OUTPUT);
  pinMode(buzz, OUTPUT);
  pinMode(left, OUTPUT);
  pinMode(right, OUTPUT);
    for(int i = 0; i<numLEDs; i++)
    digitalWrite(ledPins[i], HIGH);

  digitalWrite(bell, LOW);
  digitalWrite(buzz, LOW);
  //The delay allows time for the voltage to stabilize (exp. because the electronics are not removed from the buttons!)
  delay(1000);
}

void loop() {
{
 
 // i think iv set it up right, all the leds come on when it starts up, but how do i go about getting
 // it too execute say.. if the first button in the array becomes true that it will turn off all others and
 // also start my (bu1 = 1) command...??

   
  }



  //player 1
  if(bu1 == 1){
    hold = 1;
   
    digitalWrite(bell, HIGH);
    digitalWrite(left, HIGH);
    bu1 = 0;
    time1 = 1;
    startTime = millis();
    Serial.println("Player 1");
  }

OldSteve

#9
Aug 28, 2016, 12:47 pm Last Edit: Aug 28, 2016, 01:15 pm by OldSteve
i think iv set it up right, all the leds come on when it starts up, but how do i go about getting
it too execute say.. if the first button in the array becomes true that it will turn off all others and
also start my (bu1 = 1) command...??
Well, the first thing would be to actually read the buttons.


Quote
if the first button in the array becomes true that it will turn off all others
So you only want the first button in the array to disable any action being taken if other buttons are pressed? You don't want it to happen for any button that's pressed?
You need to describe things more clearly.
(And you can't "turn off" a button. It's an input.)

What's the purpose of the "timen" variables?

What is the "hold" variable for?

What are "bell", "buz"? Bell on pin 30, buzzer on pin 31?

What are "left and "right"?

What are the "set" and "reset" variables for? (I assume "reset" might have something to do with re-enabling reading of the buttons after all but one are locked out.)

Why do you record 'millis()' then do nothing with it?

What's the purpose of pin 41? You make it an input, then never touch it again.

I'm afraid that you haven't described you desired operation anywhere near well enough, and your code doesn't help.

I'd suggest that you start again, with a little more planning.

Basically, regarding locking out reading buttons once one is pressed, you could lock out reading all buttons, after one is pressed. (You don't need to read the pressed button again either.)


You could maybe do something like this:-
(Just a quickly-written, untested snippet as food for thought, so there might be mistakes.)
Code: [Select]
bool buttonPressed = false;
byte pressedButton;

for (int i = 0; i < numButtons; i++)
{
    if (digitalRead(buttonPins[i]) == HIGH)
    {
        buttonPressed = true;               // Flag the event.
        pressedButton = i;                  // Record which button was pressed, (0 to 7).
        break;                              // Break out of 'for' loop since a button was pressed.
    }
}

// Take action here, depending on which button was pressed, maybe using a 'switch'/'case' statement.
// When done, set "buttonPressed" to 'false'
if (buttonPressed)
{
    switch (pressedButton)
    {
        case 0:

            break;

        case 1:

            break;

        // etc etc

        case 7:

            break;

        default:
            ;
    }
    buttonPressed = false;                      // Clear the flag.
}

N.B. If needed, you could wrap the whole thing in a conditional based on "buttonPressed", so that nothing happens after a button press, including button reading, until you explicitly clear the "buttonPressed" flag, but I wasn't real clear on just what you really want to achieve, so removed that for now.

In that case, it would go something like:-
Code: [Select]
if(!buttonPressed)
{
    // All of the above code in here, except clearing the "buttonPressed" flag, then you'd clear
    // it elsewhere to re-enable button reading.
}
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Go Up