Hello!
So recently I started learning about arrays and wanted to make something that involves arrays and randoms. I came up with a little game. I have a DIP switch with 5 pins, a button and a red led. The code will select a random number from 2-6 (for example number 3) and then I have to put the switch on pin 3 to ON and press a button (to confirm my pick). Every time I get it right the red led shouldn't light up, if it does I switched the wrong switch ON and the game resets. Also the numbers shouldn't repeat. But every time I switch any switch ON and press the button, the led lights up, even if I got it right. It worked great before I put the "do not repeat numbers" in. Now I've been stuck on this for a couple of days. Where did I mess up? Thanks in advance for your help adn time .
The code:
long randNumb;
long randomNumber;
int switch6 = 6;
int switch5 = 5;
int switch4 = 4;
int switch3 = 3;
int switch2 = 2;
int button = 8;
int currentButtonValue;
int previousButtonValue;
int myArray[] = {2,3,4,5,6};
int led = 11;
int test = 0;
int number = 0;
int i;
void setup() {
pinMode(button,INPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(led, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
randomNumber = random(1,5);
Serial.println(randomNumber);
Serial.println("==");
}
void loop() {
currentButtonValue = digitalRead(button);
int chosenArray[randomNumber] = {};
if(test == 0){
randNumb = random(0,5);
chosenArray[number] = myArray[randNumb];
Serial.println(chosenArray[number]);
myArray[randNumb] = random(10,500);
number++;
test = 1;
}
if(currentButtonValue != previousButtonValue){
if(currentButtonValue == HIGH and digitalRead(chosenArray[number]) == LOW){
digitalWrite(led, LOW);
randNumb = random(0,5);
if(myArray[randNumb] < 7){
number++;
chosenArray[number] = myArray[randNumb];
Serial.println(chosenArray[number]);
myArray[randNumb] = random(10,500);
}
}
else{
digitalWrite(led, HIGH);
}
}
previousButtonValue = currentButtonValue;
}
I'm have a really hard time figuring out the code vs what it is really supposed to do. If you do select the correct DIP switch then what is supposed to happen?
so your "else" statement actually refers to the first if(currentButtonValue != previousButtonValue){
Meaning that each time your previous value matches the current value your LED will light up regardless of the dip switch or button.
A few other things I noticed are:
previousButtonValue is not initialized the first time the loop runs. While it may get a default value of 0 as it is a global var, it is always good practice to explicitly provide a value for it.
if(myArray[randNumb] < 7) but myArray[randNumb] = random(10,500) so it will never receive a number lower than 7
In general, the code is very messy. To start with, try using meaningful var names for all your variables (ie. test, number, myArray are not good names). Currently it is quite difficult to understand and this would be a good first step to make it slightly clearer.
The randomNumber is telling how many switches should be set to ON. Each time you get one right it is supposed to print another one, and so on until you either fail or get all of them. For example randomNumber is 4, that means it will ask me to guess the correct switch 4 Times. First it will print for example 1, I guess it and it will print for example 5 and so on. If I guess all 4 of them idk I might put a little speaker so it makes a buzz or something do you know you got them all right
lj_sk:
The randomNumber is telling how many switches should be set to ON. Each time you get one right it is supposed to print another one, and so on until you either fail or get all of them. For example randomNumber is 4, that means it will ask me to guess the correct switch 4 Times. First it will print for example 1, I guess it and it will print for example 5 and so on. If I guess all 4 of them idk I might put a little speaker so it makes a buzz or something do you know you got them all right
That seems different from your first description. These 2 sentences to be in conflict:
"The randomNumber is telling how many switches should be set to ON. Each time you get one right it is supposed to print another one, and so on until you either fail or get all of them. For example randomNumber is 4, that means it will ask me to guess the correct switch 4 Times."
So if the random number is 4 do you set 4 switches to ON and then press the button?
No no you are not supposed to immediately turn all 4 switches on, rather one by one. You switch one ON and press the button, if the led doesn't turn on you guessed the right one. In this case that should loop for 4 times.
ohh yeah you are right meirgold the else is the problem. I fixed 2. before but I guess I forgot to save it, didint change anything tho. Yeah I'll make the code prettier I understand its hard to read. Thanks for the help so far guys