I am not sure what you need but here is code that fades a LED up and down. It does not block your code by using Delay(), You can paste it into your Loop{} or call it as a function. You may wish to play with the int period value and the +/-5 value. Do note which i/o pins you can use with this PWM code.
//use pins 3, 5, 6, 9, 10, and 11 on the UNO
const int ledPin = 3; //whichevery pin you choose Do not forget the resistor!
int period = 10; //how fast to fade the LED:100 = 1/10 sec. etc
unsigned long time_now = 0; //have not read the time yet
int brightness = 0; //start with LED off
boolean IncreaseBrightness = true; //start with increasing brightness
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600); //you may have this; used here for testing
}
void loop()
{
// non-blocking delay
if(millis() > time_now + period) //get current time and compare
{
time_now = millis(); //for next time
//fade LED up or down
if(IncreaseBrightness)
{
analogWrite(ledPin, brightness +=5 );
if(brightness >= 255) IncreaseBrightness = false; //max ouput to LED = 255
}
else //must be decreasing brightness
{
analogWrite(ledPin, brightness -=5 );
if(brightness <= 0 ) IncreaseBrightness = true; //min ouput to LED = 0
}
}
//some code...testing displays values.....
Serial.print("brightness "); Serial.println(brightness);
}