down the rabbit hole: what's the difference between main() and loop()?

Thread Starter

mikewax

Joined Apr 11, 2016
184
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
Code:
#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:

Thread Starter

mikewax

Joined Apr 11, 2016
184
You are mixing C and "Arduino language". That is the cause of your failure.
yes i see what you mean but the basic problem is that it does work when i use the "int main()" declaration. the compiler does what it's supposed to do, which it clearly isn't doing when i use the "setup". so if this is the only way to make it work then i don't have a choice.
 

WBahn

Joined Mar 31, 2012
30,077
So which version of the code are we seeing?

While Arduino may work differently, nearly all C compilers require that there be exactly one function called "main" and it is that function that will be executed when the program starts.

Assume that somehow, someway, the compiler is able to magically guess that you want to execute the setup() function first. Notice that the loop() function is never called! But leaving that aside, walk through your setup() and do exactly what it says to do -- nothing more and nothing less. Does that sequence of steps result in the LED coming on and staying on? If so, then it should be no surprise that that is what actually happens because that is exactly what your program is telling the system to do.
 

Thread Starter

mikewax

Joined Apr 11, 2016
184
But this is how i've always done it. when i run arduino 1.6.12 IDE and i click file...new, it opens a new editor window and here is the default text in the new 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. with countless weenie programs. and i've never had a problem with it until now. that's the standard weenie syntax. plus the setup routine doesn't include the statement for the duty cycle, the "OCR1B = x%50;". if it's not compiling the loop part, there must be a reason why it's not. the compilers used by the weenie IDE are avr-g++, avr-gcc, and avr-gcc-ar. but i dunno about compilers. so the problem remains a mystery.
 

WBahn

Joined Mar 31, 2012
30,077
Well, it sounds like the Arduino IDE is doing some things behind the scene (probably constructing something like

Code:
void main(void)
{
   setup();
   while(1)
      loop();
}
But I can only guess. So I have no idea why your code isn't working since I don't do Arduino. Hopefully someone that does can shed some more light.
 

Thread Starter

mikewax

Joined Apr 11, 2016
184
yeah so it seems. but i just got an STM discovery board in the mail so maybe i'll just end the nightmare and move on. Thanx 4 the input :)
 

ClassOfZero

Joined Dec 28, 2016
114

AlbertHall

Joined Jun 4, 2014
12,347
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.
You only show the code that worked for you. Show us the code that you used for the setup/loop which didn't work so we can work out why it didn't work.
 

eetech00

Joined Jun 8, 2013
3,960
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
Code:
#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++){} } }
Your C code isn't properly structured. In a C program, the main() code block is automatically called at program startup.
Functions (like loop()) should be called from the main() code block.

Code should be structured similar to this:
----------------------------------------------
// Include's
// Constant's

// Variable Declaration's
int var = 0;

// Function prototype's
void function1(void);
int function2(void);

int main (void) {
statement1;
statement2;
function1();
var1 = function2();
}

void function1(void) {
statement1;
statement2;
}

// function2 returns an integer value
int function2(void) {
statement1;
statement2;
return 1;
}
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
eetech: that may be a a typo or autocorrect but "main()" is all lower case.

I believe there is a compiler directive to ignore case, but I would never think of turning such on.

Oh, and function2 is not declared. :)

Edit: sample code fixed (and what a fine job too!)
 
Last edited:

eetech00

Joined Jun 8, 2013
3,960
eetech: that may be a a typo or autocorrect but "main()" is all lower case.

I believe there is a compiler directive to ignore case, but I would never think of turning such on.

Oh, and function2 is not declared. :)
:eek:Yes...your right....they are both typos...i'll fix the post.
 
Top