Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

what's the difference between main() and loop()?

Status
Not open for further replies.

mikewax

Member
Here's a little mystery that has cost me about 6 days of grief. the following code uses pwm to fade an LED up and down. when i organize the code using the "main(void){....}" declaration, it works as written. But when i divide the code into two segments with "void setup(){....}" and "void loop(){....}" the LED doesn't fade in and out. It just comes on 100% and stays that way.
any ideas about why this is? thanx

#include <avr/io.h>
// fastPWM on timer1, non-invert mode, no prescale, output on pin B2, Duty cycle = (x mod 50)/50
volatile unsigned int t, u, x = 0;
void wait(unsigned int base);

//void setup() {
int main(void) {
ICR1 = 50;
TCCR1A |= (1 << COM1B1) | (1 << WGM11);
TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10);
DDRB |= (1 << DDB2);
//} void loop() {
while(1){ x += 4; OCR1B = x%50; wait(600); }
return 0;
}

void wait(unsigned int base){ for (t = 0; t < base; t++){ for (u = 0; u < base; u++){} } }
 
Last edited:
I'm not a programmer, but I think you have to have main, and any other functions are called from within there. So when you just have the setup and loop functions they don't run because there's no main. Presumably the output which goes to the LED defaults to a high state.
That's about my limit, hope it helps!
 
when using arduino IDE programmer i use setup and loop, I have only seen this program do that
when I program pic...or windows i use main

... whatever it is, it needs to be what ever the compiler identifies with as the "main"
 
Last edited:
When using C or C++ there must always be a main()... When using the Arduino development system the skeletal program is the same...

C:
void main(void)
   {
   setup();
   loop();
   }

You only ever get to see the two functions inside the main()..
 
when i run arduino 1.6.12 compiler and i click file...new, it opens a new editor window and here is the default text in the window:

void setup() {
// put your setup code here, to run once:
}

void loop() {
// put your main code here, to run repeatedly:
}


and that structure is what i've always used. setup and loop. and i've never had a problem with it until now. but timer1 won't operate with the setup loop declarations. it will only work when i use int main(). so i changed my program to use the int main() declaration. and guess what? timer2 won't work now. timer1 will only work when i use int main and timer2 will only work when i use setup loop. whatthehell did i do to deserve this?
 
Maybe it's time to delete the file or even the project and do it over. Just copy the code into temporary files so you can copy and paste it back in to the new files.

I encountered a similar thing with mplabx recently, the problem was due to the template I'd used, in combination with lack of knowledge of how to put it right. Choosing a different template made the problem go away.
 
I may not appear to be clear on this...

The Arduino environment only uses setup() and loop()... When a sketch is compiled, the pre compiler wraps your code into a main() function and then uses the main compiler to create a hex... This is so the environment is as easy as can be..

You do not see this step but it is done none the less... If you include a main() within the Arduino environment you will inevitably cause issues...
 
oh so that's their way of sorta dumbin it down. ok well i'll check out the compiled files and maybe find out what's up.
thanx

damn it just occured to me. i just gotta get a different compiler. do you know of any good ones?
 
Atmel studio 7 comes with one of the best... Free!!!
Hi, I got a copy of Atmel 7 from a person. But during installation it ask for Windows Update (two files). problem is I do not have internet. Just have expensive and poor GPRS in cellphone. I am using Windows7. Do anyone think that using Windows 8 or 10 do not ask for update?
 
i could email you the files if you have a way of retrieving them.
Hi, are you talking about the windows update two files I was talking about? If yes then send me here please: nepal.nepaliman(at)gmail.com
(at) = @
I will try to download.
 
Last edited:
I have come to really like the visual micro plugin for my visual studio 2013 and atmega2560

2013 is good size for my computer , and the plugin includes an interesting style of debugging (considering its for a chip that does not typically support debug)
 
that's funny when i installed atmel studio7 it downloaded several versions of visual studio while it was installing. and visual studio works for atmel too?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top