Errors in compiling with Atmel Studio 6

I’m trying to compile the basic Blink program using Atmel Studio 6. I’ve followed engblaze’s tutorial to setup the project in Atmel Studio 6.1. However, I still get errors.

Here is the code,

#include <avr/io.h>
#include <Arduino.h>
#include <pins_arduino.h>

int main(void)
{
	void setup(void);
	void loop(void);
	int led = 13;
	void setup() 
	{                
	  pinMode(led, OUTPUT);     
	}
	void loop() 
	{
	  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
	  delay(1000);               // wait for a second
	  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
	  delay(1000);               // wait for a second
	}
}

And here are the error and warning messages,

Error	1	a function-definition is not allowed here before '{' token	C:\Hrishis Documents\Atmel Studio\6.1\ArduinoBlink\ArduinoBlink\ArduinoBlink.cpp	27	2	ArduinoBlink
Error	2	a function-definition is not allowed here before '{' token	C:\Hrishis Documents\Atmel Studio\6.1\ArduinoBlink\ArduinoBlink\ArduinoBlink.cpp	32	2	ArduinoBlink
Warning	3	unused variable 'led' [-Wunused-variable]	C:\Hrishis Documents\Atmel Studio\6.1\ArduinoBlink\ArduinoBlink\ArduinoBlink.cpp	24	6	ArduinoBlink

Whats going wrong here? Any help would be appreciated. Thanks.

Your mistake is that you have tried to define setup() and loop() inside main(). Try a structure more like this:

int main()
{
  setup();
  for (;;)
  {
    loop();
  }
}

void setup()
{
  ...
}

void loop()
{
  ...
}

But make sure the the functions are decleared before use!.

Mark

Good point, Mark! hrishib, you can either move function main() to the end of the program, or declare:

void setup();
void loop();

before main().

He had the function prototypes in main(). Strange place for them, though.

Thanks dc42! However, I still get the same errors :frowning: I know these are really basic questions but I’ve just started out with C and C++ and I really appreciate your help!

Here is the updated code,

#include <avr/io.h>
#include <Arduino.h>
#include <pins_arduino.h>

void setup();
void loop();

int main(void)
{
	int led = 13;

	void setup(void)
	{
		pinMode(led, OUTPUT);
	}

	void loop(void)
	{
		digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
		delay(1000);               // wait for a second
		digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
		delay(1000);               // wait for a second
	}
}

The problem in your second code is that you actually put the functions inside main(). The example shown to you was just a declaration of a function… it was

void setup(void);

which only shows the return value datatype and the argument(s) data type.

I wonder, though, why you are writing it with main().

A sketch with only a setup() and loop() is really all you need.

You moved the prototypes, not the functions. It should be:

#include <avr/io.h>
#include <Arduino.h>
#include <pins_arduino.h>

int led = 13;

void setup(void)
{
  pinMode(led, OUTPUT);
}

void loop(void)
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

int main(void)
{
  init ();
  
  while (true)
    loop ();
}

Thanks Nick. Strangely this didn't work in a C++ environment in Atmel Studio, but worked just fine when I created a .c project instead.