#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 nsize-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 < n;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 < n;numLeds; i++){
userSequence[i] = getButtonPress(questionNumberArray, nnumLeds);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < nnumLeds && 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 < n;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;
}
}
}
#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 n-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);
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 < n; 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 < n; i++){
userSequence[i] = getButtonPress(questionNumberArray, n);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < n && 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 < n; 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
}
}
}
}
#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;
}
}
}
#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 n-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);
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 < n; 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 < n; i++){
userSequence[i] = getButtonPress(questionNumberArray, n);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < n && 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 < n; 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
}
}
}
}
#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 n-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
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);
bool guessCorrectly = false;
// this while loop allows the user to keep trying until they get it right
while (!guessCorrectly){
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 < n; i++){
userSequence[i] = getButtonPress(questionNumberArray, n);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < n && 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 < n; 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
}
}
}
}
#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 n-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);
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 < n; 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 < n; i++){
userSequence[i] = getButtonPress(questionNumberArray, n);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < n && 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 < n; 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
}
}
}
}
#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, 010};
ButtonLed button2 = {2, 08};
ButtonLed button3 = {9, 07};
ButtonLed button4 = {11, 06};
ButtonLed button5 = {12, 05};
ButtonLed button6 = {13, 04};
// 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 n-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
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);
bool guessCorrectly = false;
// this while loop allows the user to keep trying until they get it right
while (!guessCorrectly){
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 < n; i++){
userSequence[i] = getButtonPress(questionNumberArray, n);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < n && 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 < n; 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
}
}
}
}
Edit 1
Edit 2
I also updated the code for which pins the leds are connected to. Remember they're connected to "pins" Not "ports.
#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, 0};
ButtonLed button2 = {2, 0};
ButtonLed button3 = {9, 0};
ButtonLed button4 = {11, 0};
ButtonLed button5 = {12, 0};
ButtonLed button6 = {13, 0};
// 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 n-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
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);
bool guessCorrectly = false;
// this while loop allows the user to keep trying until they get it right
while (!guessCorrectly){
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 < n; i++){
userSequence[i] = getButtonPress(questionNumberArray, n);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < n && 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 < n; 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
}
}
}
}
Edit
#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 n-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
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);
bool guessCorrectly = false;
// this while loop allows the user to keep trying until they get it right
while (!guessCorrectly){
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 < n; i++){
userSequence[i] = getButtonPress(questionNumberArray, n);
delay(500); // delay between button presses
}
// check if the user entered the correct sequence
for (int i = 0; i < n && 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 < n; 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
}
}
}
}
Edit 1
Edit 2
I also updated the code for which pins the leds are connected to. Remember they're connected to "pins" Not "ports.
lang-cpp