2009-05-23

The obligatory "Blink LED 13"

General assumptions on this blog are that you have some electronics background, know:
  1. Basic TTL logic,
  2. Grounding,
  3. That all voltages are relative to common ground,
  4. Sending a signal to another Arduino on a wire likely requires a common ground,
  5. That internally, the Arduino uses +5Vdc and +3.3Vdc and that exceeding these in the wrong places can release smoke or at the least burn out pins,
  6. That digital inputs should not be allowed to float and either must be pulled up or down with something like a 10k resistor.
Here's a place for getting started, Lady Ada's Arduino Tutorial can help you get started with basic programming and electronics.

Since the stock Arduino includes an LED tied to ground throught a resistor on pin 13, the usual first sketch you run is one that simply blinks that LED.

It's sort of the print "Hello, World" to STDIO that just about every programmer creates, compiles and executes as an introductory program for just about every language out there. In our instance it's easier to blink the LED. And... you've already done it long ago and are tired of just blinking LED's.

This sketch monitors an input pin using attachInterrupt() to read an input pin when the logic level changes. We create an interrupt handler whose sole purpose is to run a digitalRead() on that pin, capture the value and store it to a volatile variable.

In our loop(), we can use this captured value to do just about anything we so desire (trigger an alarm, click a solenoid, signal processing to decode data, I'll leave that fun stuff to you) and in addition, reflect the input activity through an LED activity indicator.

Sketch Interrupt Blink
/***************************************************************************
* Interrupt Blink by capt.tagon
*
* Monitor input on Pin 2, Interrupt 0 for activity. Interrupt routine
* processes signal and reflects input signal state through LED on pin 13.
*
* Flashes pin 13 in synch with input on pin 2
***************************************************************************/

#define inputPin 2 // input pin for monitored input signal
#define ledPin 13 // output pin for LED signal indicator

volatile boolean inputState; // volatile variables, may be changed
boolean prevInputState; // at any time by interrupt handler

void setup() {
pinMode(inputPin, INPUT); // set pin for digital input
pinMode(ledPin, OUTPUT); // set pin for digital output
attachInterrupt(0, readLevel, CHANGE); // attach external interrupt handler
}

void loop() {
if (inputState != prevInputState) {
// if signal changed, process input signal change
}
prevInputState = inputState; // store input signal state
}

/******************************************************************************
* readLevel() {
*
* Pin 2 INT0 Interrupt Handler Reads pin state - flashes signal indicator LED
******************************************************************************/

void readLevel() {
inputState = digitalRead(inputPin); // read input signal level
digitalWrite(ledPin, inputState); // flash activity indicator pin
}

No comments:

Post a Comment