Lab3 - Pulse rate for 3 LEDs.
I like blinky lights but to be able to control the blink rate of all 3 LEDs independently with the potentiometers the use of the delay function shown in the example sketches results in unsatisfactory results when more than one LED is needed to be controlled or other operations need to take place such as reading an analogSensor.
To circumvent the inherent limitation of the delay function an alternative method was used to measure time between HIGH and LOW states of the LEDs, Millis and a Long int to store the last time the LED blinked previousMillis0. Since 3 LEDs where desired to be controlled 3 sets of storage integers where needed as well as 3 intervals which equal the analogRead of the potentiometers.
Like always credits must go to the author of the original code, David A, Mellis.
if(currentMillis - previousMillis0 > interval0) { // save the last time you blinked the LED previousMillis0 = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState0 == LOW) ledState0 = HIGH; else ledState0 = LOW; // set the LED with the ledState of the variable: digitalWrite(ledPin0, ledState0); }
Components
1 - Arduino
3 - LEDs
3 - 220 Ohm resistors
3 - 10k potentiometers
Assortment of patch cables.
Code:Arduino
/* Blink without Delay Turns on and off a light emitting diode(LED) connected to a digital pin, without using the delay() function. This means that other code can run at the same time without being interrupted by the LED code. The circuit: * LED attached from pin 13 to ground. * Note: on most Arduinos, there is already an LED on the board that's attached to pin 13, so no hardware is needed for this example. created 2005 by David A. Mellis modified 8 Feb 2010 by Paul Stoffregen This example code is in the public domain. http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay */ // constants won't change. Used here to // set pin numbers: const int ledPin0 = 9; const int ledPin1 = 10; const int ledPin2 = 11; // the number of the LED pin // Variables will change: int ledState0 = LOW; int ledState1 = LOW; int ledState2 = LOW;// ledState used to set the LED long previousMillis0 = 0; // will store last time LED was updated long previousMillis1 = 1; long previousMillis2 = 2; // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long interval0 = 1000; long interval1 = 1000; long interval2 = 1000;// interval at which to blink (milliseconds) void setup() { // set the digital pin as output: pinMode(ledPin0, OUTPUT); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); } void loop() { // here is where you'd put code that needs to be running all the time. // check to see if it's time to blink the LED; that is, if the // difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to // blink the LED. long interval0 = analogRead(A0); long interval1 = analogRead(A1); long interval2 = analogRead(A2); unsigned long currentMillis = millis(); if(currentMillis - previousMillis0 > interval0) { // save the last time you blinked the LED previousMillis0 = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState0 == LOW) ledState0 = HIGH; else ledState0 = LOW; // set the LED with the ledState of the variable: digitalWrite(ledPin0, ledState0); } if(currentMillis - previousMillis1 > interval1) { // save the last time you blinked the LED previousMillis1 = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState1 == LOW) ledState1 = HIGH; else ledState1 = LOW; // set the LED with the ledState of the variable: digitalWrite(ledPin1, ledState1); } if(currentMillis - previousMillis2 > interval2) { // save the last time you blinked the LED previousMillis2 = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState2 == LOW) ledState2 = HIGH; else ledState2 = LOW; // set the LED with the ledState of the variable: digitalWrite(ledPin2, ledState2); } }
- Login to post comments