10-23-2012 05:54 AM - edited 10-23-2012 05:55 AM
I am newbie in labview, I only have a few weeks learning by myselft and almost everything that I learned was from here, but I have one question.
I would like to store in a array a number of pressed buttons. Any time, I press a button, this will be stored in the array, but the problem is the bool to int conversion, only change ON to 1, so I wont know which button was pressed and also 0 is not usefull. For example,
Button 1 will have a value of 1;
Button 2 = 2;
Button 3 =3;
Button 4 = 4; and so on...
so, If I press once every button, the result should be 1D array indicator of 4 elements:1-2-3-4.
I really appreciate any comment or example that you can provide to me
Solved! Go to Solution.
10-23-2012 06:45 AM
Since you are working with boolean array, what do you think about "(boolean) array to number"?
Norbert
10-23-2012 06:55 AM - edited 10-23-2012 06:56 AM
Every button in the joystick is a boolean element, so I would like to assign a numerical value for every button. In my project, I used select to assign a numerical value but I dont know if there are any other different way and more usefull for my purpose that is store its numerical value in order I pressed in the array.
10-23-2012 07:00 AM
How are you going to handle multiple buttons being pressed at the same time?
Ignoring the multiple button presses for a moment, one option would be to use the Search 1D Array for a TRUE value and increment the resulting index. So button 1 would be 1, etc. and no buttons pressed would be 0.
10-23-2012 07:10 AM - edited 10-23-2012 07:16 AM
No, Only one button per time
I am trying to recreate a game where you have a 10 random number between 1 to 4. So, After press a sequence of 10 buttons (A, B, X,Y), the program gonna compare the order and if the order is correct, you win.
It is a little game that I would like to recreate about memorize sequences of numbers
I upload a vi file and a picture of the block diagram where only have the button counter, and a comparator of a random array with a array of 1's that i create just to check how to compare two arrays, but I dont have a clear idea about how to create a array of buttons values
10-23-2012 08:14 AM - edited 10-23-2012 08:16 AM
This probably isn't exactly what you want, but it should get you pretty far.
First of all, use the Cluster to Array function to get an array of the buttons pressed (boolean array). From there, use the Search 1D Array to find which button was pressed. If a button was pressed (-1 means not found, I incremented the index to make life slightly easier), add it to your array. I kept a count of number of buttons pressed and the Replace Array Subset so memory wasn't being reallocated.
Secondly, right-click on your Equal comparison. You will find a menu item for Comparison Mode->Compare Aggregates. This will give you a single boolean since it will now compare the arrays themselves.
You really should have a wait in the loop so that it isn't running at full speed (100ms should be good). Also (just noticed my bug) the Greater? should be Greater or Equal?.
10-23-2012 08:31 AM
Here is some "comparable" excerpt code to crossrulez code, which enables you to handle simultaneously pressed buttons (if needed):
Norbert
10-23-2012 09:03 AM - edited 10-23-2012 09:04 AM
Crossrulz, The program is not storing the button, I think that is the case, I create four case and every case I repeat the same operation. I think that is wrong,
what are the properties of the case structure for this program?
the default case is "use default case if is unwired "?
10-23-2012 09:05 AM
looks like we'll be seeing a new post on the R-G thread
10-23-2012 09:21 AM
@fastlater_pro wrote:
Crossrulz, The program is not storing the button, I think that is the case, I create four case and every case I repeat the same operation. I think that is wrong,
what are the properties of the case structure for this program?
the default case is "use default case if is unwired "?
You really should not have the exact same code in different cases. You can set the case to be "1..4". The ".." means a range. So any number from 1 to 4 will generate that case. That will help elimate the duplicate code.
I don't know where you are getting the "use default case if is unwired". The way the default works is if none of the other cases are satisfied, the default is called.
How do you know the button presses are not being stored? You might want to move the Buttons Pressed indicator inside of the loop so you can get an update as you loop through.
Ahhh, I just noticed that your initial array is empty. Set the index to 9 and type 0 in the array. That will initialize your array to 10 elements.