Questions tagged [code-optimization]

The process of improving the efficiency of a program such that is uses less memory and/or less computational time.

Filter by
Sorted by
Tagged with
18
votes
5answers
169k 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 ...
12
votes
5answers
4k views

Does an Arduino get worn out by too much command execution?

Excuse me if it is a silly question, but I couldn't find an answer. I've been wondering for a while whether an Arduino (or generally any other micro-controller) gets happy if the code is light and ...
9
votes
1answer
886 views

Overview of compiled code size

When I compile my code, the Arduino IDE returns the binary sketch size in byte. Is there a good way to find out (approximately) what function or what part of my code takes up how much memory in ...
7
votes
3answers
2k views

How to increase life of EEPROM?

I want to store a few (8 as of now) IR codes into my Board's EEPROM. It would be great if someone could suggest a few tips to maximize the lifetime. The IR codes in my sketch are saved in array of ...
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 ...
6
votes
4answers
3k views

What's the most efficient implementation of map(x,0,1023,50,250)?

I recently noticed how the map() function in Arduino was bulky in both terms of flash space used and time taken to execute, largely because it deals with long and involves a division and ...
6
votes
2answers
496 views

What is the proper way to implement PID?

I'm having some trouble conceptualizing how to go about implementing PID into my drone. There are a few ways I'm thinking about implementing the pipeline: Having one PID Object for each of the six ...
5
votes
4answers
2k 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 ...
5
votes
3answers
3k views

Set EEPROM from PC before program

I have been working on an arduino program, which has gotten rather large. I am close to hitting the 32k limit on Flash for my ATmega328p. I would like lower my program size by saving some of my many ...
5
votes
1answer
326 views

Does the compiled binary of a sketch include uncalled functions from a library?

I am writing a GUI using a TFT display and the Adafruit GFX library and its default font on Arduino Nano. I want to be memory efficient and I assumed that the 'optimise for size' default setting of ...
5
votes
1answer
2k views

How to enable maximum Dead Code Removal?

I am running out of code and data memory space on ATMega328P. Code size is big as I used several libraries, but, I only use a few functions of those library. Apparently, the default IDE is only ...
4
votes
4answers
24k views

What is better: one sprintf() or multiple strcat() and itoa()?

