Go Down

Topic: Newbie array woes (Read 633 times) previous topic - next topic

Gramuk

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.


AWOL

Maybe if you posted the code, so we don't have to imagine it?
"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.

Gramuk

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");

AWOL

Code: [Select]
//load these new array values
No.
Define a new variable with the same name, but limited to the scope until the next }
"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.

Jimmy60

#4
Feb 02, 2013, 11:25 pm Last Edit: Feb 02, 2013, 11:28 pm by Jimmy60 Reason: 1
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.

Go Up