Hey guys,
I've been programming arduino stuff for a short time ( about 3 months ) and have recently come across a problem in a very simple program.
The following code is supposed to dim and brighten 3 led's according to a sine wave. It works fine for 10 - 20 cycles of the dimming/brightening then becomes really choppy, then it's okay again for a while, then choppy again. Does anyone know what the problem might be?
#include <stdio.h>
int redPin = 9;
int greenPin = 6;
int bluePin = 3;
// char serialText[512];
int outputValue;
int startTime;
int now;
float sinInputValue;
void setup() {
pinMode( redPin, OUTPUT );
pinMode( bluePin, OUTPUT );
pinMode( greenPin, OUTPUT );
// Serial.begin( 9600 );
outputValue = 0;
startTime = millis();
now = millis();
}
void loop() {
now = millis();
led_getNewOutputValue( now - startTime );
// sprintf( serialText, "outputValue : %d", outputValue );
// Serial.println( serialText );
analogWrite( redPin , outputValue );
analogWrite( greenPin,outputValue );
analogWrite( bluePin, outputValue );
delay(10);
}
int led_getNewOutputValue( unsigned long millisPassed ) {
sinInputValue = ((float) millisPassed) / 500.0;
outputValue = (int) ( ( sin( sinInputValue ) + 1 ) / 2.0 * 255.0 );
}