Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created April 15, 2016 01:49
Show Gist options
  • Select an option

  • Save dwblair/44f74036fd1c927b9fb0a6fe617feab7 to your computer and use it in GitHub Desktop.

Select an option

Save dwblair/44f74036fd1c927b9fb0a6fe617feab7 to your computer and use it in GitHub Desktop.
#include <TimerOne.h>
// This example uses the timer interrupt to blink an LED
// and also demonstrates how to share a variable between
// the interrupt and the main program.
const int led = 3; // the pin with a LED
int ledState = LOW;
// note: need to add amplification factor
void setup(void)
{
pinMode(led, OUTPUT);
Timer1.initialize(500);
Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
Serial.begin(9600);
}
// The interrupt will blink the LED, and keep
// track of how many times it has blinked.
volatile int flag = 0; //whether we're in the 'on' state
void blinkLED(void)
{
if (ledState == LOW) {
ledState = HIGH;
flag = 1;
//blinkCount = blinkCount + 1; // increase when LED turns on
} else {
ledState = LOW;
}
digitalWrite(led, ledState);
}
// The main program will print the blink count
// to the Arduino Serial Monitor
void loop(void)
{
/*
noInterrupts();
flag = 0; // write to the flag without conflicting with ISR writes
interrupts();
*/
// read A0
int val1 = analogRead(0);
// read A1
// int val2 = analogRead(1);
int val2 = 0; // just have dummy variable for now
// print to serial
Serial.print(val1);
Serial.print(" ");
Serial.print(val2);
Serial.print("\n");
// wait
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment