matrix keyboard program

Hello friends,

I request you because as you can imagine, I am a little in a mess... Indeed, I have to realize a programming which is there seem simple.

To explain you the project, I am a member of a class and we have several groups. We have to(program) a mobile platform with several modules with ARDUINO:

  • Joystick, detector of obstacles, accelerometer...

For my part, I have to manage the matrix keyboard. The purpose is to deliver a message " Mode ON activated " further to the seizure of a code which will be beforehand defined. There is a specificity, if the operator seizure 3 bad codes, the program generates a message " false Code " and does not authorize any more the seizure, in made an alarm will be activated.

As regards my program, this is what I began to make, I manage to post the characters seizures but I do not manage to recurl to leave 3 attempts and block afterward

THANK YOU for your help!

sketch_jun16a.ino (1.53 KB)

Add a counter that starts at zero. Increment the counter when the code entered is not correct. If the counter is 3, send the error message and do something to block further action, like an infinite loop or a flag that is checked before processing input.

Hi John,

Thanks for your help. When the user put a wrong code, I'm not sure that the counter is incremented because when I put 3 false code, there isn't any message... For the flag, how can I put it ?

Thanks a lot for your help

sketch_jun16a.ino (1.54 KB)

You should NEVER use "counter=counter++;" because the results are undefined. For some compilers the result is the same as for "counter=counter + 1" (which you want) but current compiler treats that undefined construct as:

temp = counter;
counter=counter+1; 
counter=temp;

Since the "counter=temp;" un-does the "counter=counter+1" the result is that nothing happens.

Use one of these:

counter = counter + 1;
counter += 1;  // Shorthand for "counter = counter + 1"
++counter;
counter++;

No compiler treats c = c++; in an undefined manner - the spec is clear c++ increments c and returns the previous value of c.

So c = c++; should not change c in any compiler

On the opposite the ++c construct increments c and returns the new version c

But long story short just use c++; when you want to increment c - nothing more needed

J-M-L:
No compiler treats c = c++; in an undefined manner - the spec is clear c++ increments c and returns the previous value of c.

"Sequence points also come into play when the same variable is modified more than once within a single expression. An often-cited example is the C expression "i=i++" which apparently both assigns 'i' its previous value and increments 'i'. The final value of 'i' is ambiguous, because, depending on the order of expression evaluation, the increment may occur before, after, or interleaved with the assignment. The definition of a particular language might specify one of the possible behaviors or simply say the behavior is undefined. In C and C++, evaluating such an expression yields undefined behavior.[4]"

[4] Clause 6.5#2 of the C99 specification: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."

Thanks friends, I've find the mistake, my variable counter was declared in the loop... so even I put counter++, when the program was restarted, the counter was initialized at 0.

So thanks a lot, now it's working :slight_smile:

Karim