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
132 votes
4 answers
106k 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 ...
Edgar Bonet's user avatar
  • 42.8k
21 votes
3 answers
8k 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 ...
heltonbiker's user avatar
5 votes
1 answer
1k views

How do you combine 2 different sketches together

(Before you down-vote this, note that it is a "ringer" to answer a FAQ, with a self-answer.) This question comes up all the time. How do you combine 2 different sketches together? Say I have a sample ...
Duncan C's user avatar
  • 5,682
94 votes
22 answers
19k 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++...
DLJ's user avatar
  • 1,149
62 votes
7 answers
108k 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 ...
Cybergibbons's user avatar
  • 5,340
40 votes
12 answers
147k 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 ...
Nyxynyx's user avatar
  • 1,389
9 votes
2 answers
3k 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 ...
Madivad's user avatar
  • 1,362
41 votes
10 answers
177k 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 ...
JVarhol's user avatar
  • 1,903
13 votes
4 answers
7k 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 ...
Rexcirus's user avatar
  • 253
1 vote
1 answer
17k views

Use object of other class within class

I am writing a class for a project which will take care of handling any LCD updates for my project. The way I want to handle this is to initialize the LCD object in my main file, and then pass the LCD ...
Alex's user avatar
  • 181
31 votes
4 answers
58k 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 #...
Cybergibbons's user avatar
  • 5,340
7 votes
4 answers
35k views

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 ...
Andy's user avatar
  • 103
4 votes
4 answers
3k views

class enum was not declared in this scope

I am programming an Arduino. In the same .ino file as setup() and loop() I have defined the following: void setup() { // setup code } enum class CYCLE { TypeA, TypeB }; String cycleToString (...
AJP's user avatar
  • 181
1 vote
2 answers
2k views

How to combine two different sensor sketches to one complete sketch?

How to combine two different sensor sketches to one complete sketch for a complete circuit that attaches to one Arduino Uno? Sharp Dust Sensor int measurePin = A0; // Connect dust sensor to Arduino ...
Muhammad Lutfi's user avatar
18 votes
2 answers
27k 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 ...
asheeshr's user avatar
  • 3,837
5 votes
2 answers
82k views

"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 ...
user2950509's user avatar
3 votes
3 answers
33k views

Resetting millis() and micros()

I want to know how much time has elapsed since a certain event, and I do not want to use any external timers. It seems logical to reset an internal timer whenever the event occurs (using an interrupt)....
pseudoeuclidean's user avatar
19 votes
5 answers
78k 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]
Facorazza's user avatar
  • 431
13 votes
4 answers
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 ...
Sebazzz's user avatar
  • 231
12 votes
2 answers
11k 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 ...
RedDogAlpha's user avatar
9 votes
3 answers
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 ...
DLJ's user avatar
  • 1,149
5 votes
2 answers
5k views

How to declare a global variable in the .h file?

I have a .h library file in which I want to declare a global variable that can be read in the program loop and in the library's .cpp file. The question is, is it possible to declare a variable in the ...
Delta's user avatar
  • 263
4 votes
1 answer
4k views

XBee+Arduino communication problem

I tried testing the system and I’m not sure if the problem is with the XBee’s, the transmitting code, or the receiving code. Before I post my code, I will explain what we are doing with the signals. ...
user734's user avatar
  • 91
4 votes
3 answers
18k views

Error: 'SofwareSerial' does not name a type

I have trouble compiling this sketch. The error I get is 'SoftwareSerial' does not name a type Any ideas how could I fix it? #include SoftwareSerial.h SoftwareSerial mySerial(10, 11); // RX, TX ...
alkopop79's user avatar
  • 145
2 votes
2 answers
1k views

Optimizing code to use less Flash Memory and SRAM

I'm trying to reduce my sketch size and managed to reduce both SRAM and Flash memory. From: 1570 SRAM - 32144 Flash memory To: 644 SRAM - 19458 Flash memory This are what I've done so far: ✓ ...
ElectronSurf's user avatar
2 votes
2 answers
257 views

How can I reduce the redundancy of this code, so one function can perform the task of printing on Serial and Softserial?

