#include <serialStr.h>
#include <timeObj.h>
#include <mapper.h>
#define RED_PIN 9
#define GREEN_PIN 10
serialStr serialMgr; // serialStr does the work of getting commands for us.
timeObj frameTimer(250); // Time between changes. NON BLOCKING.
mapper pwmMapper(-1,1,0,255); // Get the values to match analog out.
float angle; // Current angle.
float step; // Step size.
float newTime; // Holds the new time to wait.
void setup() {
Serial.begin(9600); // Fire up serial stuff.
Serial.println("Type in a value (>0) for the delay in ms between calcualtions.");
serialMgr.setCallback(gotStr); // When a string arrives, call this function with it.
pinMode(RED_PIN, OUTPUT); // Do the pin thing.
pinMode(GREEN_PIN, OUTPUT); // Do the pin thing, again.
angle = 0; // What angle we looking at now?
step = 2*PI / 48; // We'll do 48 steps
newTime = 0; // Flag, no new time.
}
// When the serialMgr gets a complete string, it passes it in here.
void gotStr(char* inStr) { newTime = atof(inStr); }
void loop() {
float redVal;
float greenVal;
idle(); // idle() runs things like the serialStr.
if (frameTimer.ding()) { // Time to recalcualte?
angle = angle + step; // Chunk over the angle.
if (angle>2*PI) angle = 0; // Too far? Reset to zero.
redVal = sin(angle); // calc red
greenVal = sin(angle+PI); // calc green
redVal = pwmMapper.map(redVal); // Map red to ananlong out value.
greenVal = pwmMapper.map(greenVal); // Map green to ananlong out value.
//Serial.print(redVal); // If you want to graph the values.
//Serial.print('\t'); // Seperator.
//Serial.println(greenVal); // Other value.
analogWrite(RED_PIN,round(redVal)); // Set the analog out to the result.
analogWrite(GREEN_PIN,round(greenVal)); // Both of them.
if (newTime) { // If we have a new time..
frameTimer.setTime(newTime); // Set the new time into the timer.
newTime = 0; // Clear newTime. (Means no new time)
} else { // Else no new time to deal with.
frameTimer.stepTime(); // Just restart the timer.
}
}
}