Questions tagged [sketch]

A sketch is compiled code that is uploaded to an Arduino board.

This tag should be used when writing code for the Arduino specifically using the Arduino IDE.

A sketch has the following structure:

void setup()
{
    ...
}

void loop()
{
    ...
}

The setup function is called once when the sketch starts. The loop function then is called repeatedly until the board is either shut down or restarted.

A sketch is saved with the .ino extension.


If you are used to more traditional C programming, effectively what is done by the above is:

int main ()
  {
  init ();  // set up hardware, particularly timers used by delay()
  setup (); // your own code, to be run once only
  while (true)
    loop ();  // do this repeatedly
  return 0;   // this is never executed
  }

It's a bit more complex than that, but that is the basic idea. Also the IDE lets you take control by writing your own main() function like this:

int main ()
  {
  ...  // do whatever you want here
  return 0;
  }

If you do that, the "normal" main is ignored.

258 questions
60
votes
7 answers

Is there any way to download a sketch from an Arduino?

I made a sketch, but then I lost it. However, I uploaded it to the Arduino before losing it. Is there any way I can get it back?
The Guy with The Hat
  • 4,962
  • 7
  • 28
  • 51
37
votes
12 answers

Arduino Nano uploading gives error: avrdude: stk500_recv(): programmer is not responding

I have a Arduino Nano (Sainsmart) that I'm trying to upload a sketch to. Under the Arduino IDE, the device selected was Arduino Nano w/ ATmega328. However uploading the sketch gives me the error avrdude: stk500_recv(): programmer is not…
Nyxynyx
  • 1,319
  • 4
  • 20
  • 25
20
votes
2 answers

Asynchronous function calls in Arduino sketch

In an Arduino sketch, is there a way to make asynchronous function calls within the loop? Like listening to requests through http server and process them in a non-blocking way.
Jacer Omri
  • 301
  • 1
  • 2
  • 6
19
votes
7 answers

Loading local libraries

I'm new to Arduino/C development (coming from a JavaScript/Ruby environment), but I was wondering if it was possible to include a library from a custom folder within a sketch? So this is my situation; project.ino libs/ MyNewLib/ MyNewLib.h …
Stefan
  • 291
  • 1
  • 2
  • 6
17
votes
5 answers

What happens if there is a runtime error?

What happens if there is a runtime error in a program? Will execution of the program just stop? Is there some way I can get the Arduino to tell me what the error is?
The Guy with The Hat
  • 4,962
  • 7
  • 28
  • 51
10
votes
4 answers

How to really shrink a sketch

I want to make a sketch that is as small as possible, for test purposes. The problem is, when i compile the BareMinimum sketch (with an empty setup and loop), i get 466 bytes for the Uno and a whopping 4,242 for the Leonardo. Is there any way to…
TheDoctor
  • 3,429
  • 1
  • 20
  • 39
8
votes
2 answers

Is it possible to run a binary from EEPROM?

Say I wrote a compiled sketch to EEPROM then read it. Could I run the program from EEPROM? I guess the question is: Can an Arduino run software not in flash memory in the middle of executing the software in flash?
Piper McCorkle
  • 183
  • 1
  • 5
7
votes
4 answers

How to add hysteresis to threshold values?

This code compares the analog input value against two thresholds, having three voltage regions. Then it will turn on an LED according to what region the read voltage is in. The problem is that when the voltage read is very close to the threshold,…
Andy
  • 103
  • 2
  • 2
  • 7
7
votes
4 answers

ATtiny85 minimum setup to blink led

What would be the (very) minimum schematic to make a ATtiny85 blink a led? Restrictions are: There must be a C program compiled and uploaded to it, just like an Arduino board would have. The code would use a simple "delay" to wait a little bit…
DrBeco
  • 269
  • 2
  • 8
6
votes
2 answers

Random(min,max) Function only giving ~50 range no matter what the values?

I can't get the min,max range to function properly. Monitoring the serial, only gives out ~50 difference from the min. I then added truerandom.ino to try to fix, but no help. const int ledPin = 13; // the number of the LED pin // Variables will…
Eagle1
  • 61
  • 1
  • 6
6
votes
1 answer

strange behaviour of dtostrf()

I'm trying to program a little bit in Arduino, but I'm stuck with probably something trivial. This is what I have: char ang[3], lat[9]; dtostrf(GPS.angle, 3, 0, ang); dtostrf(GPS.latitude, 9,5, lat); Serial.println(lat); …
stUrb
  • 341
  • 2
  • 5
  • 9
5
votes
1 answer

How to add the Wiring S board to Arduino IDE v.1.6.2?

My Arduino Uno is running out of memory to support my script (yes, I've tried several ways of optimize it already: PROGMEM, EEPROM, no Strings, and so on), so I would like to upload my script to my Wiring S board. However, instead of "porting" all…
5
votes
3 answers

Using Arduino as a standalone compiler

I mostly program in C/C++ so I conviniently have a GCC compiler for all my compilations. I was wondering if there is a special compiler for Arduino that can be used just like the GCC Compiler? Because just taking a .ino sketch and passing it to the…
5
votes
5 answers

Should I try to make my sketches as small as possible, even when I have enough room?

There has been a lot of talk about shrinking sketches recently, but if you don't need the room, should it be done? Will it speed up my program? Take this code: int led = 13; int val; void setup() { pinMode(led, OUTPUT); …
Anonymous Penguin
  • 6,155
  • 9
  • 31
  • 62
5
votes
2 answers

"stray '/302' in program error" when compiling

For some weird reason, the following code doesn't compile. I get a "stray '\302' in program" error around volatile unsigned int encoderPos = 0;, and I have no idea what the issue is. I've been trying to figure this out for over 40min, and nothing…
user2950509
  • 183
  • 1
  • 1
  • 6
1
2 3
17 18