Having even more issues, for a short while i thought i had cracked it. all my code makes sense and im pretty sure its right.
But obviously not. Its still in test mode so the code isnt absolutely final with the if statements near the bottom, but thats not the issue.
The issue is when the right button on my code entry is pressed, the red light (ledOne) doesnt turn off. Anybody know whats wrong?
// Project Awesome. Code Entry With Four Buttons
#define BTNCNT 9
#define ATTCNT 2
int a = 0; //set a
int y = 0; // Set y
int x = 0; // Set x
int ledOne = 13; //Set Red LED
int ledTwo = 12; //Set Green LED
byte buttons[BTNCNT] = {2,3,4,5,6,7,8,9,10}; //Set buttons to array
byte pass[ATTCNT] = {2,3}; // Set password
byte attempt[ATTCNT]; // Set attempts array
byte state[ATTCNT]; // Make states array
byte oldstate[ATTCNT]; // When button is hit, info added here
int time = 0;
void setup(){
pinMode(ledOne, OUTPUT);
pinMode(ledTwo, OUTPUT);
for(x=0; x<BTNCNT; x++){
pinMode(buttons[x], INPUT);
}
digitalWrite(ledOne, HIGH);
}
void loop(){
for(x=0; x<BTNCNT; x++){
state[x] = digitalRead(buttons[x]); // Check states of each button
}
if(attempt[0] != pass[0]){
TimeOut();
}
for(x=0; x<BTNCNT; x++){
if(oldstate[x] != state[x]){ //If these new states are different then the old ones
if(state[x] == HIGH)
{
attempt[y] = buttons[x]; //Set attempt (Just first index for now) to buttons pressed
y++;
time = millis();
}
}
}
for(x=0; x<2; x++){
if(attempt[0] == pass[0]){
Correct();
}
}
if ((millis() - time) > 10000){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
y=0;
}
}
if(y > 3){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
}
y=0;
}
for(x=0;x<BTNCNT;x++){
oldstate[x] = state[x];
}
}
void Correct(){
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
}
void TimeOut(){
digitalWrite(ledTwo, LOW);
digitalWrite(ledOne, HIGH);
}
BTNCNT should be a constant that is derived from sizeof(buttons) and should get a meaningful name
ATTCNT should be a constant that is derived from sizeof(pass) and should get a meaningful name
you use int to declare LED pins
you call the LEDs ledOne and ledTwo, that is the variable names give no clue about the meaning of the leds
Correct() and TimeOut() are basically the inverses of each others
6a) they should have better names like Correct() and Incorrect()
6b) they should be combined into one function e.g. void IndicateInputState(boolean correct)
setup does not initialize ledTwo
you have global variables a,x,y with unclear semantics, variables (and especially global variables) need reasonable names
you comment on the global variables that you set them, this is redundant repetition of the code
I don't care for your corrections. They're not relevant to my question. Id prefer if you had looked at it and tried to see what was wrong, and if you couldn't to move on and actually help someone else rather then tearing apart a person (who has been coding for just over a weeks) code.
Thanks for the reply none the less, of anyone else can help please do post.
You still did not describe what this code is supposed todo. How do you expect a fix if you just tell you think your code is good but it does not work. You say
The issue is when the right button on my code entry is pressed
But you give absolutey no hints what "pressing the right button" actually is.
Step 0: Calm down
Step 1: Accept the fact that nobody here but you knows what this code is supposed to do. Describe what the code is expected to do and how it fails to achieve this.
Step 2: Fix the obvious stuff first, ignoring good practice is not helpful if you want to fix your code
and if you couldn't to move on and actually help someone else rather then tearing apart a person (who has been coding for just over a weeks) code.
I think you are confusing criticizing your code for criticizing you personally. They are not the same thing, but if you remain defencive about your code you would probably not benefit with any advice given? Peer review and constructive criticism is one of the most effective learning methods there is for programming. But you must be mature enough to understand that process for it to be effective.
I do realise he wasn't criticizing me,
Ill explain what the code is meant to do:
Basically the idea is its a security pad (sort of).
When the buttons required are pressed, Correct() is run.
I realise also that some of the code is over complicated, but that isnt relevant at the moment, that i can fix and refine later, aswell as the repeated commands, which are there for a reason in most cases.
The only issue im hsving is trying to work out why ledOne does not turn off when the correct button, in this case button 2 is pressed.
// Project Awesome. Code Entry With Four Buttons
#define BTNCNT 9
#define ATTCNT 2
int a = 0; //set a
int y = 0; // Set y
int x = 0; // Set x
int ledOne = 13; //Set Red LED
int ledTwo = 12; //Set Green LED
byte buttons[BTNCNT] = {2,3,4,5,6,7,8,9,10}; //Set buttons to array
byte pass[ATTCNT] = {2,3}; // Set password
byte attempt[ATTCNT]; // Set attempts array
byte state[ATTCNT]; // Make states array
byte oldstate[ATTCNT]; // Added to at end of loop
int time = 0;
void setup(){
pinMode(ledOne, OUTPUT);
pinMode(ledTwo, OUTPUT);
for(x=0; x<BTNCNT; x++){
pinMode(buttons[x], INPUT);
}
digitalWrite(ledOne, HIGH);
}
void loop(){
for(x=0; x<BTNCNT; x++){
state[x] = digitalRead(buttons[x]); // Check states of each button
}
if(attempt[0] != pass[0]){
TimeOut();
}
for(x=0; x<BTNCNT; x++){
if(oldstate[x] != state[x]){ //If these new states are different then the old ones
if(state[x] == HIGH)
{
attempt[y] = buttons[x]; //Set attempt (Just first index for now) to buttons pressed
y++;
time = millis();
}
}
}
for(x=0; x<2; x++){
if(attempt[0] == pass[0]){
Correct();
}
}
if ((millis() - time) > 10000){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
y=0;
}
}
if(y > 3){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
}
y=0;
}
for(x=0;x<BTNCNT;x++){
oldstate[x] = state[x];
}
}
void Correct(){
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
}
void TimeOut(){
digitalWrite(ledTwo, LOW);
digitalWrite(ledOne, HIGH);
}
It is not "overly complicated". It is wrong. Unless you fix the obvious stuff it is pretty pointless to fix the suspected functional defect.
You did not provide the schematic. You set the button pins to input without pullups. Maybe you have neither pullup nor pulldown resistors so the pins are probably floating. Floating pins always behave erratic and unpredictable. You also give no hint how "pushing the button" actually will change the level of the pin.
With other words: you are still not giving all required details.
I think Udo Klein has a great point but I would bet that a major pair of your program which has already been pointed out is that you aren't check the array correctly
OK, the buttons are all connected with pull down resistors from pin 2-10, pin 12 & 13 are the leds. All is excpected at the moment is that when a button, stated in pass[] is pressed the correct() function runs, and then after 5 seconds it runs TimeOut(). and everything is back to where it started, im pretty sure all the wiring is correct as an older version, which did something a bit different but woth the same wiring worked fine.
Here is a slightly updated piece of code, slightly..:
#define BTNCNT 9
#define ATTCNT 1
int a = 0; //set a
int y = 0; // Set y
int x = 0; // Set x
int ledOne = 13; //Set Red LED
int ledTwo = 12; //Set Green LED
byte buttons[BTNCNT] = {2,3,4,5,6,7,8,9,10}; //Set buttons to array
byte pass[ATTCNT] = {3}; // Set password
byte attempt[ATTCNT]; // Set attempts array
byte state[ATTCNT]; // Make states array
byte oldstate[ATTCNT]; // When button is hit, info added here
int time = 0;
void setup(){
pinMode(ledOne, OUTPUT); // Initiate all pins
pinMode(ledTwo, OUTPUT);
for(x=0; x<BTNCNT; x++){
pinMode(buttons[x], INPUT);
}
digitalWrite(ledOne, HIGH);
}
void loop(){
for(x=0; x<BTNCNT; x++){
state[x] = digitalRead(buttons[x]); // Check states of each button
}
for(x=0; x<BTNCNT; x++){
if(oldstate[x] != state[x]){ //If these new states are different then the old ones
if(state[x] == HIGH)
{
attempt[y] = buttons[x]; //Set attempt (Just first index for now) to buttons pressed
y++;
time = millis();
}
}
}
if(attempt[0] == pass[0]){
Correct();
}
if ((millis() - time) > 10000 || y > 3 ){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
y=0;
}
}
if(attempt[0] != pass[0]){
TimeOut();
}
for(x=0;x<BTNCNT;x++){
oldstate[x] = state[x];
}
}
void Correct(){
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
}
void TimeOut(){
digitalWrite(ledTwo, LOW);
digitalWrite(ledOne, HIGH);
}
well this is the code at the moment, and i cant see that issue
// Project Awesome. Code Entry With Four Buttons
#define BTNCNT 9
#define ATTCNT 1
int a = 0; //set a
int y = 0; // Set y
int x = 0; // Set x
int ledOne = 13; //Set Red LED
int ledTwo = 12; //Set Green LED
byte buttons[BTNCNT] = {2,3,4,5,6,7,8,9,10}; //Set buttons to array
byte pass[ATTCNT] = {3}; // Set password
byte attempt[ATTCNT]; // Set attempts array
byte state[ATTCNT]; // Make states array
byte oldstate[ATTCNT]; // When button is hit, info added here
int time = 0;
void setup(){
pinMode(ledOne, OUTPUT); // Initiate all pins
pinMode(ledTwo, OUTPUT);
for(x=0; x<BTNCNT; x++){
pinMode(buttons[x], INPUT);
}
digitalWrite(ledOne, HIGH);
}
void loop(){
for(x=0; x<BTNCNT; x++){
state[x] = digitalRead(buttons[x]); // Check states of each button
}
for(x=0; x<BTNCNT; x++){
if(oldstate[x] != state[x]){ //If these new states are different then the old ones
if(state[x] == HIGH)
{
attempt[y] = buttons[x]; //Set attempt[y]
y++; // add 1 to y
time = millis(); // set time to millis()
}
}
}
if(attempt[0] == pass[0]){ // attempt equals pass run: Correct()
Correct();
}
if(attempt[0] != pass[0]){ // if attempt doesnt equal pass run timeout
TimeOut();
}
if ((millis() - time) > 5000 || y > 3 ){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
}
y=0;
}
for(x=0;x<BTNCNT;x++){
oldstate[x] = state[x];
}
}
void Correct(){
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
}
void TimeOut(){
digitalWrite(ledTwo, LOW);
digitalWrite(ledOne, HIGH);
}