Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Saturday, September 18, 2010

8 LED Arduino

With a little free time this weekend, I invested it in my Arduino. I selected a project from ardx.org, CIRC02. The board layout (pdf link) involves 8 LEDs controlled by 8 output pins 2 through 9.

The initial project blinks the LEDs in ascending order, and then repeats. Wiring the board took longer than building the project




The original project source
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  for (int i = 0; i < 8; i++) {
      pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  oneAfterAnotherNoLoop();
}

void oneAfterAnotherNoLoop() {
    int delayTime = 75;
    
    for (int i = 0; i < 8; i++) {
        digitalWrite(ledPins[i], HIGH);
        delay(delayTime);
        digitalWrite(ledPins[i], LOW);
        delay(delayTime);
    }
}

With the board wired up, the blinking LEDs didn't satisfy me. I created a few other projects that used the same wiring. Adding few utility functions, and an amplification array. With this basic set up, each project need only to implement an in_order() function
/**
 * Finds the number of elements in an array.
 */
#define NELE(array) (sizeof(array) / sizeof(array[0]))

/**
 * The amount to decay each pin by
 */
#define DECAY_AMOUNT 75

/**
 * The number of miliseconds to delay by
 */
#define DELAY_MS 80

int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
int ledAmp[NELE(ledPins)];

/**
 * setup
 *
 * Invoked by the arduino infrastructure to initialize state.
 */
void setup() {
  for (int i = 0; i < NELE(ledPins); i++) {
    pinMode(ledPins[i], OUTPUT);
    ledAmp[i] = 0;
  }
}

/**
 * Neverending loop
 */
void loop() {
  in_order();
}

/**
 * Decays all the pin outs that are in use
 *
 * @param[in] amount the amount to decay the pins by
 */
void decay_pins(int amount) {
  for (int i = 0; i < NELE(ledPins); i++) {
    ledAmp[i] -= amount;
    if (ledAmp[i] < 0) {
      ledAmp[i] = 0;
    }
  }
}

/**
 * Writes all the new pin values
 */
void write_pins() {
  for (int i = 0; i < NELE(ledPins); i++) {
    analogWrite(ledPins[i], ledAmp[i]);
  }
}
The high-top fade, the LEDs are faded left to right




Source to in_order
void in_order() {
  for (int i = 0; i < NELE(ledPins); i++) {
    ledAmp[i] = 255; /* Set the pin high */
    write_pins(); /* Write all pin values */
    delay(DELAY_MS);

    decay_pins(DECAY_AMOUNT); /* Decay all pins */
    write_pins();
    delay(DELAY_MS);
  }
}


The Battle Star Galactica, where the LEDs are fade left to right, then right to left.






Source to in_order
void in_order() {
  for (int i = 0; i < NELE(ledPins) - 1; i++) {
    ledAmp[i + 1] = 255; /* Set the pin high */
    ledAmp[i] = 255; /* Set the pin high */

    write_pins(); /* Write all pin values */
    delay(DELAY_MS);

    decay_pins(DECAY_AMOUNT); /* Decay all pins */
    write_pins();
    delay(DELAY_MS);
  }

  for (int i = NELE(ledPins); i > 0 ; i--) {
    ledAmp[i] = 255; /* Set the pin high */
    ledAmp[i - 1] = 255; /* Set the pin high */

    write_pins(); /* Write all pin values */
    delay(DELAY_MS);

    decay_pins(DECAY_AMOUNT); /* Decay all pins */
    write_pins();
    delay(DELAY_MS);
  }
}

The explosion, the center LED is lit up then fades away towards the edges





Source to in_order
void in_order() {
  int center = NELE(ledPins) / 2;
  for (int i = 0; i < NELE(ledPins) / 2; i++) {
    ledAmp[center - i] = 255; /* Set the pin high */
    ledAmp[center + i] = 255; /* Set the pin high */

    write_pins(); /* Write all pin values */
    delay(DELAY_MS);

    decay_pins(DECAY_AMOUNT); /* Decay all pins */
    write_pins();
    delay(DELAY_MS);
  }
  if (NELE(ledPins) % 2 == 0) {
    /* NELE(ledPins) is even */
    ledAmp[0] = 255;
  }
}

Friday, September 10, 2010

Blink ... Blink ... Blink ...

Setting up the Arduino SDK on my laptop was the hardest part of this project. Some of the cross compiling set up scripts on gentoo have a couple of issues. Which was just as fun as chasing down the need to use icedtea for the SDK instead of the JRE from Sun (now Oracle.)




The source for the project is extremely short.
int ledpin = 13;

void setup() {
    pinMode(ledPin, OUTPUT);
}

void loop() {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
}
 I'm really looking forward to more Arduino projects.