#include <blinker.h>
#include <mapper.h>
#define MIN_DCYCLE 0 // Percent minimum dutycycle. (Close to 0)
#define MAX_DCYCLE 100 // Percent full dutycycle.
#define PERIOD 250 // Wave length in Ms. ("smaller in real life")
blinker aBlinker(2); // The blinker runs the square wave.
mapper dCycleMapper(0,1023,MAX_DCYCLE,MIN_DCYCLE); // Mapping the POT value to a percent.
float dCycle;
void setup() {
Serial.begin(9600); // So we can see what's going on.
dCycle = -1; // A number we can't get to.
readPOT(); // Do the initial POT read.
aBlinker.setPeriod(PERIOD); // Set up the period of the square wave.
aBlinker.setPercent(dCycle); // Set the initial percent of the square wave.
aBlinker.setOnOff(true); // Fire it up the square wave.
}
// This guy reads the POT and if there is a change? It updates
// the global value and returns true. Else it returns false.
bool readPOT(void) {
float localRead;
localRead = dCycleMapper.map(analogRead(A0)); // Grab and map the reading.
if (dCycle!=localRead) { // If there is a change..
dCycle = localRead; // Update the global value.
return true; // Tell the world!
} else { // Else..
return false; // Let 'em know there's no change.
}
}
void loop() {
idle(); // Run the background stuff.
if (readPOT()) { // If we read a change in the POT..
aBlinker.setPercent(dCycle); // Set the duty cycle of the blinker (square wave)
Serial.print("Duty cycle :"); // Debugging print.
Serial.print(dCycle);
Serial.println("%");
}
}
Period