Help with understanding this code.

Hey guys i'm trying imitate this code that has already been created by another user but do not get what some specific parts of the code are doing. It would be a great help if you could explain to me what the specific parts that i will highlight are doing in the code.

The code is for a lock that opened when a certain sequence of keys are pressed, these keys are linked to notes of a piano thus you have to play a tune to open the lock. The link to the other users project is http://www.instructables.com/id/Piano-Safe-With-Melody-key/

What I don't get is the if statements at the bottom where if "(button > 150){tone(piezo, 330); toon=7; volgende=false"

and/or

the whole of the decision making part at the bottom that starts with if (teller == 1 && toon == 2 && volgende == true){teller = 2; volgende = false;}

If possible could you explain what is going on here please

thank you :slight_smile:

Here is the code:

// gcceddFbbdc 25576684465
#include

Servo myservo;
int buttonPin = A0;
int button = 0;
int piezo = 3;
int toon = 0; // f=1 g=2 a=3 b=4 c=5 d=6 e=7 F=8
int volgende = true;
int teller = 1;

void setup() {
// Serial.begin(9600); //debugging
pinMode(13, OUTPUT);
myservo.attach(9);
myservo.writeMicroseconds(1000); }

void loop() {
delay(30); //debounce
button = analogRead(buttonPin);

//Serial.print(toon);
//Serial.print(volgende);
//Serial.println(button); //debugging

if (button > 400){tone(piezo, 349); toon=8; volgende=false;} // F
else {if (button > 150){tone(piezo, 330); toon=7; volgende=false;} // e
else {if (button > 100){tone(piezo, 294); toon=6; volgende=false;} // d
else{if (button > 80){tone(piezo, 262); toon=5; volgende=false;} // c
else{if (button > 60){tone(piezo, 247); toon=4; volgende=false;} // b
else{if (button > 48){tone(piezo, 220); toon=3; volgende=false;} // a
else{if (button > 38){tone(piezo, 196); toon=2; volgende=false;} // g
else{if (button > 20){tone(piezo, 175); toon=1; volgende=false;} // f
else{noTone(piezo); volgende=true;}}}}}}}}

if (teller == 1 && toon == 2 && volgende == true){teller = 2; volgende = false;}

if (teller == 2 && toon == 5 && volgende == true){teller = 3; volgende = false;} else{if(volgende == true && teller == 2 && toon != 2){teller = 1;}}

if (teller == 3 && toon == 5 && volgende == true){teller = 4; volgende = false;} else{if(volgende == true && teller == 3 && toon != 2){teller = 1;}}

if (teller == 4 && toon == 7 && volgende == true){teller = 5; volgende = false;} else{if(volgende == true && teller == 4 && toon != 7 && toon != 5){teller = 1;}}

if (teller == 5 && toon == 6 && volgende == true){teller = 6; volgende = false;} else{if(volgende == true && teller == 5 && toon != 7){teller = 1;}}

if (teller == 6 && toon == 6 && volgende == true){teller = 7; volgende = false;} else{if(volgende == true && teller == 6 && toon != 7){teller = 1;}}

if (teller == 7 && toon == 8 && volgende == true){teller = 8; volgende = false;} else{if(volgende == true && teller == 7 && toon != 6 && toon != 8){teller = 1;}}

if (teller == 8 && toon == 4 && volgende == true){teller = 9; volgende = false;} else{if(volgende == true && teller == 8 && toon != 8){teller = 1;}}

if (teller == 9 && toon == 4 && volgende == true){teller = 10; volgende = false;} else{if(volgende == true && teller == 9 && toon != 8){teller = 1;}}

if (teller == 10 && toon == 6 && volgende == true){teller = 11; volgende = false;} else{if(volgende == true && teller == 10 && toon != 4){teller = 1;}}

if (teller == 11 && toon == 5 && volgende == true){teller = 12; volgende = false;} else{if(volgende == true && teller == 11 && toon != 6){teller = 1;}}

if(teller == 12 && volgende == true){ teller = 13; digitalWrite(13, HIGH); myservo.writeMicroseconds(2000); delay(5000);} //2000 is the value for the open servo

if(teller == 13 && volgende == true && toon != 5){ digitalWrite(13, LOW); teller = 1; myservo.writeMicroseconds(1000);} //1000 is the value for the closed servo

}

Use code tags when you post code here.

This may get you started as an answer to your first question:

