Questions tagged [code-optimization]
The process of improving the efficiency of a program such that is uses less memory and/or less computational time.
132 questions
19
votes
5
answers
191k
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
5
answers
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
1
answer
1k
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 ...
8
votes
4
answers
4k
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 ...
7
votes
4
answers
1k
views
Optimizing code for ATtiny10
I'm trying to squeeze code onto an ATtiny10, but I'm just not getting there. I'm using 1060 bytes and only have space for 1024 bytes.
The code is simple enough; I need to read a button pin. If high it ...
7
votes
4
answers
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 ...
7
votes
4
answers
3k
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
2
answers
588
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
2
answers
572
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
3
answers
4k
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
1
answer
3k
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
2
answers
3k
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 <= ...
4
votes
1
answer
633
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 ...
4
votes
4
answers
27k
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
2
answers
419
views
How to cut down size of imported DigiKeyboard library
I'm working on a project with a Digispark ATTiny85, that performs keystrokes using the DigisparkKeyboard library (https://github.com/digistump/DigisparkArduinoIntegration/tree/master/libraries/...
4
votes
2
answers
269
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
2
answers
1k
views
I need compact code to fit in memory
I feel a little like I am back in 1975 trying to fit my code in 4K of RAM.
I am writing some code for an ATTiny88, and the code is starting to get big, relatively speaking. I need to save every byte ...
3
votes
1
answer
2k
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
1
answer
219
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
2
answers
322
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 =...
3
votes
1
answer
135
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
3
answers
6k
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
3
answers
2k
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
2
answers
3k
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
2
answers
3k
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 ...
2
votes
2
answers
2k
views
How to swap byte order?
Background
I'm reading a 24-bit ADC value into a uint32. Using SPI bus, here's the logic:
value = SPI.transfer(0); // read first 8 bits (MSB first)
value <<= 8; // shift ...
2
votes
1
answer
212
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
2
answers
439
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
2
answers
284
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
3
answers
194
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
1
answer
1k
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
1
answer
113
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
1
answer
351
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
1
answer
599
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
1
answer
518
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
1
answer
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
0
answers
95
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
0
answers
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
0
answers
579
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
2
answers
13k
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
4
answers
255
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
3
answers
3k
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
3
answers
186
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
2
answers
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
2
answers
1k
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
3
answers
379
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 ...
1
vote
1
answer
58
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
2
answers
535
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
1
answer
89
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
3
answers
89
views
Merging strings and constants causing scope errors
I'm getting a few errors when merging strings with constants.
Here's the pertinent code...
#define STATION_ID ABC123
#define STATION_PASS XYZ987
void loop(){
String path = "/weatherstation/...