Go Down

Topic: HELP Quiz buttons and LED's (Read 239 times) previous topic - next topic

V0idie8808

Help needed,

I m running a arduino uno with a 4 relay break out board, now all i want to do is when one of 4 buttons are pressed the corresponding Led should light up and a buzzer will sound. i have not desided on time delays and the nitty gritties. i just need help with the basic wiring and code then i will progress from there.

any help welcome i need this project done by this weekend

thank you in advance

Delta_G

You want to light up an LED in response to a button press?   There are examples that came with the IDE that do precisely that.  Have you looked at those?   

What exactly is the problem or the question?   What part is confounding you?  Nobody is going to just write the whole thing for you, especially not from such a vague description.  So what do you want us to do?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

V0idie8808

sorry @Delta_G my problem is that i need it in such a way that if one button is pressed the rest is locked out untill reset, i'm not trying to sound lazy i am only now starting to earn the code and how to write from scratch,

i really did not mean to offend anyone.

my appologies

Delta_G

#3
Jun 13, 2018, 11:08 pm Last Edit: Jun 13, 2018, 11:08 pm by Delta_G
Ok. Give it a try.  Start with just being able to light up one led.  Can you do that?   You gotta give me something to work with. 

The best thing might be to forget completely about coding it for now and just write out all the logic in plain English.  If you can't do that then you have no hope of ever writing code for it.  If you can do that then the code will just about write itself. 

This sounds like a state machine (that's a term for you to research) where you have one state where nobody has buzzed in and it reads switches and a second state for after you detect one switch and it does whatever it has to do before going back to the first state for the next question. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

V0idie8808

this is the code i am working from that i basically got from the examples menu,it give you one button and 1 LED i got it to work but now that i want to ad more switches and more leds the new ones wont work


const int buttonPin1 = 1;
const int buttonPin2 = 2;

const int ledPin1 =  6;     
const int ledPin2 =  7;

int buttonState1 = 0;         
int buttonState2 = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
 
  pinMode(buttonPin1, INPUT); 
  pinMode(buttonPin2, INPUT);
}

void loop() {
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);

 
  if (buttonState1 == HIGH) {
     digitalWrite(ledPin1, HIGH);
    delay(2000);
    } else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
   
    if (buttonState2 == HIGH){
    digitalWrite(ledPin2, HIGH);
    delay(2000);
    } else {
    digitalWrite(ledPin2, LOW);}
   
   
    }

V0idie8808

the problem i am having is both buttons turn on the same relay  and the other one randomlywhen pressed

Delta_G

I notice that you aren't using the internal pull-up resistors and you expect the button to read backwards.  Do you have external pull-down resistors or is the input floating when the button isn't pressed?  Normally one would use the internal pull-ups and wire the button to read LOW when pressed. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

PaulRB

Code: [Select]

const int buttonPin1 = 1;

You should not use pin 1 on Uno. It is used for communication with the PC, sketch upload etc. Use pins 2 and up.

Also please read the forum guidelines in the sticky post. They will tell you how to post code correctly so it is not corrupted by the forum.

And please learn to indent your code before you post it. You just have to press ctrl-T.

larryd

#8
Jun 14, 2018, 10:14 pm Last Edit: Jun 14, 2018, 10:15 pm by larryd
Here is an example that you may get some ideas from.
You can adjust the number of buttons as needed.

Code: [Select]
/*jeopardy style game-- master button (masClick) will enable 20+ other buttons (chiClick) to "buzz" in.
Once a player button is pressed, the others are locked out and the player's corresponding LED is lit indicating
which player buzzed in first. The loop resets after 5 seconds and waits for the master to be pressed again.
*/
 
//LarryD
#define pushed LOW   //pushing gives a LOW
//#define pushed HIGH  //pushing gives a HIGH
 
#define LEDon   HIGH
#define LEDoff  LOW
 
const byte masClick    = 53; // iteration should commence once masClick pin 53 goes HIGH
const byte masClickLED = 52; // will illuminate when master button is pressed
 
const byte chiClick[]    = { 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
//                           0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
const byte chiClickLED[] = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45};

const byte pinCount      = 22;
 
const unsigned int onTime         = 5000; //after you have a winner leave LED on for this time
const unsigned int debounceDelay  = 25;   //scans switches every 25 milliseconds
 
unsigned long debounceMillis; //used in timing switch checking
unsigned long FiveSecMillis;  //used in timing after first person has been detected
unsigned long masterMillis;   //used for flashing master LED after cheating is detected
unsigned long delayMillis;    //used after cheating has been acknowledgment by master push
unsigned long winnerMillis;   //used in timing to flash the winners LED
 
byte winnerIndex;          //the array index number of the winner
 
bool FiveSecFlag = false;  //false = not in 5 second timing mode
bool polingFlag  = false;  //flase = not scanning the contestant switches
bool cheatFlag   = true;   //true  = check to see if contestant button is pressed before it should be
bool flashFlag   = false;  //false = flash the master LED
 
//*********************************************************
 
