Go Down

Topic: Need some help  (Read 300 times) previous topic - next topic

Kaneki1989

Hi! I´m new at arduino coding so i´m having some trouble! I want to make a quiz game with only 3 giant pressure buttons . The problem i´m having is when i press the button the led turns on , but i want that when i press the button , the led stays on until i press the "reset" button...
I just have this code:
Code: [Select]
int led = 8;
int botao = 7;

void setup()
{
  pinMode(led, OUTPUT);
  pinMode(botao, INPUT);
}

void loop()
{
 
 if (digitalRead(botao) == HIGH)
 {
 digitalWrite(led, LOW);
 }

  if (digitalRead(botao) == LOW)
  {
    digitalWrite(led, LOW);
   
  }
  else
  {
    digitalWrite(led, HIGH);
   
  }
}


where it says botao is button.
Thank you.

slipstick

Your code says if the button is High switch the LED off and if the button is Low switch the LED off or if not switch the LED on. Does that make sense to you?

And you only have one button not 3 so there's no reset button. You need to make a little bit more effort than that.

Steve

Kaneki1989

slipstick thank you for answering me. So in this part of the code
Code: [Select]
if (digitalRead(botao) == LOW)
  {
    digitalWrite(led, LOW);
   
  }
  else
  {
    digitalWrite(led, HIGH);

i can change to this ?
Code: [Select]
if (digitalRead(botao) == LOW)
  {
    digitalWrite(led, HIGH);


And yes i only have one button because first i want to test only one and then is more easy to finish the entire code.
But to make the led always on when the button is pressed what i need to make?
Thank you.

Wawa

I want to make a quiz game with only 3 giant pressure buttons.
So three buttons for the players, and the reset button of the Arduino to start over.
Maybe this (untested) sketch will work for you.
Leo..
Code: [Select]
const byte led1 = 8; // LED 1 (+ CL resistor) between pin8 and ground
const byte led2 = 9;
const byte led3 = 10;
const byte botao1 = 5; // button 1 connected to pin 5 and ground
const byte botao2 = 6;
const byte botao3 = 7;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(botao1, INPUT_PULLUP); // use the internal pull up resistor
  pinMode(botao2, INPUT_PULLUP); // so we don't need an external resistor
  pinMode(botao3, INPUT_PULLUP);
}

void loop(){
 if (digitalRead(botao1) == LOW) { // if button 1 is pressed
  digitalWrite(led1, HIGH); // LED 1 on
  while(1); // wait here forever
 }
 if (digitalRead(botao2) == LOW) { // button 2
  digitalWrite(led2, HIGH);
  while(1);
 }
 if (digitalRead(botao3) == LOW) { // button 3
  digitalWrite(led3, HIGH);
  while(1);
 }  
}

Kaneki1989

#4
Feb 21, 2018, 03:55 pm Last Edit: Feb 21, 2018, 04:58 pm by Kaneki1989
Thank you Wawa for offering me that code.. I resetted the arduino and then i send the code that you gave me ... it worked but instead of the led turn on just when i press it stayed on even when i didnt press.. it just stayed on...
I´ll attach a image of my circuit.
Can you help me modifying the code you gived me?

Wawa

#5
Feb 21, 2018, 08:42 pm Last Edit: Feb 21, 2018, 08:45 pm by Wawa
const byte botao1 = 5; // button 1 connected to pin 5 and ground

Didn't say anything about using a resistor, or 5volt for the button.
The code I wrote uses the internal (inside the chip) pull up resistors.
Remove the 10k resistor, and connect the button(s) between pin and ground.
Leo..

Go Up