Variable Metro
Repeat actions using the Metro library and change intervals at runtime.
/* MetroChanging
*
* basic example to learn not-so basic timing
* using the Metro library :
* http://www.arduino.cc/playground/Code/Metro
* SensorAktor Workshop
* Lab3 2008
* Kunsthochschule fuer Medien Koeln
* Academy of Media Arts Cologne
* http://interface.khm.de
* pin mapping SensorAktor Shield
* import SensorAktor.h to get already defined mapping
*
* analog inputs: analog0 = pin0, analog1 = pin1, analog2 = pin2,
* analog3 = pin3, analog4 = pin4, analog5 = pin5
* built-in poti: poti = pin5
* amplified input: mic = pin0
* switches: switch1 = pin1, switch2 = pin2, switch3 = pin3
* leds: led1 = pin3, led2 = pin5, led3 = pin6
* power outs: out1 = pin3, out2 = pin5, out3 = pin6
* motor outs: motor1_1 = pin8, motor1_2 = pin13, motor2_1 = pin11, motor2_2 = pin12
* motor pwms: pwmMotor1 = pin10, pwmMotor2 = pin9
* servo outs: servo1 = pin2, servo2 = pin4, servo3 = pin7
*/
// include sensor aktor library for pin mapping
#include <SensorAktor.h>
// include Metro library for timing actions
#include <Metro.h>
// create a Metro named ledMetro
Metro ledMetro = Metro(250);
// create a variable that knows if led1 is LOW or HIGH
int ledState = LOW;
// this block is executed one time when programm starts
void setup() {
pinMode(led1, OUTPUT);
// turn off the light
digitalWrite(led1, ledState);
}
// this block is executed in a loop after setup is called one time
void loop(){
// if the metro says it is time
if (ledMetro.check()) {
// check if the led is on or off and switch it accordingly
if (ledState == LOW) {
ledState = HIGH;
// leave the led on for some random ms, by changing the Metro interval
ledMetro.interval(random(100,1000));
} else {
ledState = LOW;
// leave the led off for some random ms, by changing the Metro interval
ledMetro.interval(random(100,1000));
}
// finally set led
digitalWrite(led1, ledState);
}
}
Files needed
Since Arduino 0015 there are some problems with the old “SensorAktor.h” file.
Use this one instead.
