FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Controlling LEDs using Switches

ARDUINO-Controlling LEDs using Switches:

You want to create and use a group of values (called arrays). The arrays may be a simple list or they could have two or more dimensions. You want to know how to determine the size of the array and how to access the elements in the array.This sketch creates two arrays: an array of integers for pins connected to switches and an array of pins connected to LEDs:


/*
array sketch an array of switches controls an array of LEDs
*/
int inputPins[] = {2,3,4,5}; // create an array of pins for switch inputs
int ledPins[] = {10,11,12,13}; // create array of output pins for LEDs
void setup()
{
for(int index = 0; index < 4; index++)
{
pinMode(ledPins[index], OUTPUT); // declare LED as output
pinMode(inputPins[index], INPUT); // declare pushbutton as input
digitalWrite(inputPins[index],HIGH); // enable pull-up resistors
//(see Recipe 5.2)
}
}
void loop(){
for(int index = 0; index < 4; index++)
{
int val = digitalRead(inputPins[i]); // read input value
if (val == LOW) // check if the switch is pressed
{
digitalWrite(ledPins[index], HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPins[i], LOW); // turn LED off
}
}

}
Discussion
          Arrays are collections of consecutive variables of the same type. Each variable in the collection is called an element. The number of elements is called the dimension of the array. The preceding example demonstrates a common use of arrays in Arduino code: storing a collection of pins. Here the pins connect to switches and LEDs . The important parts of this example are the declaration of the array and access to the array elements. 
      The following line of code declares (creates) an array of integers with four elements and initializes each element. The first element is set equal to 2, the second to 3, and so on: int inputPins[] = {2,3,4,5};
If you don’t need to initialize the values when you declare an array (perhaps the values will only be available when the sketch is running) you can declare the array as follows:
int array[4];
This declares an array of four elements with the initial value of each element set to zero. The number within the square brackets ([]) is the dimension, and this sets the number of elements. This array has a dimension of four and can hold, at most, four integer values. The dimension can be omitted if array declaration contains initializers because the compiler figures out how big to make the array by counting the number of initializers.
The first element of the array is element[0]:
int firstElement = inputPin[0]; // this is the first element
The last element is one less than the dimension, so in the preceding example, with a dimension of four, the last element is element 3:
Int lastElement = inputPin[3]; // this is the last element
It may seem odd that an array with a dimension of four has the last element accessed using array[3], but because the first element is array[0], the four elements are:
array[0],array[1],array[2],array[3]
In the previous sketch, the four elements are accessed using a for loop:
for(int index = 0; index < 4; index++)
{
//get the pin number by accessing each element in the pin arrays
pinMode(ledPins[index], OUTPUT); // declare LED as output
pinMode(inputPins[index], INPUT); // declare pushbutton as input
}
This loop will step through the variable index with values starting at 0 and ending at 3. It is a common mistake to accidentally access an element that is beyond the actual dimension of the array. This is a bug that can have many different symptoms and care must be taken to avoid it. One way to keep your loops under control is to set the dimension of an array by using a constant as follows:
const int PIN_COUNT = 4; // define a constant for the number of elements
int inputPins[PIN_COUNT] = {2,3,4,5};
for(int index = 0; index < PIN_COUNT; index++)
pinMode(inputPins[index], INPUT);
        Another common use of arrays is to hold a string of text characters. In Arduino code, these are called character strings (strings for short). A character string consists of one or more characters, followed by the null character (the value 0) to indicate the end of the string.

No comments:

Post a Comment