Debugging my arduino sketch

is there a built in debugger? if not how can i debug my code?

is there a built in debugger?

No

not how can i debug my code?

Use Serial.print() to output messages indicating which parts of the program are being executed and the value of pertinent variables at those points.

You need to use the Serial object. Example:

void setup() {
  // put your setup code here, to run once:
  int testVal = 10;
  float x = 3.145;
  
  Serial.begin(9600);
  Serial.print("My test value = ");    // Display a string constant
  Serial.println(testVal);             // Show a value and then print a new line  
  Serial.print("Pi is = ");   
  Serial.println(x);           

}

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

}

Note that the Serial object rounds floating point numbers by default. Google "Printing floating point numbers with Arduino" for more info.

The code you showed as an example all the serial printing is done in the setup(), what if the serial.print() was done in the loop ? especially if the values are being updated after every iteration and i have to keep track of it?

Well yeah, for things from loop you'd print them from loop. What problem were you having with print statements in loop?

harris47naeem:
The code you showed as an example all the serial printing is done in the setup(), what if the serial.print() was done in the loop ? especially if the values are being updated after every iteration and i have to keep track of it?

If loop() is iterating very quickly (as it should) it will be impractical to print data for every iteration. I reckon you have two options. Either temporarily add a delay(200); into loop() to slow things down, or arrange only to print things every half second or so. If necessary you could save values from every iteration of loop() into an array and pause to print the array when it is full.

...R

There IS a debugger option, though it is non-trivial.

Depending on your chip and features it supports, you can get a hardware debugger (Like AVRDragon as a cheap option) and using Atmel Studio (Windows only) you can do some debuger-style activity (stepping through code, examining memory, breakpoints, etc) using either JTAG or DebugWire interface (depending on your Arduino)

So far this has not netted great results, but it kinda works, as long as you are in windows and can get your code to compile under Atmel Studio...

-HH

harris47naeem:
The code you showed as an example all the serial printing is done in the setup(), what if the serial.print() was done in the loop ? especially if the values are being updated after every iteration and i have to keep track of it?

Wouldn’t the best way to answer this question be for you to type some code in loop() and find out for yourself. It’s one of those give-a-fish-teach-to-fish things.

Did it, i seem to get it now, thanks all.