Pages: [1]   Go Down
Author Topic: Newbie array woes  (Read 612 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe if you posted the code, so we don't have to imagine it?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

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.

// check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {     
    //
    byte KitSet[7] = {
      36,22,36,4,5,6,7        }; //load these new array values
    digitalWrite(ledPin, HIGH); // just visually confirms the button was registered as pressed
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Button 1 pressed"); // just confirms via LCD that button 1 was pressed
    lcd.setCursor(2,2);
    lcd.print("Style 1");

  }
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH) {     
    // turn LED on:
    byte KitSet[7] = {
      48,12,13,14,15,16,17        }; //load these new array values
    digitalWrite(ledPin, HIGH); // just visually confirms the button was registered as pressed
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Button 2 pressed"); // confirms via LCD that button 2 was pressed
    lcd.setCursor(2,2);
    lcd.print("style 2");
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//load these new array values
No.
Define a new variable with the same name, but limited to the scope until the next }
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Two things I see quickly is, that isn't how you read a button. You need to call an digitalRead().

Second, the way you declare the arrays they will only scope to the if statement they are declared in. In other words once the if statement is done the array will be tossed to the bit bucket.

Once you get all your code posted I suspect we may find that a two dimensional array will serve your purpose.
« Last Edit: February 02, 2013, 05:28:32 pm by Jimmy60 » Logged

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

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.

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

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status

//Define initial variables for first use
// Order listed below
int KitSet[7] = {
  90,30,3,4,5,6,7};

//Define now the MIDI PARTS -
#define drumchan           16
//Define input pins
#define piezoAPin  0            // single piezo on Analogue pin 0
#define ledPin     13           // for midi out status and button press status indication
#define PIEZOTHRESHOLD 100      // analog threshold for piezo sensing
int val,t;                      // for use as velicity sense from Piezo - not used at present

// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);   // 20x4 LCD connected

void setup() {

  pinMode(ledPin, OUTPUT);               // SETS LED BLINK ON INPUT / OUTPUT
  pinMode(button1, INPUT);               // Set the pushbutton pins as an input:
  pinMode(button2, INPUT);
  Serial.begin(31250);                   // set MIDI baud rate
  lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print("Button 1 for Kit 1");
  lcd.setCursor(1,2);
  lcd.print("Button 2 for Kit 2");

}
// Start looping the inputs
void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);

  // 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);
  }
}

// Send a MIDI note-on message.
void noteOn(byte channel, byte note, byte velocity) {
  midiMsg( (0x90 | channel), note, velocity);
}

// Send a MIDI note-off message. BUT actually sending a NOTE ON (as this simluates a note off when velocity = 0
void noteOff(byte channel, byte note, byte velocity) {
  midiMsg( (0x90 | channel), note, 0x00);
}

// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
  digitalWrite(ledPin,HIGH);  // indicate we're sending MIDI data
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(127);              // Just set max velocity otherwise use byte data2
  digitalWrite(ledPin,LOW);
  Serial.print (data1);
}
 
Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Pages: [1]   Go Up
Jump to: