Go Down

Topic: How do I change an array once it has been defined (Read 579 times) previous topic - next topic

benzure

Feb 18, 2013, 05:28 pm Last Edit: Feb 18, 2013, 05:31 pm by benzure Reason: 1
I have built a secret knock sensor based on the Steve Hoefer script (http://grathio.com/2009/11/secret_knock_detecting_door_lock/) for the lab at work.

I'd like to use a switch to change the defined array at the start.

So, when the switch is HIGH or 1 the secretCode array changes to:
1)
Code: [Select]
int secretCode[maximumKnocks] = {70, 25, 25, 50, 50, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

And when it is LOW or 0 it uses the original stated below:
2)
Code: [Select]
int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  // Initial setup: "Shave and a Hair Cut, two bits."

The reason for this is that I'd like a different knock at the weekend for a different team without having to program it at the end of Friday and I thought a switch would be great.

In the setup I've included:
3)
Code: [Select]

int stateWeekend = digitalRead(switchOne);
if (stateWeekend == 1) {
    Serial.println("Weekend"); //debug
// array change
 }


Where I've commented for an array change, I have tried everything I'm aware of. By defining each one e.g. secretCode[0] = 70; secretCode[1] 25; e.t.c. within the setup and loop, as well as a complete array as above in code 1, without the int definition, with maximumKnocks as [] and so on... I've ran out of ideas.

I have been reading and trying different things but to my understanding, if I took the array out and placed it in the set up, the script wouldn't work.


PaulS

You have to assign elements one value at a time. If you have two or more arrays, you can copy data from one to another, one element at time, in a for loop:
Code: [Select]
for(byte i=0; i<maximumKnocks; i++)
{
   secretCode[i] = weekendKnocks[i]; // or weekdayKnocks[i]
}

UKHeliBob

How about using a 2 dimensional array and using the switch to change the index variable from dimension 0 to dimension 1.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

benzure


benzure

Actually, I have one more question. Obviously when the array is played once the door unlocks but is it possible that if the array were to be played twice it would trigger another LED?

What would that statement look like? 

PaulS

Quote
Obviously when the array is played once the door unlocks

You can't "play an array", so, not that is not obvious.

Nick Gammon


You have to assign elements one value at a time.


You could assign the pointer to the array, wouldn't that work?


You can't "play an array", so, not that is not obvious.


Yes, I wouldn't play with them if I could help it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

UKHeliBob


Actually, I have one more question. Obviously when the array is played once the door unlocks but is it possible that if the array were to be played twice it would trigger another LED?

What would that statement look like? 

If you mean can you count how many times the knock sequence has been successful and cause an action to take place when it has been used twice, then of course you can. 

When it has been successful once sat a variable to true.  When the sequence has been entered again check the variable.  If it is set to true then turn on the LED.  If you only want the LED to light when the code is entered correctly twice in succession then set the variable to false if code entry fails.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

benzure

Sorry about 'playing the array' but what UKHeliBob said is what I'm looking for. I'll see what I can come up with and post it here if I get stuck again. Thanks for pointing me in the right direction.

Go Up