Author Topic: Arduino Weirdness.  (Read 5509 times)

0 Members and 1 Guest are viewing this topic.

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Arduino Weirdness.
« on: May 18, 2011, 07:06:54 am »
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: [Select]
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);
  }
}

I am use Arduino Version 0022 on Ubuntu 11.04.

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

Regards
 
Testing one two three...
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9954
  • Country: nz
Re: Arduino Weirdness.
« Reply #1 on: May 18, 2011, 08:28:54 am »
The first for loop looks wrong
"for (PWMwidth = 0; 255; PWMwidth++)"

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

« Last Edit: May 18, 2011, 08:32:24 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Re: Arduino Weirdness.
« Reply #2 on: May 18, 2011, 09:03:45 am »
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
Testing one two three...
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9954
  • Country: nz
Re: Arduino Weirdness.
« Reply #3 on: May 18, 2011, 09:23:06 am »
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
« Last Edit: May 18, 2011, 09:35:59 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline firewalker

  • Super Contributor
  • ***
  • Posts: 2450
  • Country: gr
Re: Arduino Weirdness.
« Reply #4 on: May 18, 2011, 09:25:45 am »
Is the pin that the LED is connected PWM capable?
Become a realist, stay a dreamer.

 

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Re: Arduino Weirdness.
« Reply #5 on: May 18, 2011, 09:40:43 am »
Many thanks Psi, i didnt think of that. I canged it and it works... ;D

Regards
Testing one two three...
 

Offline johnboxall

  • Supporter
  • ****
  • Posts: 652
  • Country: au
  • You do nothing, you get nothing.
    • Books, services and more:
Re: Arduino Weirdness.
« Reply #6 on: May 19, 2011, 04:00:37 am »
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.
Hi PeterG
If you are starting with Arduino, you may find my tutorial series of interest:
http://tronixstuff.wordpress.com/tutorials/
Have fun!

Offline thakidd

  • Regular Contributor
  • *
  • Posts: 56
Re: Arduino Weirdness.
« Reply #7 on: May 19, 2011, 06:27:52 am »
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.
Hi PeterG
If you are starting with Arduino, you may find my tutorial series of interest:
http://tronixstuff.wordpress.com/tutorials/
Have fun!

tronixstuff: checked out your site a while back. great work! the site is chock full of stuff any beginner can go to town and back with.
 

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Re: Arduino Weirdness.
« Reply #8 on: May 19, 2011, 09:20:58 am »
tronixstuff: Yes i have found your site very useful. Well done making such a helpful site.

Regards
Testing one two three...
 

Offline johnboxall

  • Supporter
  • ****
  • Posts: 652
  • Country: au
  • You do nothing, you get nothing.
    • Books, services and more:
Re: Arduino Weirdness.
« Reply #9 on: May 24, 2011, 06:49:27 am »
tronixstuff: Yes i have found your site very useful. Well done making such a helpful site.
Regards
Thanks PeterG. This week I discuss video out, it is very simple to use.


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf