Firstly, I know that I have already posted this. I do not think anyone here is stupid so: The game is: a power source connected to 6 buttons connected to 6 LEDs. you press them all in the randomly generated order, and they blink in the order pressed after completion, then randomize and reset.
I have an array shuffler code 1)Does the array shuffler work 2)How do I configure ports and attach them to values in the array, 3) How do I use an if-then statement to say if all buttons pressed in order, then LEDs blink 3 times? I have a picture but not the actual Arduino, so, upon request, I can post the picture. Thank you. code:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
int questionNumberArray[] = {0,1,2,3,4,5,6};//the array itself
const size_t n = sizeof(questionNumberArray) / //the array used
sizeof(questionNumberArray[0]);//the base of the array
for (size_t i = 0; i < n - 1; i++) //the loop itself
{
size_t j = random(1, n - i);
int t = questionNumberArray[i]; //integer output for increase
questionNumberArray[i] = questionNumberArray[j];//the value definition
questionNumberArray[j] = t;
}
}
The six buttons are connected to digital pins (1, 2, 9, 11, 12, 13)
-
If you've already posted this, why have you posted it again? It won't get answered any quicker.CharlieHanson– CharlieHanson2015年10月19日 03:05:30 +00:00Commented Oct 19, 2015 at 3:05
-
3Possible duplicate of array shuffler if-then and output questionCharlieHanson– CharlieHanson2015年10月19日 03:06:22 +00:00Commented Oct 19, 2015 at 3:06
-
Yes, this version is just much more organized so...I figured people would be more inclined to answer iterrr– errr2015年10月19日 03:21:52 +00:00Commented Oct 19, 2015 at 3:21
-
my arduino's ports are 1, 2, 9, 11, 12, and 13. for me to link to them at the top of the code how would I distinguish between all the different PinB's. also, they would be pins, not ports right, since there needs to be detection of the button being pressed?errr– errr2015年10月19日 04:03:14 +00:00Commented Oct 19, 2015 at 4:03
-
What pins are the 6 buttons connected to?Chris– Chris2015年10月19日 06:05:28 +00:00Commented Oct 19, 2015 at 6:05
1 Answer 1
So you leds are connected to "pins" not "ports." Since each of the buttons will turn on only 1 led we need to keep track of which button pairs with which led. To do this we can use a struct
like this one:
struct ButtonLed{
int buttonPin;
int ledPin;
};
You declare a ButtonLed
like this: ButtonLed button1 = {1, 0}
where the first parameter is the button's pin and the second is the led's pin. (All of the led pins are set to 0 since I don't know which pins you have them connected to.)
And the array will hold the button/led pin pair like this:
ButtonLed questionNumberArray[] = {button1, button2, button3, button4, button5, button6};
Since there are 6 leds you know that your array is size 6 so use this declaration instead
const int n = 6;
I fixed you range for the random number generation. Look at the documentation for random()
here https://www.arduino.cc/en/Reference/Random. We can use this function for shuffling the array.
void shuffleArray (ButtonLed arr [], const int size){
for (int i = 0; i < size - 1; i++){ // iterates through array except last element
int j = random(0, size); // generates a random index from (0 to n-1) inclusive
// swap current index with random index
ButtonLed t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
We will use this function to wait until the user enters a sequence of button presses:
ButtonLed getButtonPress (ButtonLed arr [], const int size){
bool pressed = false;
ButtonLed pressedButton;
while (!pressed){ // loops until a button was pressed
for (int i = 0; i < size && !pressed; i++){ // !pressed allows early out
if (digitalRead(arr[i].buttonPin)){ // one of the six buttons was pressed
pressed = true; // exit both loops
pressedButton = arr[i]; // stores which button was pressed
}
}
}
return pressedButton; // return the pressed button
}
Essentially it gets stuck in a loop and reads each of the 6 buttons until one of them is pressed and then records which one was pressed.
Try using the code below and read the comments the only thing you need to do is change all the zeros in the ButtonLed
declarations to whatever pin the leds are connected to. Also the struct
above needs to go into it's only file called "ButtonLed.h" It must be called that.
#include "ButtonLed.h"
// These are ButtonLeds our newly created data type
// they hold both an led's pin and the pin for the button
// that turns it on
ButtonLed button1 = {1, 10};
ButtonLed button2 = {2, 8};
ButtonLed button3 = {9, 7};
ButtonLed button4 = {11, 6};
ButtonLed button5 = {12, 5};
ButtonLed button6 = {13, 4};
// This function will randomly shuffle an array of ButtonLeds
void shuffleArray (ButtonLed arr [], const int size){
for (int i = 0; i < size - 1; i++){ // iterates through array except last element
int j = random(0, size); // generates a random index from (0 to size-1) inclusive
// swap current index with random index
ButtonLed t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
ButtonLed getButtonPress (ButtonLed arr [], const int size){
bool pressed = false;
ButtonLed pressedButton;
while (!pressed){ // loops until a button was pressed
for (int i = 0; i < size && !pressed; i++){ // !pressed allows early out
if (digitalRead(arr[i].buttonPin)){ // one of the six buttons was pressed
digitalWrite(digitalRead(arr[i].ledPin, HIGH); // turns on the led the user chose
delay(200);
digitalWrite(digitalRead(arr[i].ledPin, LOW); // turns it back off
pressed = true; // exit both loops
pressedButton = arr[i]; // stores which button was pressed
}
}
}
return pressedButton; // return the pressed button
}
void setup() {
randomSeed(analogRead(A0));
// This is how you set up the leds for output
pinMode(button1.ledPin, OUTPUT);
pinMode(button2.ledPin, OUTPUT);
pinMode(button3.ledPin, OUTPUT);
pinMode(button4.ledPin, OUTPUT);
pinMode(button5.ledPin, OUTPUT);
pinMode(button6.ledPin, OUTPUT);
// This is how you set up the buttons for input
pinMode(button1.buttonPin, INPUT);
pinMode(button2.buttonPin, INPUT);
pinMode(button3.buttonPin, INPUT);
pinMode(button4.buttonPin, INPUT);
pinMode(button5.buttonPin, INPUT);
pinMode(button6.buttonPin, INPUT);
}
void loop() {
// This array is of type ButtonLed our newly created data type
ButtonLed questionNumberArray[] = {button1, button2, button3,
button4, button5, button6};
const size_t n = 6; // array's size
shuffleArray(questionNumberArray, n);
numLeds = 1;
bool guessCorrectly = false;
// this while loop allows the user to keep trying until they get it right
while (!guessCorrectly){
// This loop displays the correct sequence to the user
for (int i = 0; i < numLeds; i++){
digitalWrite(questionNumberArray[i].ledPin, HIGH); // turn on the led
delay(500); // decrease this number to flash faster; increase to flash slower
digitalWrite(questionNumberArray[i].ledPin, LOW); // turn off the led
}
bool correctButton = true; // the user has pressed the correct button
ButtonLed userSequence [6]; // stores the sequence the user entered
// lets the user push 6 buttons
for (int i = 0; i < numLeds; i++){
userSequence[i] = getButtonPress(questionNumberArray, numLeds);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < numLeds && correctButton; i++){
if (userSequence[i].buttonPin != questionNumberArray[i].buttonPin)
correctButton = false;
}
if (correctButton){ // user entered the correct sequence
guessCorrectly = true;
// This will turn on the leds in the correct sequence
for (int i = 0; i < numLeds; i++){
digitalWrite(questionNumberArray[i].ledPin, HIGH); // turn on the led
delay(500); // delay half a second
digitalWrite(questionNumberArray[i].ledPin, LOW); // turn off the led
}
numLeds++;
if (numLeds > 6)
--numLeds;
}
}
}
After you are finished typing the code above it ctrl+shift+n
and open a new tab called "ButtonLed.h" and add this to it:
#ifndef BUTTONLED_H
#define BUTTONLED_H
#include <Arduino.h>
// We create new data type called ButtonLed which stores the button's pin
// and the led's pin; this allows us to keep both an led and it's
// corresponding button together without have to manage 2 arrays
struct ButtonLed{
int buttonPin;
int ledPin;
};
#endif
Make sure you open a new tab and not a new project. Also as others have said you should really learn how to program. Be aware this code compiles however, I haven't tested it. I will update the code when you tell me what pins the leds are connected to.
Edit 1
I added a while loop that allows the user to keep trying until they guess correctly.
Edit 2
I also updated the code for which pins the leds are connected to. Remember they're connected to "pins" Not "ports.
-
The arduino looks like this: postimg.org/image/j65d4jd6b/59b97e93errr– errr2015年10月20日 04:33:46 +00:00Commented Oct 20, 2015 at 4:33
-
-
how do i choose an answer, and what are the ports of the buttonLEDs in relation to the picture? They are hitting multiple points, how do I know which to use?errr– errr2015年10月20日 05:35:38 +00:00Commented Oct 20, 2015 at 5:35
-
Ok so the way your arduino is set up right now won't work. This is because none of the leds are connected to the Arduino's pins. I have added a picture of a schematic to my answer. You must replicate the schematic exactly in order for it to work with the code. Also what do you mean by "choose an answer"? Do you mean "how do you guess a sequence?" or "how do you choose a sequence for someone else to guess?"Chris– Chris2015年10月20日 05:53:24 +00:00Commented Oct 20, 2015 at 5:53
-
i mean literally the buttons are in place. to replace the zeros, which number do I put for each?errr– errr2015年10月20日 05:58:17 +00:00Commented Oct 20, 2015 at 5:58
Explore related questions
See similar questions with these tags.