Skip to main content

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
19 votes
5 answers
191k views

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
12 votes
5 answers
4k views

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 ...
ahmadx87's user avatar
  • 549
9 votes
1 answer
1k views

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 ...
fuenfundachtzig's user avatar
8 votes
4 answers
4k views

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
7 votes
4 answers
1k views

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 ...
Earthbound Ruben's user avatar
7 votes
4 answers
3k views

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 ...
Cybergibbons's user avatar
  • 5,430
7 votes
4 answers
3k views

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 ...
karx's user avatar
  • 235
7 votes
2 answers
588 views

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 ...
Lehue's user avatar
  • 193
6 votes
2 answers
572 views

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

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 ...
riverrun's user avatar
5 votes
1 answer
3k views

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

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 <= ...
Marcelo_M's user avatar
  • 121
4 votes
1 answer
633 views

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 ...
WildCat's user avatar
  • 323
4 votes
4 answers
27k views

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 ...
ErnestW's user avatar
  • 109
4 votes
2 answers
419 views

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/...
emma.makes's user avatar
4 votes
2 answers
269 views

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 ...
OpenAGRO's user avatar
3 votes
2 answers
1k views

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 ...
LesRhorer's user avatar
  • 113
3 votes
1 answer
2k views

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?
Jacksonkr's user avatar
  • 327
3 votes
1 answer
219 views

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 ...
Steve's user avatar
  • 33
3 votes
2 answers
322 views

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 =...
Marcelo_M's user avatar
  • 121
3 votes
1 answer
135 views

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 ...
Cheddar's user avatar
  • 133
2 votes
3 answers
6k views

I have some Arduino code that looks like: char packet = ser.read(); switch(packet){ case 'a': someobj.somefunc(); break; case 'b': otherobj.otherfunc(); ...
Cerin's user avatar
  • 1,698
2 votes
3 answers
2k views

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 ...
Rob's user avatar
  • 135
2 votes
2 answers
3k views

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 ...
Andreas's user avatar
  • 23
2 votes
2 answers
3k views

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 ...
Noisemaker's user avatar
2 votes
2 answers
2k views

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 ...
hazymat's user avatar
  • 121
2 votes
1 answer
212 views

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 ...
AfiJaabb's user avatar
  • 219
2 votes
2 answers
439 views

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 ...
Shpigford's user avatar
  • 484
2 votes
2 answers
284 views

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 ...
Ramrod's user avatar
  • 187
2 votes
3 answers
194 views

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 + ...
Erik's user avatar
  • 271
2 votes
1 answer
1k views

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() { ...
Ali Özgür's user avatar
2 votes
1 answer
113 views

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 ...
Chris's user avatar
  • 51
2 votes
1 answer
351 views

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 ...
madneon's user avatar
  • 225
2 votes
1 answer
599 views

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....
KarmaCoding's user avatar
2 votes
1 answer
518 views

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*...
Ibn Sina's user avatar
2 votes
1 answer
1k views

I am using Dust Sensor GP2Y1010AU0F I downloaded the code from the manufacturer here: http://www.waveshare.com/wiki/Dust_Sensor /**********************************************************************...
Daniel Benedykt's user avatar
2 votes
0 answers
95 views

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 ...
Chris H's user avatar
  • 168
2 votes
0 answers
2k views

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 ...
Arxkz's user avatar
  • 23
2 votes
0 answers
579 views

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 ...
bigstew123's user avatar
1 vote
2 answers
13k views

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 ...
Bay's user avatar
  • 303
1 vote
4 answers
255 views

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 ...
kellogs's user avatar
  • 136
1 vote
3 answers
3k views

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 ...
eToll Systems's user avatar
1 vote
3 answers
186 views

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 ...
opt10n's user avatar
  • 11
1 vote
2 answers
2k views

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 ...
theSongbird's user avatar
1 vote
2 answers
1k views

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: ...
Nico L.'s user avatar
  • 45
1 vote
3 answers
379 views

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 ...
gnzg's user avatar
  • 157
1 vote
1 answer
58 views

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 ...
智障的人's user avatar
1 vote
2 answers
535 views

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 ...
James's user avatar
  • 113
1 vote
1 answer
89 views

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] ...
Matt's user avatar
  • 158
1 vote
3 answers
89 views

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/...
Shpigford's user avatar
  • 484