Hi guys, 1st of all i am noob and this is my 1st project.
Short description.
The aim of code part is to generate random array consisting of numbers 1-3 and store in array of with length which is possible to change (declare as int?).
Then to declare 3 digital switches as numbers 1-3 and store pressed sequence in another same type array which can be compared with previous.
Experience so far
I was trying to play around with code example which is given here (http://arduino.cc/forum/index.php?topic=144748.0)
The final one works well but it is a bit confusing. I can not understand if the byte array is stored like string and then the input from buttons is being stored as string and then compared? because in case when i wanted to change byte combination[] = "1332" to int combination = {1,3,3,2} i have got nonsense. as well i was trying to implement random
randomSeed(analogRead(0));
for (int i=0; i <= 4; i++){
combination = random(1, 4); [/quote] generator in setup and to print this default array to serial > int x; > for (x = 0; x < 4; x = x + 1) { > Serial.println(myPins[x]); but as well i got nonsense. > const int greenled = 11; > const int redled = 8; > const int b1 = 2; > const int b2 = 3; > const int b3 = 4; > > int pwcount; > byte combination[] = "1332"; > byte userInput[4]; > > int buttonstate1 = 0; > int buttonstate2 = 0; > int buttonstate3 = 0; > > void setup() { > pinMode(greenled, OUTPUT); > pinMode(redled, OUTPUT); > pinMode(b1, INPUT); > pinMode(b2, INPUT); > pinMode(b3, INPUT); > Serial.begin(9600); > } > > void loop(){ > buttonstate1 = digitalRead(b1); > buttonstate2 = digitalRead(b2); > buttonstate3 = digitalRead(b3); > > if (buttonstate1 == HIGH){ > userInput[pwcount] = '1'; > pwcount++; > delay(300); > Serial.print('1'); > } > if (buttonstate2 == HIGH){ > userInput[pwcount] = '2'; > pwcount++; > delay(300); > Serial.print('2'); > } > if (buttonstate3 == HIGH){ > userInput[pwcount] = '3'; > pwcount++; > delay(300); > Serial.print('3'); > } > for(byte n = 0; n <=4; n++){ > > > if (userInput[pwcount] == combination[n] && pwcount >=4){ > digitalWrite(redled, LOW); > digitalWrite(greenled, HIGH); > Serial.println("unlocked"); > pwcount = 0; > > } > else { > if(userInput[n] != combination[n] && pwcount >=4){ > digitalWrite(greenled, LOW); > digitalWrite(redled, HIGH); > Serial.println("Denied"); > pwcount = 0; > n = 0; > } > } > } So maybe somebody can explain me what am I doing wrong and how can i improve it? Regards, Q
If you're trying to input a sequence of numbers corresponding to a sequence of button presses, your logic is wrong.
You need to wait until any button is pressed, store the number corresponding to the pressed button in your array, wait for the next button to be pressed, store the corresponding number in the next element of the array, and so on. For example, you could define a function to wait for a button press and return the corresponding number; call that once for each array element and you have your input sequence:
// incomplete, untested
const byte BUTTON_COUNT = 3;
const int buttonPins[BUTTON_COUNT] = { 2, 3, 4 };
const byte KEY_COUNT = 4;
byte userInput[KEY_COUNT];
for(byte i = 0; i < KEY_COUNT; i++)
{
userInput[i] = getKey();
}
// returns a number in the range 1 .. BUTTON_COUNT
byte getKey()
{
byte result = 0;
while(result == 0)
{
for(byte i = 0; i < BUTTON_COUNT; i++)
{
if(digitalRead(buttonPins[i]) == HIGH)
{
result = i+1;
}
}
}
return result;
}
Thank you for help and explanation, i will brainstorm peters code to read and store buttons now, but so far i have succeeded to generate random array and print it to serial.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
byte hashish[4] = {5, 2, 6, 4};
// berore random generator.
// working random generator for array of length 4
// generated numbers are from 1 to 3included
randomSeed(analogRead(0));
int j;
for (int j=0; j <= 4; j++){
hashish[j] = random(1, 4);
}
// working serial print full array in one line
int i;
for (i = 0; i < 4; i = i + 1) {
Serial.print(hashish[i]);
}
Serial.println(" ");
delay (1000);
// put your main code here, to run repeatedly:
}
working as charm )
and gonna work now on storing arrays from button push sequence
Okay i see this mistake now as well, so with this sign it stores 5th array and increases it's size automatically, but in serial print it skip it because i requested to print 4 right?