Are arrays contents automatically set to zero? [SOLVED]

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Are arrays contents automatically set to zero? [SOLVED]

Post by DanPolka »

When an array is initialized as follows, are their contents automatically set to zero, or what?

Code: Select all

  static byte previous[4];  
  static long time[4]; 
Last edited by DanPolka on Sat Mar 08, 2014 6:36 pm, edited 1 time in total.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Are arrays contents automatically set to zero?

Post by adafruit_support_bill »

That depends on the compiler you are using and where the array is declared. Most compilers do initialize arrays declared at file scope.
See the section on initializing here:
http://www.cplusplus.com/doc/tutorial/arrays/

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: Are arrays contents automatically set to zero?

Post by DanPolka »

(hope this isn't a duplicate reply, I thought I sent one, but don't see it here.)

Thanks for the reference! I'm using the Arduino IDE.

That's interesting! It says,
By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.
But as far as I can see, in the function I'm looking at, with

Code: Select all

if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
neither 'previous[index]' nor 'time[index]' get data put into them before they are looked at the first time through, which doesn't seem too good.

It's probably not important, I'm just trying to understand the debounce code as well as I can.

Here's the best I've been able to figure it out, any additions or corrections would be appreciated!
(I'd like to be able to remove the actual debounce part from the scan switches part and make it a separate function, so I could call it more than once to check for double-tap, but I'm not sure how to do that, parameter passing etc.)

Code: Select all

byte check_switches()
{
  static byte previous[4];     // array of previous state of button input pins
  static long time[4];      // array of a point in time, reset for & on each keys pressing
  byte reading;       // a momentary reading of the state of a button pin
  byte pressed;       // a return of the PIN NUMBER for a button that has been pressed
  byte index;  // a zero based counter for the for-loop, used to access the (zero based) arrays
  pressed = 0;  // if no button pressed, return zero

  for (byte index = 0; index < 4; ++index) {  // loop through the button pin array, from 0 to 3
    reading = digitalRead(14 + index);    // read each pin in succession as loop progresses
    
    // THIS IS WHERE THE DEBOUNCE OCCURS:
    // it looks to see if the CURRENT immediate state of an button input pin is LOW,
    // AND the last(?) known recorded state of that pin was HIGH, 
    // AND this moment in time (millis()) minus the TIME of the LAST time the key wasn't pressed????
    if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
    {
      // switch has been pressed
      time[index] = millis();  // saving the moment in time this button was pressed
      pressed = index + 1;   // accounting for zero-based array index
      break;  // exiting from for loop
    }
    previous[index] = reading;  // not sure why this is HERE, and not ALSO just before the <break>
  }
  // return switch number (1 - 4)
  return (pressed);
}

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: Are arrays contents automatically set to zero?

Post by DanPolka »

Sigh, sorry, I confused myself, I'd already posted the last part of my reply just before this in another (related) thread! I didn't mean to double post, sorry. :( I'll try to avoid this in the future, I realize it wastes peoples time.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Are arrays contents automatically set to zero?

Post by adafruit_support_bill »

By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized
The arrays in question (previous and time) are declared "static". So while they are defined in the scope of the function, they are actually initialized the same as file-scope arrays. (With most compilers anyway).

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: Are arrays contents automatically set to zero?

Post by DanPolka »

Ah, ok, obvious now! Not used to C. Thanks.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”