Página 9 - Primeros pasos con la Tarjeta Arduino

//
initialize the serial port; needed for debugging below
Serial.begin(9600);
//
initialize the LED pin
pinMode(ledPin, OUTPUT);
//
initialize the input pin
pinMode(buttonPin, INPUT);
}
void loop() {
//
for all timers
currentTime = millis();
//
checks to see if the number of milliseconds has passed
if ( abs(currentTime - fadeTimerLast) >= fadeTimerFreq) {
fadeTimerLast = currentTime;
//
read the value from the input
buttonValue = digitalRead(buttonPin);
//
step the fading
if(buttonValue == 1){
//
if the button is pressed, increase the fade
fadeValue = fadeValue + fadeStep;
}
else{
//
if the button is not pressed, decrease the fade
fadeValue = fadeValue - fadeStep;
}
//
constrain the fadeValue so it can't go off toward infinity
fadeValue = constrain(fadeValue, 0, fadeRange);
//
get the tweened value -- i.e. the smooth value
fadeValueTweened = Quad_easeInOut(fadeValue, 0, fadeRange);
//
use the tweened value to set the brightness of the LED
analogWrite(ledPin, fadeValueTweened);
//
print the values to the serial port for debugging
Serial.print(buttonValue);
Serial.print(", ");
Serial.println(fadeValue);
}
}
//
Quad easing thanks to Robert Penner
//
variables used are type "float" so that you can throw smaller numbers at it and it will still work well
float Quad_easeInOut(float t, float fixedScaleStart, float fixedScaleEnd){
//
float b = 0, c = 1, d = 1;
float b = fixedScaleStart;
float c = fixedScaleEnd - fixedScaleStart;
float d = fixedScaleEnd;
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
}