Questions tagged [programming]

The process of designing and writing source code as part of a program (or sketch) for Arduino. For questions about uploading code to an Arduino board, use the [uploading] tag instead.

Filter by
Sorted by
Tagged with
91
votes
4answers
68k views

How can I handle the millis() rollover?

I need to read a sensor every five minutes, but since my sketch also has other tasks to do, I cannot just delay() between the readings. There is the Blink without delay tutorial suggesting I code ...
88
votes
22answers
16k views

What are the other IDEs for Arduino?

The basic Arduino IDE lacks a lot of the sophistication present in other IDEs such as code completion, code collapsing, folder organisation, etc. Are there other IDEs that allow programming in C or C++...
52
votes
8answers
87k views

Is using malloc() and free() a really bad idea on Arduino?

The use of malloc() and free() seems pretty rare in the Arduino world. It is used in pure AVR C much more often, but still with caution. Is it a really bad idea to use malloc() and free() with ...
39
votes
9answers
158k views

Programming an Arduino using Python, rather than C/C++

I am not very skilled with the C Language and I was wondering if there is a way in which python could be used to program an Arduino. This would most likely require a different IDE in order to be able ...
29
votes
3answers
51k views

Is it better to use #define or const int for constants?

Arduino is an odd hybrid, where some C++ functionality is used in the embedded world—traditionally a C environment. Indeed, a lot of Arduino code is very C like though. C has traditionally used #...
26
votes
12answers
102k views

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 ...
22
votes
2answers
15k views

Why can't I declare a class in another tab in Arduino IDE?

I wanted to move some of my code out into a second tab in the Arduino IDE, to keep things better organised. At first, I only tried moving a function, and it seemed to work fine. I could call the ...
21
votes
5answers
75k views

What overheads and other considerations are there when using a struct vs a class?

C on embedded systems has traditionally use structs to hold structured data. Arduino brings C++ to the table, so we can use classes instead. Lets say we have two different data structures which ...
20
votes
3answers
7k views

Classes and objects: how many and which file types do I actually need to use them?

I have no previous experience with C++ or C, but know how to program C# and am learning Arduino. I just want to organize my sketches and am quite comfortable with the Arduino language even with its ...
19
votes
5answers
7k views

How much can I recurse? How much can I recurse? How much ca!@#QFSD@$RFW

The Arduino Uno board has limited RAM which means it has a limited call stack available. Sometimes, recursion is the only quick option to implement a certain algorithm. So, given that the call stack ...
19
votes
2answers
12k views

Would an infinite loop inside loop() perform faster?

When you're writing a typical sketch, you usually rely on loop() being called repeatedly for as long as the Arduino is running. Moving in and out of the loop() function must introduce a small overhead ...
18
votes
5answers
71k views

deprecated conversion from string constant to 'char*'

What does this error means? I can't solve it in any way. warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
18
votes
5answers
155k views

How can I declare an array of variable size (Globally)

I'd like to make three arrays of the same length. According to the documentation, Arrays must be defined as int myArray[10]; where 10 can be substituted for a known length (another integer), or filled ...
17
votes
3answers
8k views

What is the best way to unit test my code without controller? [duplicate]

I want to unit test my Arduino code. Ideally, I want to execute and test my code without uploading it to the board. What tools or libraries can help me with this? There is an Arduino emulator in ...
17
votes
2answers
24k views

What happens when I call exit() from my program?

In either of setup or loop, if I were to add an exit(0) call, where would control be passed to? What would the next state of the microcontroller be? Would it stop execution and power down? I am using ...
17
votes
6answers
4k views

How can Arduino source code be tested automatically (continuous integration)?

I'd like manage my Arduino source code (projects and libraries) under source control with continuous testing. How can I automatically compile the code with continuous integration tools, to make sure ...
17
votes
2answers
29k views

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.
14
votes
5answers
6k views

Programming options other than C++

