Hello,
I want to make some sort of quiz on the arduino but I have no idea how to pause it and wait until one of the 3 buttons have been pressed.
How I had it in mind:
Serial.write("Question");
if (pin1 == 1) { //Pin1 is the good answer
Score++;
Serial.write(Score);
}
else {
Score--;
Serial.write(Score);
}
//Next question follows)
I also want a button to reset the score, but that is easy, if it is pressed, score = 0.
Serial.print(F("Question text"));
while (digitalRead(input1) == HIGH && digitalRead(input22) == HIGH && digitalRead(input3) == HIGH)
{
//do nothing
}
if (digitalRead(input1) == LOW)
{
score++;
}
else
{
score--;
}
You can use this function:
byte scan_buttons( ) {
/* active low button press assumed */
byte bounce = 1;
byte button = 0;
if( digitalRead( button1 ) == 0 ) // read the pin
button = button1; // save the pin number
else if( digitalRead( button2 ) == 0 )
button = button2;
else if( digitalRead( button3 ) == 0 )
button = button3;
if( !button ) // no button pressed
return 0;
while( bounce ) { // debounce button release
if( digitalRead( button ) == 1 ) {
delay( 30 );
if( digitalRead( button ) == 1 )
bounce = 0;
}
}
/* return the pin number of pressed button or,
* normalize pin number to 1-3 if it makes things easier.
*/
return button;
}
and then call it with:
while( (button = scan_buttons( )) == 0 ) // stick here until button pressed.
;
then process your "button" variable.
(change the type "byte" to "BYTE" for Arduino)
boolrules:
You can use this function:
byte scan_buttons( ) {
/* active low button press assumed */
byte bounce = 1;
byte button = 0;
if( digitalRead( button1 ) == 0 ) // read the pin
button = button1; // save the pin number
else if( digitalRead( button2 ) == 0 )
button = button2;
else if( digitalRead( button3 ) == 0 )
button = button3;
if( !button ) // no button pressed
return 0;
while( bounce ) { // debounce button release
if( digitalRead( button ) == 1 ) {
delay( 30 );
if( digitalRead( button ) == 1 )
bounce = 0;
}
}
/* return the pin number of pressed button or,
* normalize pin number to 1-3 if it makes things easier.
*/
return button;
}
and then call it with:
while( (button = scan_buttons( )) == 0 ) // stick here until button pressed.
;
then process your "button" variable.
(change the type "byte" to "BYTE" for Arduino)
Okay, I tried it, somehow I can't get it working correctly:
#include <avr/sleep.h>
#define button1 2
#define button2 3
#define button3 4
#define reset 5
int button;
int banana = 0;
byte scan_buttons() {
/* active low button press assumed */
byte bounce = 1;
byte button = 0;
if ( digitalRead( button1 ) == 0 ) // read the pin
button = button1; // save the pin number
else if ( digitalRead( button2 ) == 0 )
button = button2;
else if ( digitalRead( button3 ) == 0 )
button = button3;
if ( !button ) // no button pressed
return 0;
while ( bounce ) { // debounce button release
if ( digitalRead( button ) == 1 ) {
delay( 30 );
if ( digitalRead( button ) == 1 )
bounce = 0;
}
}
/* return the pin number of pressed button or,
normalize pin number to 1-3 if it makes things easier.
*/
return button;
}
void questions() {
Serial.println("Question text");
while ((button = scan_buttons()) == 0 ); // stick here until button pressed.
if (button == 1) {
banana++;
Serial.print("Bananas: ");
Serial.println(banana);
Serial.print("Button: ");
Serial.println(button);
}
else {
banana--;
Serial.print("Bananas: ");
Serial.println(banana);
Serial.print("Button: ");
Serial.println(button);
}
}
void setup() {
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(reset, INPUT);
}
void loop() {
//Serial.write("clearscreen\n");
questions();
}
I think I'm doing something incredible stupid at "if (button == 1)"
And "byte" should stay in lower case letters, in capital letters it doesn't change color in the code and doesn't compile
I think I'm doing something incredible stupid at "if (button == 1)"
#define button1 2
#define button2 3
#define button3 4
button = button1;
How will button ever equal 1 ?
UKHeliBob:
#define button1 2
#define button2 3
#define button3 4
button = button1;
How will button ever equal 1 ?
Well, I assign those buttons to a pin, I meant to do 2 in the code, I fixed it by doing this:
if ( (int)button == 2 )
But now when I want to press again (as it loops, it shows the question again), I can not "answer", when I press the button it does nothing
Okay, somehow it now works with this:
if ( (int)button == 2 )
or
if ( button == 2 )
or
if ( (button == button1 )
But it still doesn't get through the 2nd question
But now when I want to press again (as it loops, it shows the question again), I can not "answer", when I press the button it does nothing
So you can't get variable banana to change? What does it print after a correct answer and after a wrong answer?
boolrules:
So you can't get variable banana to change? What does it print after a correct answer and after a wrong answer?
Correct answer is +1. Starting with 0 bananas.
Wrong answer is -1 banana (each answer good or wrong is +1 or -1)
When I want to answer question 2 (copy pasted first question) it doesn't "answer", it doesn't print the pressed button nor the bananas I currently have. Only thing it posts is the question (after the question I have the "pause" line)
I really don't see any question2 in your code.
boolrules:
I really don't see any question2 in your code.
#define button1 2
#define button2 3
#define button3 4
#define reset 5
int button;
char banana = 0;
byte scan_buttons() {
/* active low button press assumed */
byte bounce = 1;
byte button = 0;
if ( digitalRead( button1 ) == 0 ) // read the pin
button = button1; // save the pin number
else if ( digitalRead( button2 ) == 0 )
button = button2;
else if ( digitalRead( button3 ) == 0 )
button = button3;
if ( !button ) // no button pressed
return 0;
while ( bounce ) { // debounce button release
if ( digitalRead( button ) == 1 ) {
delay( 30 );
if ( digitalRead( button ) == 1 )
bounce = 0;
}
}
/* return the pin number of pressed button or,
normalize pin number to 1-3 if it makes things easier.
*/
return button;
}
void questions() {
Serial.println("Question 1");
while ( ( button = scan_buttons() ) == 0 ); // stick here until button pressed.
if ( button == 2 ) {
banana++;
Serial.print("Bananas: ");
Serial.println((int)banana);
Serial.print("Button pressed: ");
Serial.println((int)button);
}
else {
banana--;
Serial.print("Bananas: ");
Serial.println((int)banana);
Serial.print("Button: ");
Serial.println((int)button);
}
Serial.println("Question 2");
while ( ( button = scan_buttons() ) == 0 ); // stick here until button pressed.
if ( (int)button == 2 ) {
banana++;
Serial.print("Bananas: ");
Serial.println((int)banana);
Serial.print("Button pressed: ");
Serial.println((int)button);
}
else {
banana--;
Serial.print("Bananas: ");
Serial.println((int)banana);
Serial.print("Button: ");
Serial.println((int)button);
}
}
void setup() {
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(reset, INPUT);
}
void loop() {
//Serial.write("clearscreen\n");
questions();
}
Here it is
So you're saying that "Question 2" is never printed?
boolrules:
So you're saying that "Question 2" is never printed?
Question 2 is printed but when I press a button, nothing happens, only at question 1.
If I remove question 2, it should go back to question 1 as it loops, even then nothing happens on a button press. The question is printed though
Let's go back to this response:
Correct answer is +1. Starting with 0 bananas.
Wrong answer is -1 banana (each answer good or wrong is +1 or -1)
I can tell this is how it's supposed to work. The question was: "what prints?"
And you should use your #defines in all places:
if ( (int)button == 2 ) { // replace "2" with "button1"
// the cast to int does nothing
But I don't know. SOMETHING has to print between prints of "Question2" and "Question 1".
What happens (what is printed) if you just press button1, slowly, about 6 times in a row?
Just struck me: How are your buttons wired? Have you done:
pinMode( pin, INPUT_PULLUP );
boolrules:
Just struck me: How are your buttons wired? Have you done:
pinMode( pin, INPUT_PULLUP );
I have it as INPUT, I used pullup because it had some problems, but it seems fine now.
I have it wired from 5V to the button, from the button to the pin.
When the button is pressed it should print the current score and the pushed button (as pin number)
I have it wired from 5V to the button, from the button to the pin.
That will never change state reliably, but if it did, it requires that all of the level sensing in scan_buttons( ) be reversed.
There is a clue in the comment:
byte scan_buttons( ) {
/* active low button press assumed */
You have to connect one side of the buttons to a pin, the other side to GND, and execute
pinMode( pin, INPUT_PULLUP )
in setup( ) for each button. I should have seen this right off the bat. 
If it still does not work, you can go ahead a feel free to answer post #12.
boolrules:
That will never change state reliably, but if it did, it requires that all of the level sensing in scan_buttons( ) be reversed.
There is a clue in the comment:
byte scan_buttons( ) {
/* active low button press assumed */
You have to connect one side of the buttons to a pin, the other side to GND, and execute
pinMode( pin, INPUT_PULLUP )
in setup( ) for each button. I should have seen this right off the bat. :blush:
If it still does not work, you can go ahead a feel free to answer post #12.
It works, thanks!