void Multi_print (String str) { Serial.print(str); mySerial.print(str); } void Multi_println (String str) { Serial.println(str); mySerial.println(str); } void Multi_parse_int (int value) { ...
user335292's user avatar
1 vote
2 answers
15k views

Send multiple int values from Python to Arduino using pySerial

I'm trying to send 3 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or ...
ZanQdo's user avatar
  • 31
1 vote
3 answers
5k views

How to automatically reset the nrf24l01+ with code?

I have two separate nrf24l01+ connected to two different Arduino UNOs, one as TX and the other as RX. The problem is the connection stops after a while between them. On the Arduino COM screen I have ...
paulplusx's user avatar
  • 171
0 votes
2 answers
128 views

What is the opposite of this data operation?

If I store in an array a double-value in this format, in which format I should be able to read the same double out? double myDouble = 12.123456; byte myArray[] = {0x00, 0x00, 0x00, 0x00}; myArray[0] ...
William Roy's user avatar
0 votes
1 answer
2k views

PWM delay without blocking the code

I want my LED to turn on gradually without using the delay() function and blocking the code. Here's the code i came up with: int led_pin = 6; unsigned long millisTimer = 0; int PWMdelay = 5000; int ...
ElectronSurf's user avatar
0 votes
3 answers
10k views

I keep on getting a stray '\302' in program error.How do I fix it.This is in arduino uno [duplicate]

#include <LiquidCrystal.h> int trigPin = 13; //sensor's trigger pin connected to pin 13 int echoPin = 7; //sensor's echo pin connected to pin 11 float pingTime; //time for the ping to hit ...
Adam's user avatar
  • 19
0 votes
2 answers
86 views

Easy bipolar motor control?

Is here some easy way to control bipolar motor without using any other external devices, drivers or shields? I have small motor from CD-ROM so I expect it's low voltage. By control I mean just ...
user avatar
0 votes
1 answer
135 views

Stopping the program after 1 second

I am a Beginner in Arduino Programming. I am working on a project where I control A DC motor with IR sensor and arduino. Until here it is fine. But I want the Motor to stop after one second even ...
Nallella Shashank's user avatar
0 votes
0 answers
40 views

Moving servos with IR sensors

This is what I have so far: The plan: I have two IR sensors I have two servos What I would like to achieve is as follows: Enter IR sensor 1 and leave, servo 1 will turn to 180 Enter and leave IR ...
PeterJoe's user avatar
0 votes
1 answer
5k views

Get the uploaded code from arduino by using arduino IDE 1.0.6 [duplicate]

Recently bought a arduino device which already have program, it is model AR-293D and I have already connect it with my PC(windows 7) through usb when I open the IDE , It seems there is no function ...
user782104's user avatar
-2 votes
2 answers
440 views

I am trying to combine an automatic door lock with an RFID MFRC522 with a IR REMOTE program

I am getting this error. Arduino: 1.8.2 Hourly Build 2017/02/17 11:33 (Windows 8.1), Board: "Arduino/Genuino Uno" Tone.cpp.o (symbol from plugin): In function `timer0_pin_port': (.text+0x0): ...
user33872's user avatar
23 votes
3 answers
50k 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.
Jacer Omri's user avatar
22 votes
5 answers
8k 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 ...
asheeshr's user avatar
  • 3,837
19 votes
5 answers
187k 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 ...
user3.1415927's user avatar
18 votes
3 answers
10k 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 ...
Hardik Thaker's user avatar
15 votes
5 answers
7k 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?
tstew's user avatar
  • 729
13 votes
6 answers
43k 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, ...
apnorton's user avatar
  • 533
12 votes
3 answers
52k 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() ...
user1584421's user avatar
  • 1,395
11 votes
3 answers
101k 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, ...
Stratmoss's user avatar
  • 137
10 votes
4 answers
9k 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 ...
TheDoctor's user avatar
  • 3,469
8 votes
2 answers
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 ...
Andy Braham's user avatar
8 votes
4 answers
3k views

What are the traditional ways to optimize program memory usage?

While doing big projects using Arduino boards (Uno, Atmega328P MCU). I used to get warnings like this Sketch uses 13764 bytes (44%) of program storage space. Maximum is 30720 bytes. Global variables ...
Mayoogh Girish's user avatar
8 votes
3 answers
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 ...
Joel's user avatar
  • 523
7 votes
1 answer
3k views

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, ...
stUrb's user avatar
  • 351
7 votes
4 answers
6k views

Multithreading with Arduino

Well I've just started learning the concept of multithreading with C++ and immediately a bunch of questions came to mind about the possibility of using multithreading with microcontrollers in general ...
Omar Hussein's user avatar