I was creating Arduino function which sends two int and additional chars between them and at the beginning and end of char array - then print that array to the serial port in almost real time. (For ...
4
votes
2answers
204 views

Problem with inaccurate delays between sensor readings

During the last two months I have been working on a project that consists of using a Seeeduino Stalker V2.3, two AM2305 sensors, a TSL2561 sensor, an HC-05 XBee module, a 1,800 mAh 3.7V LiPo, a 7 cm x ...
3
votes
1answer
1k views

OOP vs procedural programming on arduino

It seems like the majority of Arduino applications are written without using classes / objects. Is there a reason for this? Does is slow down the Arduino or somehow present undesirable behavior?
3
votes
2answers
532 views

How to move a servo quickly and without delay function

Im trying to move a servo from one place to another while using the typical for loop you find in the servo's library example. int lightON = 180; int lightOFF = 90; for (pos1 = lightOFF; pos1 <= ...
3
votes
1answer
151 views

Cleaning up code? Removing repetition?

I am using Adafruit motor shield, I am running multiple DC motors for different amounts of time, but to start at the same time - I have researched that to do this I have to monitor the elapsed time of ...
3
votes
1answer
91 views

Read time digits from DS1307 and run code based on return

I am currently reading a DS1307 adafruit breakout board and I need to drive two nixie tubes from a shift register. The shift register is running two 74141 IC chips. I have code that loops through all ...
2
votes
3answers
5k views

Storing an array of function pointers

I have some Arduino code that looks like: char packet = ser.read(); switch(packet){ case 'a': someobj.somefunc(); break; case 'b': otherobj.otherfunc(); ...
2
votes
3answers
954 views

Reducing Memory Usage lots of Floats

Im using an Arduino Uno - All code at github.com/robbrad/AirQuality Current sketch usage: Sketch uses 23186 bytes (71%) of program storage space. Maximum is 32256 bytes. Global variables use 1428 ...
2
votes
2answers
2k views

How do I set a constant in setup() that I can use in loop()?

I am new with coding, and I was wondering how I could run something once and define it as a constant. Here is my code: int sensorValue1 = analogRead(A0); int sensorValue2 = analogRead(A1); int g0 ...
2
votes
3answers
297 views

A more elegant way to deal with timers without using delay()?

I am looking for a more elegant way to write a function that performs timer-based tasks as in the case of the code below, which essentially collects 5 sample values from a microphone module over a ...
2
votes
2answers
173 views

Port Manipulation - fastest implementation

I'm working on an Arduino program but am getting bogged down on what is the 'correct' implementation. I understand that multiple versions may work, so for my needs the 'correct' version will be the ...
2
votes
1answer
82 views

FIXING BUG when Using Wire library to obtain compass heading

I am using the HMC-5983 magnetometer module to obtain the compass heading of a autonomous car I am building using the arduino platform.I'musing the Z axis and X axis to obtain the heading. However ...
2
votes
3answers
149 views

Conditional randomization gets stuck

First-time poster here... I have the following code to randomize note values and store them in an array loop1[i], where i is a specific step in the loop: nextNote = random(-2, 3); note = note + ...
2
votes
2answers
360 views

Possible to condense this if statement?

I have this if statement: if (now.hour() == 5 && now.minute() == 10 && now.second() == 0 && now.dayOfTheWeek() != 1 && now.dayOfTheWeek() != 7){ Is there a way to ...
2
votes
1answer
54 views

Thermocouple and multimeter

I've put together this script that should allow my Arduino Mega to be show thermocouple readings on the serial monitor, whilst also showing readings of voltage and ohms across a given component. I'd ...
2
votes
1answer
760 views

Error in IR receiver project

I'm trying to receive remote information with an IR receiver and this is my code: #include <IRremote.h> int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results; void setup() { ...
2
votes
1answer
247 views

Is setting clock divider worth changing on delay() to save power?

Along all the "advanced" methods described in this article, does simple method as downscaling processor while delay makes a difference on power consumption? How much time does the clock alternation ...
2
votes
2answers
50 views

How to move a servo with a function

Im currently moving a servo from one side to another by using the typical for loop like this: int lightON = 180; int lightOFF = 90; if (buttonState == HIGH) { digitalWrite(LED, HIGH); for (pos1 =...
2
votes
1answer
437 views

PCF8547 Wire.write explanations

I am using an ESP8266-12E with a PCF8547. I have managed to turn on and turn off LEDs using these two things. I am using these three commands to do it. Wire.begin(SDA_PIN,CLK_PIN); Wire....
2
votes
1answer
409 views

Sim800L's library can not compile on Arduino Due

Sim800L's library can not compile on Arduino Due. Is there any way to create a library for Arduino Due? #include <Sim800l.h> #include <SoftwareSerial.h> Sim800l Sim800l; char* text; char*...
2
votes
1answer
1k views

Results for GP2Y1010AU0F Dust Sensor

I am using Dust Sensor GP2Y1010AU0F I downloaded the code from the manufacturer here: http://www.waveshare.com/wiki/Dust_Sensor /**********************************************************************...
2
votes
0answers
50 views

Am I maximising my A0 readout rate with this peak-detection code?

I have a pulse going into A0 on a Nano, and want to detect the peak voltage. With low-pass filtering to slow down the input pulse I'm still reading marginally too slow. The code overall inhibits a ...
2
votes
0answers
2k views

Sending data from webserver to Arduino

Hello Arduino enthusiasts! I've been working on a keypad lock system for school lately and I need some advice.Basically, my code checks if the entered passcode is the same than a predefined passcode ...
2
votes
0answers
495 views

Faster output of Serial.print() data coming from TSL1402R sensor [closed]

I am reading a TSL1402R with an Arduino Mega2560. The code is based on the sketch from the arduino playground here:http://playground.arduino.cc/Main/TSL1402R The application is a digital caliper for ...
1
vote
4answers
146 views

Porting code from Arduino IDE to other IDEs - expected code size reduction

Could I realistically be expecting some 25% - 50% code size reduction if I abandoned Arduino IDE's framework and moved to something more rudimentary such as AVR Studio ? I am using the EEPROM, Timer1 ...
1
vote
2answers
12k views

How to read code from Arduino Uno to Arduino IDE? [duplicate]

we can upload a code into Arduino UNO from our computers, but how about reading code? Can we read and get C codes from compiled codes from Arduino hardwares? My second question is that will we read ...
1
vote
3answers
111 views

Is there a more elegant way to check highest / lowest sample data?

N00b question, Is there a better way for readability / elegance / DRY etc to check various sensor values for high/low record value? This code works, but looks like the long way round. void ...
1
vote
2answers
610 views

code optimazation 6x6 Matrix

I planed a 6x6 matrix with a Arduino Nano as controller. Its only possible to let one LED in the matrix lighten up. Any idea how I get the Code smaller? (Im a C Noobie ^^) ~Nico Circuit diagram: ...
1
vote
2answers
316 views

Generate a true analog signal using Arduino

I am looking for a way to create a true analog output with the arduino uno. I am constantly reading an analog input on a0 pin, all i want to do is send the same signal as output. I know arduino ...
1
vote
2answers
2k views

Const data optimisation for Arduino

I'm taking a course on Arduino Uno programming and on today's talk I heard that the const data is optimized by Arduino, so that it occupies less space than the actual data type (namely, a const int ...
1
vote
2answers
447 views

Correct use of interrupt vs monitoring switch

Background: I have a uni assignment which requires an autonomous robot to do a number of tasks and then finish in a set spot after being triggered to start by an outside physical interaction. I've ...
1
vote
1answer
44 views

How to int variables with similar names without tons of code? [duplicate]

I'm using int with 14 LEDs to specify pins, but I don't want to use int 14 times, like this: int led1 = 2; int led2 = 3; int led3 = 4; int led4 = 5; int led5 = 6; int led6 = 7; int led7 = 8; int led8 ...
1
vote
2answers
1k views

Best practice to declare a 'static' text and save memory

I'm working on a project where I need to create a JSON to send back state of an object. I created the code to achieve it and all works fine, but I used this to declare the JSON fields: static const ...
1
vote
3answers
564 views

Breaking up for loop with ISR

I am curious if the following scenario is possible. EICRB |= (0<<ISC60)|(0<<ISC61); EIMSK |= (1<<INT6); ISR(INT6_vect) { myISR(); } void myISR() { digitalWrite(led_off, ...
1
vote
1answer
68 views

Array Addressing

Is it possible to use a name instead of a number to address an array? I was reading about enum lists and thought it might be possible. ie int array[3] = {something, somethingElse, somethingMore] ...
1
vote
3answers
3k views

Are static variables in a function bad?

For my code I use a couple of static variables. But when I search the internet, they seem "evil". For example: I have a function which will be executed every loop (5ms looptime). to increase the ...
1
vote
2answers
240 views

Light sensor acting up when used with other code

I have a light sensor that works fine and outputs the correct data to the serial monitor when I only upload the following code to the nano: const int lightSensorPin = A0; int lightSensorValue = 0; ...
1
vote
1answer
516 views

Using Serial.write() to send a uint32_t send one byte x4 or send 4 bytes x1

In the IRremote library, IR signals are decoded and held in results.value. My project uses the NEC protocol which is 32 bits, so the IR code will always be 4 bytes. uint32_t dCodedIR; // // code ...
1
vote
1answer
104 views

LED Programme where it delays the amount written in the Serial Monitor

I have written a simple code to delay the blinking of LED 13, the delay time is set by us.I have incurred a problem when I enter a 2 digit value in the serial monitor.There is some problem with the ...