Electronics > Microcontrollers

Arduino Weirdness.

(1/2) > >>

PeterG:
Hi all,
    i went and got myself a Arduino Mega1280 and started to play with the onboard LED. It did not take me long to find a weird outcome to what i thought was a simple LED app.

I tried to do a basic LED app using PWM to fade the LED in and out. As it turns out, it does not work well.

i am using the following code. I have attached the file as well.


--- Code: ---int PWMpin=13;
int PWMwidth=0;

void setup()
{               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(PWMpin,OUTPUT);
}

void loop()
{


  ledUP();
  delay(500);
  LedDown();
  delay(500);

}

void ledUP()
{
  //delay(100);
  for (PWMwidth = 0; 255; PWMwidth++)
  {
    analogWrite(13, PWMwidth);
    delay(10);
  }
}

void LedDown()
{
  //delay(100);
  for (PWMwidth = 255; PWMwidth>0; PWMwidth--)
  {
    analogWrite(13, PWMwidth);
    delay(10);
  }
}
--- End code ---

I am use Arduino Version 0022 on Ubuntu 11.04.

Can anybody replicate this issue or am i doing something wrong?

Regards
 

Psi:
The first for loop looks wrong
"for (PWMwidth = 0; 255; PWMwidth++)"

Should be something like
"for (PWMwidth = 0; PWMwidth <= 255; PWMwidth++)"

PeterG:
The strange thing is, if i call either sub by itself, it works fine. But if i enable both as it is in the file, it runs the first sub but not the second.
The demo file that is similar works well.

Regards

Psi:
With the 'test' part of the for loop set to 255 the test will always be true so the loop will work but never finish.

The function may appear to work but it's actually stuck with the variable PWMwidth getting bigger and bigger. Probably wrapping around to zero at some point and starting again.

I suspect that's why it seems to work when you try the LedUp function by itself.

When you try LedUp and LedDown together it will get stuck inside LedUp and never get to LedDown

firewalker:
Is the pin that the LED is connected PWM capable?

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version