if (button > 150)       // if variable button is greater that 150
{ 
   tone(piezo, 330);    // call tone function with parameters pin number and a frequency
   toon=7;              // set variable (probably a Germanification of Tone)
   volgende=false       // set variable "English: following"
}

What I don't get is the if statements at the bottom where if "(button > 150){tone(piezo, 330); toon=7; volgende=false"

First, that is piss-poorly structured code.

if(button > 150)
 {
    tone(piezo, 330);
    toon=7;
    volgende=false;
 }

If you press a switch, play some note (that's what tone() does), and record what that note is (that's what toon seems to be for). volgende seems to indicate that you are still entering button presses, because the lock isn't opened yet.

Thank you for the help! could you possibly explain how this part is working such as what "teller" is doing

if (teller == 1 && toon == 2 && volgende == true){teller = 2; volgende = false;}

if (teller == 2 && toon == 5 && volgende == true){teller = 3; volgende = false;} else{if(volgende == true && teller == 2 && toon != 2){teller = 1;}}

if (teller == 3 && toon == 5 && volgende == true){teller = 4; volgende = false;} else{if(volgende == true && teller == 3 && toon != 2){teller = 1;}}

if (teller == 4 && toon == 7 && volgende == true){teller = 5; volgende = false;} else{if(volgende == true && teller == 4 && toon != 7 && toon != 5){teller = 1;}}

if (teller == 5 && toon == 6 && volgende == true){teller = 6; volgende = false;} else{if(volgende == true && teller == 5 && toon != 7){teller = 1;}}

if (teller == 6 && toon == 6 && volgende == true){teller = 7; volgende = false;} else{if(volgende == true && teller == 6 && toon != 7){teller = 1;}}

if (teller == 7 && toon == 8 && volgende == true){teller = 8; volgende = false;} else{if(volgende == true && teller == 7 && toon != 6 && toon != 8){teller = 1;}}

if (teller == 8 && toon == 4 && volgende == true){teller = 9; volgende = false;} else{if(volgende == true && teller == 8 && toon != 8){teller = 1;}}

if (teller == 9 && toon == 4 && volgende == true){teller = 10; volgende = false;} else{if(volgende == true && teller == 9 && toon != 8){teller = 1;}}

if (teller == 10 && toon == 6 && volgende == true){teller = 11; volgende = false;} else{if(volgende == true && teller == 10 && toon != 4){teller = 1;}}

if (teller == 11 && toon == 5 && volgende == true){teller = 12; volgende = false;} else{if(volgende == true && teller == 11 && toon != 6){teller = 1;}}

if(teller == 12 && volgende == true){ teller = 13; digitalWrite(13, HIGH); myservo.writeMicroseconds(2000); delay(5000);} //2000 is the value for the open servo

if(teller == 13 && volgende == true && toon != 5){ digitalWrite(13, LOW); teller = 1; myservo.writeMicroseconds(1000);} //1000 is the value for the closed servo

This is why you are exhorted to use code tags.

code tags.PNG

code tags.PNG

Thank you for the help! could you possibly explain how this part is working such as what "teller" is doing

if (teller == 1 && toon == 2 && volgende == true){teller = 2; volgende = false;}

if (teller == 2 && toon == 5 && volgende == true){teller = 3; volgende = false;} else{if(volgende == true && teller == 2 && toon != 2){teller = 1;}}

Start with the second line in that mess. It's determining which switch was pressed (what value toon has) and which order that switch should have been pressed in (what value teller has). If the proper switch was pressed as the second switch press event, teller is incremented. If the wrong switch was pressed, teller is decremented.

Following on from the previous comment.
The code is not very nicely written and could be much better structured and more compact. It could be reduced to a few lines if the note sequence needed to open the lock and the enumeration of the analog values returned by key presses were to be represented by arrays. But, anyway . . .

teller is the position in the sequence of key presses that we are currently expecting.

volgende is a continuation flag. We can continue once a key has been pressed and released. The code could be restructured so it is tested only once instead of about 30 times.

toon is the number of the piano key just pressed.

so: 

if (teller == 5 && toon == 6 && volgende == true){teller = 6; volgende = false;} else{if(volgende == true && teller == 5 && toon != 7){teller = 1;}}

can be broken down as ....