Can I program my Arduino using any programming language other than C++? If so, which programming languages? What software would I need for compiling and loading my code onto the Arduino?
13
votes
5answers
19k views

Reducing lag between the arduino and a processing sketch on my computer

I'm currently on project #14 of the Arduino project book. I'm trying to control a processing sketch on my laptop using my Arduino. This is accomplished by using a potentiometer to control the ...
13
votes
4answers
4k views

Getting a truly random number in Arduino

What is the best method to get a truly (as opposed to pseudo) random number in Arduino, or at least the best possible approximation? From my understanding, the function randomSeed(analogRead(x)) it's ...
13
votes
4answers
7k views

Is setup and loop provided for convenience?

Arduino sketches usually feature a setup and loop function. Are these functions only provided for convenience or do they actually have special purposes? (e.g.: are some operations disallowed or ...
13
votes
4answers
11k views

Why do sketches take up so much space and memory?

When I compile this sketch for the Yún: int led = 7; void setup() { pinMode(led, OUTPUT); } void loop() { digitalWrite(led, HIGH); } I get: Sketch uses 5,098 bytes (17%)...
12
votes
6answers
38k views

Code for one-time execution in Arduino

Question: Where do I put code if I want it to execute only once per Arduino startup? Background: I tend to use my Arduino to perform tasks that should be executed once per power cycle. (For example, ...
12
votes
3answers
45k views

Functions with string parameters

Inside my main loop there is this string: String string1; I have a function that will take string1 as parameter, and use it to send this string as SMS. sendSMS(string1); This is the sendSMS() ...
12
votes
2answers
3k views

Arduino as ISP without a computer

Has anyone seen or know of a method of preloading (using a computer or whatever) a firmware as data onto one arduino set up to operate as an isp and then using that arduino plug the isp interface into ...
10
votes
2answers
8k views

Will a .ino Arduino Sketch compile directly on GCC-AVR?

Okay, we all have seen those questions all over the web such as Arduino v.s. C++, or other similar questions. And a vast majority of the answers do not even touch compilation differences other than ...
10
votes
3answers
3k views

Could someone explain this weird looking code, used to setup timers?

While looking at sketches other people have written, I occasionally come across code that looks somewhat like this: TCCR1A = 0; TCCR1B = 0; TCNT1 = 34286; TCCR1B |= (1 << CS12); TIMSK1 |= (1 &...
10
votes
3answers
87k views

Why is my LCD 16*2 is not displaying any characters, although all the wiring is correct?

Recently I have linked up a typical 16*2 LCD (Liquid Crystal Display) display to my Arduino Uno and have linked all the pins up. Here is my connection:(LCD pin first) 1=GND, 2=+5v, 4=11, 6=12, 11=5, ...
10
votes
5answers
26k views

Why is my real time clock getting the wrong time from my PC?

I want my real-time clock to set its time as the time on my PC. However, when I run the following sketch, the real-time clock reports the time as being 32-33 seconds earlier than my PC says the time ...
9
votes
4answers
21k views

What is the difference between declaring a variable outside of loop and declaring static inside loop?

These are two ways I can hold a variable outside of loop (or any function). First, I can declare it with global scope outside of loop: void setup() { Serial.begin(9600); } int count = 0; void ...
9
votes
2answers
3k views

Why is int only 2 bytes?

When using C/C++ on other platforms, the int type is typically 4 bytes (or potentially more). However, on Arduino, it's only 2 bytes. Why is it different? Does it affect performance if I always use ...
9
votes
2answers
1k views

What will happen when I use an invalid pin number?

Related to: What happens if there is a runtime error? This question is similar to the one above, however this is an alternate situation: int pin = 999; pinMode(pin, OUTPUT); digitalWrite(pin, HIGH)...
9
votes
3answers
26k views

Arduino time keeping using millis() is not accurate or correct?

I have been using the Arduino to record some data. In my Arduino sketch I also used the millis() function so I can keep track of the time at which each value I am measuring is taken. However, I ...
9
votes
4answers
6k views

What is the best way to define an I/O pin?

I read definitions like const int ledPin = 9; and also #define ledPin 9 I know that a definition like int ledPin = 9; is a bad practice if you're not going to change it (which you usually won't),...
9
votes
2answers
2k views

Can the 2nd MCU on the UNO R3 be used for keyboard emulation?

Following on from What's the 2nd ICSP header for in Arduino Uno R3? I was wondering how this could be hacked to our advantage. For example, could that firmware be rewritten to have the UNO ...
9
votes
4answers
8k views

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 ...
9
votes
2answers
2k views

Allocate object memory statically; intialize it dynamically?

I have an object whose constructor gets passed a parameter. If I know the parameter value at compile time, I can construct the object statically: static FOOOBJ foo(3); (I understand that it isn't ...
9
votes
3answers
3k views

How to measure total energy usage of an arduino board

I have a project that I'm currently working on. We have built a green wall, wich consists of plants on a wall Now, we have a pump situated at the bottom that waters all the plants. There is an ...
9
votes
3answers
3k views

How to write makefile-compatible sketches?

I'd like to write my sketches so that I can either build/upload them using the Arduino IDE, or optionally using GCC and a makefile. I know about including the function declarations at the top, but is ...
9
votes
3answers
901 views

Programming many Arduinos simultaneously with a wireless broadcast

I will have many microcontroller-based devices which will be independent from each other, but all will run the same program. I would like to upload a program to all of them at the same time with a ...
8
votes
5answers
16k views

How to loop over analog pins?

I want to do something along the lines of for (int i = 0; i < 4; i++) { analogRead(i); } Which appears to work, but the following does not: for (int i = 0; i < 4; i++) { pinMode(i, INPUT);...
8
votes
3answers
2k views

Hiding WLAN password when pushing to GitHub

I have started to use GitHub to store Arduino code. However some of my code uses the SSID and password for my WLAN which I don’t want published. A common way of handling this in non-embedded ...
8
votes
2answers
2k views

Convert a Bitstring into an integer value

I have got a String filled up with 0 and 1 and would like to get an Integer out of it: String bitString = ""; int Number; int tmp; bitString = ""; for (i=1;i<=10;i++) { tmp= analogRead (...
8
votes
3answers
6k views

Manchester Library Won't Compile for Attiny85

I am creating a wireless sensor using an Attiny85. I want to send the data to an arduino uno, so I purchased the 315mhz rf link kit from spark fun. Since the Attiny85 does not have a TX I decided to ...
8
votes
2answers
4k views

OOP vs Inline with Arduino

I have been programming for quite a while now but I am new to Arduino and AVR Programming. The main question I have about programming these Micro-controllers is are there major differences in ...
8
votes
2answers
7k views

Reading a varying voltage into Arduino

Using an Arduino Nano and a current transformer (CT), I'm trying to sense the current flowing through a 120 V 60 Hz line. Circuit CT outputs 0-1&nbsp;V according to its specifications....
8
votes
1answer
6k views

Is it better to use c_str or toCharArray?

When reading/trying a recent answer, I was surprised to see that Arduino's String class supports the c_str() method, just like the C++ std::string class. As expected, it appears to get a pointer to ...
8
votes
1answer
3k views

Event driven programming

I have ran across a couple of different event libraries that are available on the AVR and Andruino but I am not sure which one is the best option. What I would like to do is design my program in a ...
7
votes
5answers
13k views

how fast does loop() run in Arduino

Hi I am using Arduino Nano to gamify my experience on Unity3D. I know that Update() in Unity3D runs every frame per second but I because I'm not an electronics guy I don't know how loop() function ...
7
votes
2answers
550 views

Storage usage of methods compared to copied code

When you have really small snippets of code that are repeated several times in your micro controller program, is it better to write a method? I mean better in terms of performance and storage usage. I ...

1
2 3 4 5
31