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
4answers
130k 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
673 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
1k 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
542 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
2k 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 ...
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 ...
5
votes
2answers
467 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 ...
4
votes
3answers
2k 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 ...
4
votes
4answers
21k 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
194 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
1answer
127 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
74 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
602 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
1k 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
262 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
144 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
3answers
131 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
331 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
493 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
188 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
1answer
376 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
332 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
432 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
2answers
9k 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
4answers
123 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
1k 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
1answer
40 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
636 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
1answer
59 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
2answers
222 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
100 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 ...
1
vote
1answer
1k views

Super-fast USB communication; how is it possible?

On Arduino forums, there's a post where someone improved the USB communication and made it super-fast. I don't understand how he did this, and I'm trying to recreate this, but failing. My test code ...
1
vote
2answers
1k views

Low Power Library 4 seconds ON, 10 minutes OFF

Could someone please demonstrate how to use a counter with the Arduino 'low power' library to wait for 10 minutes then digitalWrite 4000, HIGH and loop. (I don't mean the simple 'blink' sketch). I'm ...
1
vote
1answer
226 views

Help avoiding floating point math

I'm using a Texas Instruments DRV2605 haptic controller in my project to drive a LRA vibration motor. I would like to calculate the LRA rated voltage during run time using the formula below as ...
1
vote
2answers
409 views

Digital Vs. Analog Sensor

Background: I'm using an Atmega1284P to add sensors to an off road racing truck onto a CANBUS network. The standard automotive sensors are thermistors, but the Dallas 1 wire temp sensors (DS18B20) ...
1
vote
1answer
1k views

Arduino Due SPI speed reduction

I need to reduce the SPI clock speed of the Arduino Due down to about 100 kHz. Unfortunately my hardware doesn't support higher speeds. With the current maximum divider of 255, I can only reach a ...
1
vote
3answers
2k 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
3answers
283 views

What is the simplest way to “ask” the arduino which input pins are HIGH?

I am trying to build a 36-line cable tester, using the Digistump DigiX (99 i/o pins). Digital pins 0-35 are outputs, connected via the cable in question, to (in a perfect world) digital input pins 36-...
1
vote
2answers
155 views

Questions on programming logic?

So I am learning arduino code by going through examples and playing with them, and I came across something interesting. I am not fluent in any coding language but I am fully familiarized with computer ...
1
vote
1answer
2k views

Optimizing Arduino File Selection from SD card

the following code will display the files on an SD card in an Arduino and allow the user to open a file of their choosing. Here's the code with a walkthrough of what it does. In the Init state, I ...
1
vote
1answer
496 views

Optimization Teensy 3.6 Arduino IDE fast I/O Multiplexing Led Matirx

I'm currently working on a 10x10 LED Matrix which is controlled by 5 Shift registers using multiplexing. I've the problem that the Teensy 3.6 is too fast/slow. One part of my code is the Shiftout ...
1
vote
1answer
122 views

Code Optimisation [closed]

I have tried fading the RGB Led with different colour format and sequence. Working of Code :- When value of number is 1, the Red LED fades gradually and glow up with the delay interval of 10ms with ...
1
vote
0answers
123 views

FIltering, FXOS8700, FXAS21002C optimization

I'm working on an inverse pendulum project, and am using an adafruit NXP_FXOS8700_FXAS21002 along with an Arduino due, and an esp8266 (two separate projects, same code however). I'm using a Magdwick ...
1
vote
0answers
87 views

Why does the program size increase so drastically with these switch-cases?

EDIT: The problem is my op-code catch is op >= 0x20 || op <= 0x2F rather than op >= 0x20 && op <= 0x2F and so most of the switch-case was being optimised away! This is a really ...
1
vote
0answers
1k 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 ...
1
vote
1answer
55 views

ble_write slows down my sketch causing problems (Blend Micro & NRF8001)

I'm working on a prototype which uses the RedBearLabs Blend Micro with the NSC's nrf8001 ble chip, but have ran into a problem that I'm not sure how to solve. I have two tactile buttons connected to ...