hi, I'm really new to programming and would love some advice on why my code is not working as expected.
I'm trying to store the value of the new button pressed (there is 6 in a sequence, defined by a voltage set out using a voltage divider, then all feeding into A0) in the array 'buttons' which should then turn on the red LED and off the green LED.
const int buttonPin = A0;
const int greenLed = 4;
const int redLed = 2;
int buttons [4] ;
//array of 4 button press sequence
int buttonVal = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println("unlocked");
//starts as unlocked
}
void loop() {
// put your main code here, to run repeatedly:
if (buttons[3]==0){
buttonVal = analogRead(buttonPin);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println("Please enter a code sequence.");
Serial.print ("buttonVal: ");
Serial.print(buttonVal);
Serial.print (" buttons: ");
Serial.print(buttons[0]);
Serial.print(buttons[1]);
Serial.print(buttons[2]);
Serial.println(buttons[3]);
int code (buttonVal);
}
if (buttons[3]!=0){
Serial.print (" new code: ");
Serial.print(buttons[0]);
Serial.print(buttons[1]);
Serial.print(buttons[2]);
Serial.println(buttons[3]);
delay(250);
// if the array buttons is 0,0,0,0 = turn green LED on, write "entre code" and run code function
}
}
int code (int value) {
value=analogRead(A0);
int i=0;
//i is set to 0
while (i<4){
//as long as i is less than 4, continue. i is not the code itself, it is the length of the code
if (value>=1000){
buttons[i]=1;
i++;
delay(250);
//if 1st button pressed, store in array buttons then add 1 to i. e.g. if 1 is pressed first: buttons[0]=1, i=1. if pressed 3rd: buttons[2]=3, i=3 etc.
}
else if (value>=720 && value<=750){
buttons[i]=2;
i++;
delay(250);
}
else if (value>=560 && value<=570){
buttons[i]=3;
i++;
delay(250);
}
else if (value>=450 && value<=470){
buttons[i]=4;
i++;
delay(250);
}
else if (value>=392 && value<=400){
buttons[i]=5;
i++;
delay(250);
}
else if (value<=350 && value>=330){
buttons[i]=6;
i++;
delay(250);
} //when code function called, read A0 voltage and put assosiated value in array.
}
}
I have no idea where I have gone wrong although I clearly have somewhere.
I hope this makes sense to someone.
When you call a function you just use the function name.
Then when you define your function you pass it the variable value. This makes variable value a totally different variable than you defined before. It has the same name but it is only valid while your code function is being run. You then proceed to wipe out any number you passed by assigning it to the result of an analogue read.
Having put your numbers in the buttons array then you don’t seem to do anything with them.
thanks for your replies, however it only seems to serial print:
unlocked
Please enter a code sequence.
buttonVal: 0 buttons: 0000
and then stops completely no mater how many buttons I press.
I was expecting the code to run this and show me the change in the array as I pressed the buttons and then when the array was full (buttons[3}!=0) to print the new code and turn the red LED on and green off. code (buttonVal); { Serial.print("new code: "); Serial.print(buttons[0]); Serial.print(buttons[1]); Serial.print(buttons[2]); Serial.println(buttons[3]); }
i've changed this part and took out the analogRead A0 at the start of the 'code' function.
So when this happens please post all the latest code you have tried, so we can check if you have taken all the advice you have been given and applied it correctly. We need to see the whole of your code if we are to be of any help. Post this in a new post, do not change any of your previous posts.
// if the array buttons is 0,0,0,0 = turn green LED on, write "entre code" and run code function
}
}
int code (int value) {
int i=0;
//i is set to 0
while (i<4){
//as long as i is less than 4, continue. i is not the code itself, it is the length of the code
if (value>=1000){
buttons[i]=1;
i++;
delay(250);
//if 1st button pressed, store in array buttons then add 1 to i. e.g. if 1 is pressed first: buttons[0]=1, i=1. if pressed 3rd: buttons[2]=3, i=3 etc.
}
else if (value>=720 && value<=750){
buttons[i]=2;
i++;
delay(250);
}
else if (value>=560 && value<=570){
buttons[i]=3;
i++;
delay(250);
}
else if (value>=450 && value<=470){
buttons[i]=4;
i++;
delay(250);
}
else if (value>=392 && value<=400){
buttons[i]=5;
i++;
delay(250);
}
else if (value<=350 && value>=330){
buttons[i]=6;
i++;
delay(250);
} //when code function called, read A0 voltage and put assosiated value in array.
}
}
hope this makes sense.
thanks so much for all your help
What do you mean with 'code sequence' ? You don't read anything from the serial line.
Within the function 'code' the parameter 'value' is never changed, therefore it will be 0 at the start of the sketch ( no button is pressed ) and your while loop will never end, because 'i' is never incremented.
Edit: Now I think you mean a sequence of button presses? But that will not work, because you never read your analog input after asking for a code sequence.
hi, yes its mainly just for me to know whereabouts the code is running. so if it says 'enter a code sequence.' it means the buttons have not yet been pressed and no sequence has been set to 'unlock'.
so im trying to make a lock where i can set the 'code' sequence at the beginning, it locks, and then the same sequence must be pressed again to open the lock.
Surely. But your function 'code' does not do, what you intended it to do.
In this case, it makes no sense to pass the parameter 'value'. The key value must always be read in again in the function. And it must also be checked when the button is released again and the next button is pressed ( it may be the same button that is pressed twice, isn't it? ).