void setup()
{
  pinMode(masClick, INPUT_PULLUP);
  pinMode(masClickLED, OUTPUT);
  digitalWrite(masClickLED, LEDoff);
 
  for (int i = 0; i < pinCount; i++)
  {
    pinMode(chiClick[i], INPUT_PULLUP);
    pinMode(chiClickLED[i], OUTPUT);
    digitalWrite(chiClickLED[i], LEDoff);
  }
 
  //Lamp test, flash LEDs 10 times at reset, an even number here leaves the LEDs off
  for (int x = 0; x < 20; x++)
  {
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite(chiClickLED[i], !digitalRead(chiClickLED[i]));
    }
    digitalWrite(masClickLED, !digitalRead(masClickLED));
    delay(500);
  }
 
} //END of             s e t u p ( )
 
//*********************************************************
 
void loop()
{
 
  //NON-blocking code goes here
 
  //**************************
  //if there is any cheating flash master LED
  if (flashFlag == true)
  {
    if (millis() - masterMillis >= 250)
    {
      masterMillis = millis();
      //toggle master LED
      digitalWrite(masClickLED, !digitalRead(masClickLED));
    }
  }
 
  //**************************
  //are we in the 5 second timeout period?
  if (FiveSecFlag == true && millis() - winnerMillis > 100ul)
  {
     //flash winners LED
     winnerMillis = millis();
     //toggle LED
     digitalWrite(chiClickLED[winnerIndex], !digitalRead(chiClickLED[winnerIndex]));
  }
 
  else if (FiveSecFlag == true && millis() - FiveSecMillis >= onTime)
  {
    //five seconds has gone by, disable timeout
    FiveSecFlag = false;
 
    //turn off contestant LEDs
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite (chiClickLED[i], LEDoff) ;
    }
 
    //turn off master LED
    digitalWrite (masClickLED, LEDoff);
 
    //allow cheat checking
    cheatFlag = true;
  }
 
  //**************************
  //time to scan the switches?
  if (millis() - debounceMillis >= debounceDelay)
  {
    debounceMillis = millis();
    readSwitches();
  }
 
} // END of             l o o p ( )
 
 
//*********************************************************
 
void readSwitches()
{
  //**************************
  //should we check for cheating?
  if (cheatFlag == true)
  {
    //check to see if any number of contestants are holding their switch down
    for (int i = 0; i < pinCount; i++)
    {
      if (digitalRead(chiClick[i]) == pushed)
      {
        //turn on the cheaters LED
        digitalWrite (chiClickLED[i], LEDon);
 
        //notify master that a switch is being pressed too early
        flashFlag = true;
      }
    }
  }
 
  //**************************
  //if there is cheating, has the master acknowledged it
  if (flashFlag == true && digitalRead(masClick) == pushed)
  {
    //Stop master LED flashing
    flashFlag = false;
    //turn off Master LED
    digitalWrite (masClickLED, LEDoff);
    //turn off all contestant LEDs
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite (chiClickLED[i], LEDoff) ;
    }
 
    //must give some time after cheat acknowledgment
    delayMillis = millis();
  }
 
  //**************************
  //Start the game?
  //not in 5 second mode, no cheating, we are not currently poling, 2 seconds has gone by since
  //the last cheat acknowledgment and the master switch is now pressed
  if (FiveSecFlag == false && flashFlag == false && polingFlag == false  && millis() - delayMillis > 2000
      && digitalRead(masClick) == pushed)
  {
    //turn on master LED
    digitalWrite (masClickLED, LEDon);
 
    //allow scanning
    polingFlag = true;
    //disable check checking
    cheatFlag = false;
  }
 
  //**************************
  //should we scan for a winner?
  if (polingFlag == true)
  {
    //scan for the first LOW switch
    for (int i = 0; i < pinCount; i++)
    {
      if (digitalRead(chiClick[i]) == pushed)
      {
        //save the winners array index
        winnerIndex = i;
        winnerMillis = millis();
       
        //turn on the winner LED
        digitalWrite (chiClickLED[i], LEDon);
 
        //disable further scanning
        polingFlag = false;
       
        //Enable the 5 second countdown timer
        FiveSecFlag = true;
        FiveSecMillis = millis();
       
        //we found the winner, stop looking any further
        break;
      }
    }
  }
 
} // END of         r e a d S w i t c h e s ( )
 
//*********************************************************   



.
No technical PMs.
The last thing you did is where you should start looking.

Unsigned_Arduino

 
Help needed,

I m running a arduino uno with a 4 relay break out board, now all i want to do is when one of 4 buttons are pressed the corresponding Led should light up and a buzzer will sound. i have not desided on time delays and the nitty gritties. i just need help with the basic wiring and code then i will progress from there.

any help welcome i need this project done by this weekend

thank you in advance
You could just run the button directly into the relay or even just the LED. Power->button->led->ground.

PaulRB

#10
Jun 16, 2018, 08:52 am Last Edit: Jun 16, 2018, 08:53 am by PaulRB
You could just run the button directly into the relay or even just the LED. Power->button->led->ground.
No, that's no good. This is for a quiz game, so only one led/relay should activate, indicating which player hit their button quickest.

Unsigned_Arduino

  Is this what you want to do?

1. Check for button presses
  1a. If one button is pressed, than lock other buttons
        Then, light LED attached
  2a. If reset button is pressed, than unlock all buttons

Go Up