Relatively new to programming so this is my first time experimenting with arrays. Essentially, I'm trying to create a scoreboard where I press one button and it increases the value of one player's score, and then I press a different button to increase another player's score, same with increasing the round number. There's also a button meant to print this scoreboard to the Serial Monitor. Lastly, I have a button that is meant to reset the scores back to zero and the rounds back to one.
In order to accomplish this I want to use two arrays, one with the default numbers and one that simply features a bunch of ones, so that I can add one to any index I choose depending on the corresponding button. The only issue is, I have no clue how to pull this off. Currently, I have a bunch of filler code just to vaguely get the formatting right but I was hoping someone here could help me with the real deal. Thank you all in advance.
(Because I'm a new user it's not allowing me to attach my code so I shall paste it below.)
int reset = 2;
int update = 3;
int gameRound = 4;
int playerTwo = 5;
int playerOne = 6;
int values[3][2] = {0, 1, 0}; {1, 1, 1} ;
void setup() {
for (int i = 2; i < 7; i++) {
pinMode (i, INPUT); }
Serial.begin(9600);
}
void loop() {
delay(200);
if (digitalRead(playerOne) == LOW) {
Serial.print("number");
}
delay(200);
if (digitalRead(playerTwo) == LOW) {
Serial.print("numbertwo");
}
delay(200);
if (digitalRead(gameRound) == LOW) {
Serial.print("numberthree");
}
delay(200);
if (digitalRead(update) == LOW) {
Serial.println("PLAYER1 ROUND PLAYER2");
Serial.print(" ");
Serial.print(values[0][0]);
Serial.print(" ");
Serial.print(values[2][0]);
Serial.print(" ");
Serial.println(values[1][0];
Serial.println();
}
}
Arrays are just variables, except that you access individual cells with an index. There are plenty of good tutorials on line showing you how to use them, so spend some time studying a couple and running the examples. Example search phrase "c++ array tutorial"
This won't compile, and even if fixed, probably does not do what you expect, as the data initialization is not in the customary index order.
int values[3][2] = {0, 1, 0}; {1, 1, 1} ;
Practice with one dimensional arrays before trying 2 or more dimensions.
Post a schematic of your project, even if drawn freehand.
You are testing the button for LOW but apparently they are always on LOW.
Maybe you have to change the pinMode to PULLUP.
(pinMode(i, INPUT_PULLUP);
We will only know how they are if we post the schematic.
this doesn't make so much sense for me.
If I were you, I would think about what kind of data belongs together.
This is a pin and a variable for the counter.
All together you have 3 counters increased by 3 pins
Therefore define a structure with a pin and a counter.
And from that combination an array with 3 elements.
To have the name available add a variable name to your structure.
When you have an array (of the structure), you can use for loops to get rid of duplicated code.
Something like this:
// https://forum.arduino.cc/t/unsure-of-how-to-use-arrays/1085257
// to be deleted 2023-03
// code in forum
const uint8_t resetPin = 2;
struct Counter
{
uint16_t buttonPin; // the button connects the GPIO with LOW,
uint16_t count; // the current count of this counter
char name[16]; // a name for this counter
};
// now create an array of the structure and initialize the variables
Counter counter[] =
{
{6, 0, "Player One"},
{5, 0, "Player Two"},
{4, 0, "Game Round"},
};
// output the current data to Serial
void output() {
for (auto & i : counter) {
Serial.print(i.name); Serial.print(":"); Serial.println(i.count);
}
Serial.println();
}
void setup() {
for (auto & i : counter) {
pinMode (i.buttonPin, INPUT);
}
pinMode(resetPin, INPUT);
Serial.begin(9600);
}
void loop() {
// check all counters if the button is pressed
for (auto & i : counter) {
if (digitalRead(i.buttonPin) == LOW)
{
i.count++;
output();
delay(200); // dirty delay
}
}
// check if reset is pressed
if (digitalRead(resetPin) == LOW)
{
Serial.println(F("RESET"));
for (auto & i : counter) {
i.count = 0;
}
output();
delay(200); // dirty delay
}
}
Adding one more player is ONE SINGLE LINE. Just add the row with pin, 0, "name it"
I figured it out! I watched some tutorials like you recommended and while I'm sure there's a much less complicated way of doing what I did here, I'm still proud of what I made.
int reset = 2;
int display = 3;
int gameRound = 4;
int playerTwo = 5;
int playerOne = 6;
int values[2][3] = {{0, 1, 0}, {1, 0, 1}};
void setup() {
for (int i = 2; i < 7; i++) {
pinMode (i, INPUT); }
Serial.begin(9600);
}
void loop() {
delay(200);
if (digitalRead(playerOne) == LOW) {
values[0][0] = values[0][0] +values[1][0];
}
delay(200);
if (digitalRead(playerTwo) == LOW) {
values[0][2] = values[0][2] +values[1][2];
}
delay(200);
if (digitalRead(gameRound) == LOW) {
values[0][1] = values[0][1] +values[1][0];
}
delay(200);
if (digitalRead(display) == LOW) {
Serial.println("PLAYER1 ROUND PLAYER2");
Serial.print(" ");
Serial.print(values[0][0]);
Serial.print(" ");
Serial.print(values[0][1]);
Serial.print(" ");
Serial.println(values[0][2]);
Serial.println();
}
delay(200);
if (digitalRead(reset) == LOW) {
values[0][0] = values[1][1];
values[0][1] = values[1][0];
values[0][2] = values[1][2];
}
}