Show Posts
Pages: 1 2 3 [4]
46  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 09, 2012, 12:34:44 pm
A for loop without its i++ at the end looks strange to me :-)

I'd use a direction variable with values +1 or -1 and would add that to the current pwm value.

Code:
int i;
int pwm = 0;
int direction = +1;    // either +1 or -1

analogWrite(pin, pwm);

for (i = 0; i < 255; i++) {
    pwm += direction;
    analogWrite(pin, pwm);
    delay(d);    // see "blink without delay" example for how to get rid of this!
}
direction = -direction;

If direction is greater that 1 then 255 needs to be decreased accordingly.

HTH

That looks a lot better then mine.. thanks
47  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 09, 2012, 11:10:48 am
You're not going to break anything by uploading code, unless you've miswired your LED, but you might want to go and look at how "for" loops are structured.

PWM pins don't need to have their pinMode set.

So I dont need anything in "void setup"?

My for loop ahhhhh...... yep I see it now, thanks.

Better?
Code:
//////////////////////////////////////////////////////////////////
//By: S_Flex
//Date: 11/09/2012
//Fade PWM output
//////////////////////////////////////////////////////////////////

#define fadePin3  3
#define DelayTime 15

int ThisLoop = 0; // current loop style 0 for ++ and 255 for --
int NextLoop = 255;

int temploop = 0;

void setup(){
  //pinMode(fadePin3, OUTPUT);
}

void loop(){

  // Fade pin loop
  for(int i = ThisLoop; i<NextLoop;){
    analogWrite(fadePin3, i);
    delay(DelayTime);
    if (ThisLoop == 0) i++;
    if (ThisLoop == 255) i--;
  }
  // Set next loop
  temploop = ThisLoop;
  ThisLoop = NextLoop;
  NextLoop = temploop;   
}
48  Using Arduino / Programming Questions / PWM - Fade a pin on: November 09, 2012, 10:34:01 am
I am a noob with C/C++, But i have done some Perl in my day and can understand a lot of the programing languages out there.

This is my first code I ever made for an Arduino Leonardo that verified with no error's, but will it work the way I think it should?
It should be another way of doing http://bildr.org/2012/03/rfp30n06le-arduino/
Using PWM to fade a pin from 0 to 255 and 255 to 0.
I don't want to upload it and it brake my new toy.

Code:
//////////////////////////////////////////////////////////////////
//By: S_Flex
//Date: 11/09/2012
//Fade PWM output
//////////////////////////////////////////////////////////////////

#define fadePin3  3
#define DelayTime 15

int ThisLoop = 0; // current loop style 0 for ++ and 255 for --
int NextLoop = 255;

void setup(){
  pinMode(fadePin3, OUTPUT);
}

void loop(){

  // Fade pin loop
  for(int i = ThisLoop; i<NextLoop;){
    if (ThisLoop == 0) i++; // Will this work? Or should I do if & else?
    if (ThisLoop == 255) i--;
    analogWrite(fadePin3, i);
    delay(DelayTime);
  }

  // Set next loop
  if(ThisLoop == 0) {
    ThisLoop = 255;
    NextLoop = 0;
  }
  else if (ThisLoop == 255) {
    ThisLoop = 0;
    NextLoop = 255;
  }
}
Pages: 1 2 3 [4]