Weird Delay

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 );
}

millis() returns an unsigned long, so startTime and now should have that type as well.

Thanks, I"ll try that out when I get home. This is why god invented pair programming. :slight_smile:

This is why god invented pair programming.

Maybe I'm just too "old school", but every time I read something about "agile development", the more I get the feeling it was something invented by managers who've never coded a day in their lives. "Pair programming" seems like the ultimate in micro-management to me...

:stuck_out_tongue: