Thursday 29 June 2017

Array - Knight Rider - & initialize PinMode - Arduino

 This is a exercise in Arrays
It makes use of 6 LEDs connected to the pins 2 - 7 on an arduino uno using 220 Ohm resistors.
They are triggered like the lights of "KIT" - the car belonging to the Hoff.

A bit about arrays first.
They are a list or collection of variables that can be accessed via an index number. 
      The word "array" isn't actually used. The symbols [] and {} do the job.
      eg: int myArray[] = {6,21,34,2,1,0,152}; 
      Here we declared an array with 7 values.  Arduino creates 7 places in memory for these values.
 
     We can also just tell the arduino to create 7 memory spots, and then enter the values later :
      int myArray[7];
 
To assign a value to the second spot we use a command like this:
myArray[1] = 21; 
This is the index number.
The first spot always has an index value of 0 (they are zero indexed).
 
Code 1
Link:
 
Such a simple piece of code.
This is a great way of cycling through different lists.
In this example we use the array to choose which LED to light. 
It could just as easily be a menu.
Another way might involve using the "switch case".... will cover in another post
 
 // ******************************************************************************
/* Knight Rider 3
* --------------
*
* This example concentrates on making the visuals fluid.
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/
int pinArray[] = {2, 3, 4, 5, 6, 7};
int count = 0;
int timer = 30;
void setup(){
for (count=0;count<6;count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
for (count=5;count>0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
}
// ********************************************************* 
 

 Use 220 ohm resistors.
Cathode of the LEDs connect to gnd.
//88888888888888888888888888888888888
This second bit of code is really useful for setting all your pins
to output mode for example. 
Rather than having to type & initialize each pin as an output.
The section in the void setup is the bit that does it. 
//88888888888888888888888888888888888888888
/*
   Array sketch with for loop()
   an array of 3 LEDs that blink consecutively
*/

// create array of output pins for LEDs
int ledPin[] = {10, 11, 12};

void setup()
{
  //  for loop to set all ledPins to output mode
  for (int index = 0; index < 3; index++)
  {
    // declare LED as output
    pinMode(ledPin[index], OUTPUT);

  } //close for loop()
} // close void setup()

void loop()
{

 // for loop() to blink LEDs consecutively
  for (int i = 0; i < 3; i++)
  {
     // set LEDpins HIGH
    digitalWrite(ledPin[i], HIGH);
    // add 1 second delay
    delay(1000);
    // set LEDpins LOW
    digitalWrite(ledPin[i], LOW);
  } // closing for loop()

} // close void loop() 
 
 
//8888888888888888888888888888888888888888888888888888888888 
 Links
+ https://www.tutorialspoint.com/arduino/arduino_arrays.htm
 
 ---------------------------------
-------------------------------------
 

No comments:

Post a Comment