if ( 
      teller == 5             // if we are expecting the 5th key in the sequence
      && toon == 6            // and key number 6 was pressed
      && volgende == true     // and we can continue
   )
   {
                              // good. we have the correct key in the correct sequence.
      teller = 6;             // now we expect the 6th key in the sequence       
      volgende = false;       // block until the next key has been pressed and released.
   } 
   else 
   {                          // bad. The wrong key was pressed for this position (5) 
      if ( 
            volgende == true  // if we can continue 
            && teller == 5    // and we are expecting the 5th key in the sequence
            && toon != 7      // and key 7 wasn't pressed ( why 7 ????? )
         ){teller = 1;}       // start again from the begining and expect the first key.
   }

Ellis_Leahy:
Thank you for the help! could you possibly explain how this part is working such as what "teller" is doing

Looks like:

toon - selects what note is playing (A, B, C, etc).
teller - the current position in the melody.
volgende - whether the melody is currently playing or not.

Hey guys i'm trying imitate this code

Please don't. Don't imitate this code. It is abominable.

Hey guys thanks for all the help, its is appreciated! I only really want 2 buttons on my circuit so I have changed the code to just two buttons as you can see below (if there are any problems with the changes I have made could you point them out please)

Furthermore how would I write the sequence of just the first button being pressed then the second button being pressed to move the servo. I mean that I would only have to push two buttons, one after the other to move the servo.

Thanks again
:slight_smile:

#include <Servo.h>

Servo myservo;
int buttonPin = A0;
int button = 0;
int piezo = 3;
int toon = 0; // c=1 d=2
int volgende = true;
int teller = 1;

void setup() {
// Serial.begin(9600); //debugging
pinMode(13, OUTPUT);
myservo.attach(9);
myservo.writeMicroseconds(1000); }

void loop() {
delay(30); //debounce
button = analogRead(buttonPin);

//Serial.print(toon);
//Serial.print(volgende);
//Serial.println(button); //debugging

{if (button > 100)
{
tone(piezo, 294);
toon=2;
volgende=false;} // d
else{if (button > 80)
{
tone(piezo, 262);
toon=1;
volgende=false;} // c
else
{noTone(piezo);
volgende=true;}}}}

6v6gt:
Use code tags when you post code here.

...

dougp:
This is why you are exhorted to use code tags.
...

When you've presented your code more nicely, you can attempt to explain how the variable button is going to get its value.

And how do you suggest I would be able to improve the layout of the code?

Ellis_Leahy:
And how do you suggest I would be able to improve the layout of the code?

With ......

CODE TAGS

Use the </> widget in the post editor or enclose your code in these:

[ CODE ]

my code here

[ /CODE ]

(without any spaces in the tags)

Okay cool thank you! thank you for being patient :slight_smile: Here is the code withing the tags.

#include <Servo.h>

Servo myservo; 
int buttonPin = A0; 
int button = 0; 
int piezo = 3; 
int toon = 0; //  c=1 d=2 
int volgende = true; 
int teller = 1;

void setup() { 
// Serial.begin(9600); //debugging 
pinMode(13, OUTPUT); 
myservo.attach(9); 
myservo.writeMicroseconds(1000); }

void loop() { 
delay(30); //debounce 
button = analogRead(buttonPin);

//Serial.print(toon); 
//Serial.print(volgende); 
//Serial.println(button); //debugging

{if (button > 100)
{
  tone(piezo, 294); 
  toon=2; 
  volgende=false;} // d 
else{if (button > 80)
{
  tone(piezo, 262); 
  toon=1; 
  volgende=false;} // c 
else
{noTone(piezo);
volgende=true;}}}}

Well, that is a lot better, at least optically.
But you still have to do this for your modified code :

explain how the variable button is going to get its value.

6v6gt:
Well, that is a lot better, at least optically.
But you still have to do this for your modified code :

explain how the variable button is going to get its value.

Doesn't

button = analogRead(buttonPin);

explain it well enough?

Okay thank you!

PaulS:
Doesn't

button = analogRead(buttonPin);

explain it well enough?

So you are suggesting for me to used that? and where would i put this in my code? just at the end of my code or at the start? and then write the decision making aka the if statements?

So you are suggesting for me to used that?

I copied that line from your code.

Oops. I missed that.
Anyway, the OP can now give an example of the “tune” which has to be played to open the lock. Of course, with only 2 piano keys available it’s unlikely to be a Rachmaninoff piece, but useful to know would be the number of notes required to open the lock.