Arduino Weirdness. - Page 1
Forum section: electronics / general

author: PeterG
author_rank: Frequent Contributor
timestamp: May 18, 2011, 07:06:54 am
content: 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);}}I am use Arduino Version 0022 on Ubuntu 11.04.Can anybody replicate this issue or am i doing something wrong?Regards

author: Psi
author_rank: Super Contributor
timestamp: May 18, 2011, 08:28:54 am
content: The first for loop looks wrong"for (PWMwidth = 0; 255; PWMwidth++)"Should be something like"for (PWMwidth = 0; PWMwidth <= 255; PWMwidth++)"

author: PeterG
author_rank: Frequent Contributor
timestamp: May 18, 2011, 09:03:45 am
content: 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

author: Psi
author_rank: Super Contributor
timestamp: May 18, 2011, 09:23:06 am
content: 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

author: firewalker
author_rank: Super Contributor
timestamp: May 18, 2011, 09:25:45 am
content: Is the pin that the LED is connected PWM capable?

author: PeterG
author_rank: Frequent Contributor
timestamp: May 18, 2011, 09:40:43 am
content: Many thanks Psi, i didnt think of that. I canged it and it works...Regards

author: johnboxall
author_rank: Unknown
timestamp: May 19, 2011, 04:00:37 am
content: Quote from: PeterG on May 18, 2011, 07:06:54 amHi PeterGIf you are starting with Arduino, you may find my tutorial series of interest:http://tronixstuff.wordpress.com/tutorials/Have fun!

author: thakidd
author_rank: Regular Contributor
timestamp: May 19, 2011, 06:27:52 am
content: Quote from: tronixstuff on May 19, 2011, 04:00:37 amtronixstuff: 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.

author: PeterG
author_rank: Frequent Contributor
timestamp: May 19, 2011, 09:20:58 am
content: tronixstuff: Yes i have found your site very useful. Well done making such a helpful site.Regards

author: johnboxall
author_rank: Unknown
timestamp: May 24, 2011, 06:49:27 am
content: Quote from: PeterG on May 19, 2011, 09:20:58 amThanks PeterG. This week I discuss video out, it is very simple to use.