Go Down

Topic: GAMESHOW Double Hits please help! (Read 519 times) previous topic - next topic

NZarduino




hey guys so im making  gameshow that has 8 button inputs and 8 led outputs via relays.

a button is pressed and it stops reading all buttons, then it turns all lights off that did not get pressed first.
then it rings makes a buzzer sound (via Relay) for 500ms then off again, then i calls the contestants name
via the mp3 shield.

then it is manually reset via another input button


the issue is when two buttons are pressed at the same time im getting multiple hit scenario.
causes the program to become unstable and do strange things


tips on handling the selection of winning input??


Thanks














#include <SoftwareSerial.h>
#include <MP3.h>
#include <SPI.h>

unsigned long intervalA = 500;
unsigned long intervalB = 1000;
unsigned long startTime;
static bool p1Active;
static bool p2Active;
static bool p3Active;
static bool p4Active;
static bool p5Active;
static bool p6Active;
static bool p7Active;
static bool p8Active;
static bool p1on;
static bool p2on;
static bool p3on;
static bool p4on;
static bool p5on;
static bool p6on;
static bool p7on;
static bool p8on;
static bool reset;
MP3 mp3;

void setup()
{
  pinMode(22,OUTPUT);
  pinMode(23,OUTPUT);
  pinMode(24,OUTPUT);
  pinMode(25,OUTPUT);
  pinMode(26,OUTPUT);
  pinMode(27,OUTPUT);
  pinMode(28,OUTPUT);
  pinMode(29,OUTPUT);
  pinMode(30,OUTPUT);
  pinMode(31,OUTPUT);
  pinMode(32,INPUT);
  pinMode(33,INPUT);
  pinMode(34,INPUT);
  pinMode(35,INPUT);
  pinMode(36,INPUT);
  pinMode(37,INPUT);
  pinMode(38,INPUT);
  pinMode(39,INPUT);
  pinMode(41,INPUT);



  digitalWrite(22,LOW);
  digitalWrite(23,LOW);
  digitalWrite(24,LOW);
  digitalWrite(25,LOW);
  digitalWrite(26,LOW);
  digitalWrite(27,LOW);
  digitalWrite(28,LOW);
  digitalWrite(29,LOW);
  digitalWrite(30,HIGH);
  digitalWrite(31,HIGH);

  p1Active = true;
  p2Active = true;
  p3Active = true;
  p4Active = true;
  p5Active = true;
  p6Active = true;
  p7Active = true;
  p8Active = true;
  reset = true;

  mp3.begin(MP3_SOFTWARE_SERIAL);



  mp3.volume(0x1F);

  mp3.set_mode(MP3::SINGLE) ;
}


void loop()
{
{
  if (p1Active && (digitalRead(32)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(23,HIGH);
    digitalWrite(24,HIGH);
    digitalWrite(25,HIGH);
    digitalWrite(26,HIGH);
    digitalWrite(27,HIGH);
    digitalWrite(28,HIGH);
    digitalWrite(29,HIGH);
    digitalWrite(30,LOW);
    reset = false;
    p1on = true;
    startTime = millis();


  }


  if (p1on && (millis() - startTime == intervalA))
  {
    digitalWrite(30,HIGH);
  }


  if (p1on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0001);

  }

  }
{
  if (p2Active && (digitalRead(33)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(22,HIGH);
    digitalWrite(24,HIGH);
    digitalWrite(25,HIGH);
    digitalWrite(26,HIGH);
    digitalWrite(27,HIGH);
    digitalWrite(28,HIGH);
    digitalWrite(29,HIGH);
    digitalWrite(30,LOW);
    reset = false;
    p2on = true;
    startTime = millis();
  }


  if (p2on && (millis() - startTime == intervalA))
  {
    digitalWrite(30,HIGH);
  }


  if (p2on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0002);


  }
}
{

  if (p3Active && (digitalRead(34)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(23,HIGH);
    digitalWrite(22,HIGH);
    digitalWrite(25,HIGH);
    digitalWrite(26,HIGH);
    digitalWrite(27,HIGH);
    digitalWrite(28,HIGH);
    digitalWrite(29,HIGH);
    digitalWrite(30,LOW);
    reset = false;
    p3on = true;
    startTime = millis();
  }


  if (p3on && (millis() - startTime == intervalA))
  {
    digitalWrite(30,HIGH);
  }


  if (p3on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0003);


  }
}
{

  if (p4Active && ( digitalRead(35)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(23,HIGH);
    digitalWrite(24,HIGH);
    digitalWrite(22,HIGH);
    digitalWrite(26,HIGH);
    digitalWrite(27,HIGH);
    digitalWrite(28,HIGH);
    digitalWrite(29,HIGH);
    digitalWrite(30,LOW);
    reset = false;
    p4on = true;
    startTime = millis();
  }


  if (p4on && (millis() - startTime == intervalA))
  {
    digitalWrite(30,HIGH);
  }


  if (p4on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0004);


  }
}
{

  if (p5Active && (digitalRead(36)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(23,HIGH);
    digitalWrite(24,HIGH);
    digitalWrite(25,HIGH);
    digitalWrite(22,HIGH);
    digitalWrite(27,HIGH);
    digitalWrite(28,HIGH);
    digitalWrite(29,HIGH);
    digitalWrite(31,LOW);
    reset = false;
    p5on = true;
    startTime = millis();
  }


  if (p5on && (millis() - startTime == intervalA))
  {
    digitalWrite(31,HIGH);
  }


  if (p5on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0005);


  }
}
{

  if (p6Active && (digitalRead(37)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(23,HIGH);
    digitalWrite(24,HIGH);
    digitalWrite(25,HIGH);
    digitalWrite(26,HIGH);
    digitalWrite(22,HIGH);
    digitalWrite(28,HIGH);
    digitalWrite(29,HIGH);
    digitalWrite(31,LOW);
    reset = false;
    p6on = true;
    startTime = millis();
  }


  if (p6on && (millis() - startTime == intervalA))
  {
    digitalWrite(31,HIGH);
  } 


  if (p6on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0006);


  }
}
{

  if (p7Active && (digitalRead(38)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(23,HIGH);
    digitalWrite(24,HIGH);
    digitalWrite(25,HIGH);
    digitalWrite(26,HIGH);
    digitalWrite(27,HIGH);
    digitalWrite(22,HIGH);
    digitalWrite(29,HIGH);
    digitalWrite(31,LOW);
    reset = false;
    p7on = true;
    startTime = millis();
  }


  if (p7on && (millis() - startTime == intervalA))
  {
    digitalWrite(31,HIGH);
  }


  if (p7on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0007);


  }

}
{
  if (p8Active && (digitalRead(39)))

  {
   
    p1Active = false;
    p2Active = false;
    p3Active = false;
    p4Active = false;
    p5Active = false;
    p6Active = false;
    p7Active = false;
    p8Active = false;
    delay(100);
    digitalWrite(23,HIGH);
    digitalWrite(24,HIGH);
    digitalWrite(25,HIGH);
    digitalWrite(26,HIGH);
    digitalWrite(27,HIGH);
    digitalWrite(28,HIGH);
    digitalWrite(22,HIGH);
    digitalWrite(31,LOW);
    reset = false;
    p8on = true;
    startTime = millis();
  } 


  if (p8on && (millis() - startTime == intervalA))
  {
    digitalWrite(31,HIGH);

  }


  if (p8on && (millis() - startTime == intervalB))
  {

    mp3.play_usb_disk(0x0008);


  }


  if (digitalRead(41))

  {
   
    digitalWrite(22,LOW);
    digitalWrite(23,LOW);
    digitalWrite(24,LOW); 
    digitalWrite(25,LOW);
    digitalWrite(26,LOW);
    digitalWrite(27,LOW);
    digitalWrite(28,LOW);
    digitalWrite(29,LOW);
    digitalWrite(30,HIGH);
    digitalWrite(31,HIGH);
    p1Active = true;
    p2Active = true;
    p3Active = true;
    p4Active = true;
    p5Active = true;
    p6Active = true;
    p7Active = true;
    p8Active = true;
    p1on = false;
    p2on = false;
    p3on = false;
    p4on = false;
    p5on = false;
    p6on = false;
    p7on = false;
    p8on = false;

  }
}
}

Robin2

#1
Jun 28, 2015, 01:02 pm Last Edit: Jun 28, 2015, 01:03 pm by Robin2
That is a very long piece of code to present without using code tags (the </> button)
Code: [Select]
so it looks like this
see How to use the forum

When you edit it and post it properly I will be easily able to select it and look at it in my text editor.

It would also be a good idea for you to study the use of arrays

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jurs

hey guys so im making  gameshow that has 8 button inputs and 8 led outputs via relays.
Your code is very confusing and not well structured.

If you have 8 button inputs and 8 led outputs, and each of them is to be treated the same way, then you normally will use structured programming, arrays and for-loops.

Here is just a small code snippet to declare arrays of pins for input and output, and a for-loop in the setup() function to set the pins as INPUT and OUTPUT pins.

Code: [Select]

const byte inputPins[]=  {30, 31, 32, 33, 34, 35, 36, 37};
const byte outputPins[]= {22, 23, 24, 25, 26, 27, 28, 29};
#define NUMPINS (sizeof(inputPins))

const byte resetPin=40;
const byte buzzerPin=41;

void setup() {
  for (int i=0;i<NUMPINS;i++)
  {
    pinMode(inputPins[i],INPUT);
    pinMode(outputPins[i],OUTPUT);
  }
  pinMode(resetPin,INPUT);
  pinMode(buzzerPin,OUTPUT);
}

void loop() {
}


Do you get an idea how to handle arrays of equal members in a for-loop?

Go Up