So I've had the Arduino Mega 2560 for 2 weeks now and starting to think up projects (done most of the blinky led's and wired up a 20x 4 LCD etc) and came across a problem to which I cannot solve. Spent 4 days trying to read up but I think I need some pointers if possible. Basically I have 4 momentary digital switch inputs when selected will set up a 7 digit numerical array for later use in the program. This array was set up as byte KitSet[7] = {1,2,3,4,5,6,7};

In the loop section I monitor the push buttons and if button 1 is high (pressed) I then want to change the 7 numerical values that reside int the array to a new set of numbers. I do this by again the same command byte KitSet[7] = {35,36,32,41,42,16,17};For each button after using the else command there is a new set of array numbers.

What I find is that although the button returns a high value and all other settings within the button command work well - why does the array never take these new values and always contain the values from the very first byte KitSet[7] = {1,2,3,4,5,6,7} ? I have a whole lot to learn but all tutorials I can find just tell how to create and read from an array. I'm an electronics guy trying to learn the wonders of the software side of things too - I've read so many tutorials and looked at other's sketches and I don't seem to be able to master this simple task (not much hope for me then...) - please be gentle - just started out here. Cheers.

Of course - yes - sorry - this is the portion of the loop button checking. I'm at another computer so don't have all the sketch. I realise this may be really primative but it works for me until I can get the hang of coding. The array is KitSet7] and I've tried using the "int" and also this "byte" but neither is doing the job. Again - first posting on here and so I will figure out also how to correctly upload sketches to posts too and so apologise in advance for this formatting.

Hi jimmy. I finally got home to my PC and upload the sketch with the array query. From what you said this makes sense that the array is only used within the context of that IF condition. I learn something new every day. So this sketch is basically a project to try getting my son into programming *(and me as it turns out). This is the old chestnut of the midi drum. I thought great - a simple one since he loves drums! What We're looking to do is to interface to a midi controller (which all works) and then choose different drum kits by selecting with simple input switches. As you can see the sketch is so basic but I tried so long trying to figure out menu systems that my son was losing interest. So we simplified it to just 2 buttons to select two kits, but even this I can get working. Any advice or pointers on how to set maybe a global array and then change it's values as per a simple switch press.Here goes... much cut down from the original 4 button. Again I realize how basic this is but just to try prove a principle before going too in deep. I have NO previous programming experience and it shows. But we start somewhere.

// constants won't change. They're used here to // set pin numbers:const int button1 = 22; // the number of the pushbuttonconst int button2 = 23; // the number of the pushbuttonconst int ledPin = 13; // the number of the LED pin

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState1 == HIGH) { int KitSet[7] = { 36,6,7,8,9,10,11 }; // New set of numbers in the array for button 1 press digitalWrite(ledPin, HIGH); // turn LED on: Visual indication that button press was registered lcd.clear(); lcd.setCursor(1,0); lcd.print("Kit Set 1 selected");

} // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState2 == HIGH) { int KitSet[7] = { 48,12,13,14,15,16,17 }; // New set of numbers in the array for button 1 press digitalWrite(ledPin, HIGH); // turn LED on: Visual indication that button press was registered lcd.clear(); lcd.setCursor(1,0); lcd.print("Kit Set 2 selected");

} // Enter the MIDI setup here // // deal with first piezo, this is kind of a hack val = analogRead(piezoAPin); if( val >= PIEZOTHRESHOLD ) { t=0; while(analogRead(piezoAPin) >= PIEZOTHRESHOLD/2) { t++; } noteOn(drumchan,KitSet[0], t*2); // Where KitSet[0] is the first value of the array and used in this example // Note that the other parameters in the array would be for different // later defined piezo inputs ; delay(50); noteOff(drumchan,KitSet[0],0); }}

Those KitSet arrays look like separate instances to me, at least on a glance, but then you also a global one. The curly brackets define context, you see. Ok, you have initialized the global array, but you don't have the code that accesses anything but the first item in the array

.

I would either set the values on the global array individually inside the if, or just pass the local array using a pointer to a function. You'd have int * as the parameter in the function and access an array item just like you would otherwise do outside the function. Then just make sure you don't go over the bounds of the array inside the function because the function wouldn't know how big the array is supposed to be. Then again, you could also pass the size of the array as either an int value for example.

But, I don't know how midi or piezo works and don't know what the array items represent to be able to tell what the code is supposed to look like. You could also have a two-dimensional global array that would define all the kitsets in one place. Then you'd just access individual kitsets by index.

I think I'm starting to understand this and guess I have to read more on arrays. Certainly this 2 dimensional array. What I'd originally planned to do was to have 7 inputs (Piezo inputs) and the array would define a note number to be output over the midi interface. Giving a kind of standard drum kit. 7 drum pads with 7 individual sounds defined in that 7 bit array. Then I thought ok this is simple.... lets make the ability to change the array values to output a different set of 7 numbers so as effectively changing the drum kit.You're right that the sketch above has the 7 array values but only ever looks at the first - this was intentional since I wanted to try getting this to work with the bare minimum of 1 drum pad (piezo input) and so the other 6 values were just dummy values and would be called for the other 6 piezo inputs if I got the first working.

I think I'll have a tinker around by defining the arrays at the beginning and then selecting the array depending on switch status. I'll do a lot more reading and plan a bit better rather than diving in.Trouble is, I work oversees during the week and only have weekends to try out on the device. Least I've lots to read while I'm away! Cheers, Graeme.