Pages: [1]   Go Down
Author Topic: How do I change an array once it has been defined  (Read 469 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:

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.

« Last Edit: February 18, 2013, 11:31:05 am by benzure » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
for(byte i=0; i<maximumKnocks; i++)
{
   secretCode[i] = weekendKnocks[i]; // or weekdayKnocks[i]
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3488
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How about using a 2 dimensional array and using the switch to change the index variable from dimension 0 to dimension 1.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your help. Issue resolved.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Obviously when the array is played once the door unlocks
You can't "play an array", so, not that is not obvious.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3488
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: