What i am trying to accomplish is to make an simon sais game. The problem is that when i press the button i get no response if it's correct or incorrect.
const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;
const int p_LEDs[] = {2, 3, 4, 5};
long OnTime = 50;
long OffTime = 150;
const int maxSequenceLength = 10;
int sequenceLength = 1;
bool buttonstate1 = LOW;
bool buttonstate2 = LOW;
bool buttonstate3 = LOW;
bool buttonstate4 = LOW;
int sequence[maxSequenceLength];
int Points = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
for (int i = 0; i < 4; i++) {
pinMode (p_LEDs [i], OUTPUT);
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(b3, INPUT);
pinMode(b4, INPUT);
}
}
void loop () {
int B_1 = 0;
int B2 = 0;
int B3 = 0;
int B4 = 0;
buttonstate1 = digitalRead(b1);
buttonstate2 = digitalRead(b2);
buttonstate3 = digitalRead(b3);
buttonstate4 = digitalRead(b4);
for (int i = 0; i < maxSequenceLength; i++) {
sequence[i] = random(0, 4); // Random sequence maken met maar 4 lampjes.
int userArray[sequenceLength];
int z ;
int Y;
int n;
for (int x = 0; x < sequenceLength; x ++) {
digitalWrite(p_LEDs[ sequence[i] ], HIGH);
delay(500);
digitalWrite(p_LEDs[ sequence[i] ], LOW);
delay(200);
}
while ( z < sequenceLength) {
for ( z = 0; z < sequenceLength;) {
if (buttonstate1 == HIGH) {
B_1 == 0;
userArray[z] = B_1;
z++;
}
if (buttonstate2 == HIGH) {
B2 == 1 ;
userArray[z] = B2;
z++;
}
if (buttonstate3 == HIGH) {
B3 == 2 ;
userArray[z] = B3;
z++;
}
if (buttonstate4 == HIGH) {
B4 == 3 ;
userArray[z] = B4;
z++;
}
}
}
if (z == sequenceLength ) {
break;
}
for (int n = 0; n < sequenceLength ; n++)
{
if (sequence[sequenceLength] == userArray[sequenceLength] )
{
Goed();
sequenceLength ++;
}
else {
sequenceLength = 1;
Fout();
}
}
delay(5000);
}
}
void Goed() {
digitalWrite (p_LEDs [0, 1, 2, 3], HIGH);
delay (500);
digitalWrite (p_LEDs [0, 1, 2, 3], LOW);
delay (1000);
Points++;
lcd.begin(16, 2);
lcd.home ();
lcd.print("Correct!");
lcd.setCursor ( 0, 1 );
lcd.print(Points);
lcd.print (" Punten");
delay(1000);
lcd.clear();
}
void Fout() {
lcd.begin(16, 2);
lcd.home ();
lcd.print("Game Over!");
delay(1000);
lcd.clear();
}
Do the pushbuttons have 4 pins, like the one in this picture?
The pins are already connected together as two pairs.
It is easy to select the wrong pins to connect to, if you are not aware of how they are already connected. You end up with a button which appears to be permanently pressed.
If you choose two diagonally opposite pins, it should be right every time.
JohnLincoln:
Do the pushbuttons have 4 pins, like the one in the picture?
The pins are already connected together as two pairs.
It is easy to select the wrong pins to connect to, if you are not aware of how they are already connected. You end up with a button which appears to be permanently pressed.
If you choose two diagonally opposite pins, it should be right every time.
If you changed the (normally open?) switch wiring as described by Grumpy Mike, the switch inputs will read low when when the switch is closed (pressed).
from inside the loop function? If not that creates a new variable also called z but with some random value in it, and the global variable z dosn't get a look in.
Also you do need to zero the variable z before every while loop, otherwise it is left at the value from the previous time round the loop.
from inside the loop function? If not that creates a new variable also called z but with some random value in it, and the global variable z dosn't get a look in.
Also you do need to zero the variable z before every while loop, otherwise it is left at the value from the previous time round the loop.
const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;
const int p_LEDs[] = {2, 3, 4, 5};
long OnTime = 50;
long OffTime = 150;
const int maxSequenceLength = 10;
int sequenceLength = 1;
int sequence[maxSequenceLength];
int z = 0 ;
int Points = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
Serial.begin(9600);
for (int i = 0; i < 4; i++) {
pinMode (p_LEDs [i], OUTPUT);
}
pinMode(b1,INPUT_PULLUP);
pinMode(b2,INPUT_PULLUP);
pinMode(b3,INPUT_PULLUP);
pinMode(b4,INPUT_PULLUP);
}
void loop () {
int B_1 = 0;
int B2 = 0;
int B3 = 0;
int B4 = 0;
for (int i = 0; i < maxSequenceLength; i++) {
sequence[i] = random(0, 4);
Serial.print (digitalRead (b1));
int userArray[sequenceLength];
for (int x = 0; x < sequenceLength; x ++) {
digitalWrite(p_LEDs[ sequence[i] ], HIGH);
delay(500);
digitalWrite(p_LEDs[ sequence[i] ], LOW);
delay(200);
}
while (z < sequenceLength) {
if (digitalRead(b1) == LOW) {
B_1 = 3;
userArray[z] = B_1;
z++;
}
if (digitalRead(b2) == LOW) {
B2 = 2 ;
userArray[z] = B2;
z++;
}
if (digitalRead(b3) == LOW) {
B3 = 1 ;
userArray[z] = B3;
z++;
}
if (digitalRead(b4) == LOW) {
B4 = 0 ;
userArray[z] = B4;
z++;
}
if (z == sequenceLength) {
break;
z = 0;
}
}
for (int n = 0; n < sequenceLength ; n++)
{ Serial.print (userArray[sequenceLength]);
if (sequence[sequenceLength] == userArray[sequenceLength] )
{
Goed();
sequenceLength ++;
}
else {
sequenceLength = 1;
Fout();
}
}
delay(5000);
}
}
void Goed() {
digitalWrite (p_LEDs [0, 1, 2, 3], HIGH);
delay (500);
digitalWrite (p_LEDs [0, 1, 2, 3], LOW);
delay (1000);
Points++;
lcd.begin(16, 2);
lcd.home ();
lcd.print("Correct!");
lcd.setCursor ( 0, 1 );
lcd.print(Points);
lcd.print (" Punten");
delay(1000);
lcd.clear();
}
void Fout() {
digitalWrite (p_LEDs [2], HIGH);
delay (500);
digitalWrite (p_LEDs [2] , LOW);
delay (1000);
lcd.begin(16, 2);
lcd.home ();
lcd.print("Game Over!");
delay(1000);
lcd.clear();
}