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
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 ...
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 ...
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 ...
-2
votes
1
answer
719
views
How do I create a code that lets me use an array of 10 thermistors on one arduino?
I am new to programming and am trying to create an array of 10 thermistors on one arduino. So far I have found a code for Temperature Sensor Using NTC Thermistor that is only for one thermistor. How ...
1
vote
0
answers
68
views
Sampling rate for Adafruit LSM6DSOX and Arduino MKR WIFI 1010
Please let me know if this post has the wrong tags, I will make sure to change it.
My goal is to get the highest sampling rate possible from the Adafruit LSM6DSOX (6667Hz) using the Arduino MKR WIFI ...
1
vote
1
answer
300
views
How to optimize checking for specific string in a UART stream
I want to check for specific string (like "RecordStart") in a UART stream (115 200 baud 8N1) from a camera, to know when it is recording or not/if there is an SD. I am using an ESP32-WROOM-...
0
votes
2
answers
505
views
best practice for performance: empty loop() when using interrupt?
I'm working on a little midi hardware project and I'm using attachInterrupt() to assign a callback function to an interrupt pin.
Nothing gets done in the loop function, so it looks like this:
void ...
-1
votes
3
answers
379
views
Is there any way to efficiently code reading of four sensors and sending a serial output once it does detect a reading?
I am actually wondering if there's a way to efficiently do this block of code. There are two things in my specifications and these are:
Four IR Sensors that detects blockage. Basically just to ...
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 <= ...
1
vote
2
answers
221
views
Code Optimization: Interrupt when a value reaches 0
I have a working program and I wish to improve its efficiency as much as possible.
It will blink slow, then faster and faster until it restarts.
It works, but my main concern is, currently, I am ...
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/...
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 ...
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 =...
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 ...
0
votes
1
answer
2k
views
Optimised random alphanumeric string generator
I'm trying to build arduino nonce generator, but the only thing I found is this question on arduino forum but I can't find out how to make it work for me. I checked and Serial.available() is always 0 ...
1
vote
4
answers
5k
views
Arduino Blink two LEDs without Delay(amount of repetitions)
Arduino community. I have had experience with Arduino for 3 years now and have finally come to the conclusion to get rid of the delay function entirely.
Below is the code to blink two LEDs a ...
1
vote
0
answers
98
views
How do I optimize this IRremote code?
I am using a ATTINY85 Digispark board with an IR receiver to light some LEDs in my project. The Digispark has a limitation that does not allow it to use the IRremote.h library. I know that there a ...
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 ...
0
votes
1
answer
162
views
Parallel I/O - maybe use Machine code function call
I'm a newbie to arduino but been programming lots.
I have a project where I would like to output 4 bits to adjacent pins and read 4 bits from another (4) adjacent pins. (Imagine a matrix keyboard ...
1
vote
1
answer
422
views
send and recieve analog data from one arduino to another arduino via web/ethernet
i am able to send audio signal through mic and send it via arduino and receive it on speaker.
i need two things
1.) The voice i am hearing on speaker has on speaker has lots of noise with it. i need ...
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 ...
1
vote
0
answers
72
views
Can a Windows 10 computer use Arduino IDE 1.8.10 sometimes AND use Arduino IDE 2.0.0 the rest of the time?
Can Windows 10 computer use Arduino IDE 1.8.10 sometimes, AND use Arduino IDE 2.0.0 the rest of the time?
1
vote
1
answer
141
views
Arduino Mega prototype analog timer interrupts: newbie question
I am currently using an Arduino Mega with a fast vibration sensor to trigger the components operation (force sensor, temperature sensor) and also toggle sleep mode.
I am very new at this and I have ...
1
vote
0
answers
1k
views
converting int or float into bytes array in arduino
I am trying to send data from one arduino to another over ethernet using udp protocol.
In arduino writing data over udp is like this
UDP.write(buffer, size);
where buffer is an array to send as a ...
0
votes
1
answer
47
views
Change character sequence
I turn on the transmission of one key that works to open the door, but after a certain time I need to close the door, and for this I need to use the second key. I want to do without additional buttons,...
1
vote
0
answers
963
views
How to stop my 16*2 lcd blink?
Here I wrote code for my weighting scale measurement. In which I added an on/off switch for my LCD display. But in defined delay time my LCD is a blink and I do not want it. I want my LCD switch on/...
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 ...
0
votes
1
answer
95
views
How to shorten this code (because my sensor reads slowly)?
I'm making a fare meter for our school project. I have too much lcd.print code. This code is not finished yet. My only concern is how to minimize the usage of lcd.print. I think it's the one that ...
0
votes
3
answers
140
views
I have made a Nano send 4 pot values to an Uno please help with code
Happy New Year!
So I have made a Nano send 4 pot values to an Arduino Uno over serial but I have a problem: I don't know how to separate these values. Here's the code:
Master:
int Pot = A0;
...
-1
votes
1
answer
267
views
Port manipulation
My UNO having some stability issues because of heavy memory use, I'm now optimizing the code
now looking to reduce the digital read memory use with the help of port manipulation. I got the code for ...
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 ...
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 ...
1
vote
0
answers
84
views
Automatic water pump Trigger System - I need my code FIXED
int out = LED_BUILTIN;
int strt = 3;
int stp = 9;
void setup() {
pinMode(out,OUTPUT);
pinMode(strt,INPUT);
pinMode(stp,INPUT);
}
void loop() {
while(true) {
if(digitalRead(strt) == LOW) ...
0
votes
1
answer
95
views
How might I add a delay, close the servo and go to the start again?
#include <Servo.h>
Servo myServo;
const int piezo = A0;
const int button = 2;
const int yellow = 3;
const int green = 4;
const int red = 5;
int knockVal;
int buttonVal;
const ...
1
vote
0
answers
1k
views
Reduce time between two consecutive SPI transfers on ESP32
I'm trying to drive an external DAC via SPI interface using ESP32 chip. But I cannot achieve the desired speed (>1M transfers/sec, each transfer consists of 16bits). The reason is not the SPI speed ...
0
votes
2
answers
2k
views
Is there a variable type of half byte? Is there a workaround?
I have this long array of 64 values going from 0 to 15, values which only cost 8 bits, for a pulse width modulation.
So I made a test to see its cost in space, commenting one of the two arrays.
const ...
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 ...
1
vote
1
answer
487
views
Plant irrigation system
I'm very new to the Arduino game I wanted to make a small auto watering system for my plants. I bought these items
5V 1 CHANNEL RELAY MODULE FOR ARDUINO PIC AVR DSP ARMB
9 VOLT BATTERY SNAP CONNECTOR ...
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() {
...
-1
votes
2
answers
77
views
multiple invdividual servo controll arduino
I want to declare 11 servo motors in my code. Is there a better way to do that than just declare everyone under each other. The reason is that i have a code that will set the rotation for one at a ...
1
vote
0
answers
57
views
Division with big constant number. Ideas to make it more efficient?
Im reading the voltage applied to my microcontroller using the internal voltage reference.
Here the topic is talked, and there are some working examples.
Im NOT using this examples, im using my own ...
1
vote
1
answer
891
views
Control arduino with keybaord via computer
I'm brand new to arduino but I'm trying to figure out how to set up a WASD steering system for my robot I made.
I have the physical setup all ready but I don't know the code to use that would allow ...
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 ...
1
vote
0
answers
123
views
Dragino Lora shield for Arduino sensor use
I am new to arduino and lora technology and i have dragino lorashield for arduino Uno and dragino gateway. I have also MQ-3 gas sensor and I want to detect the alcohol value and to send it to the ...
0
votes
3
answers
29k
views
How do I repeat a piece of code
For a school assignment I need to make a LED blink on/of 3 times then stop.
Now I have come up with the following code:
I think it's ''clean'' enough for what it has to do, but I have a feeling that ...
0
votes
1
answer
118
views
Modular Coding - What types of code portions to convert into library files? Rationale and methods?
I'm working on documentation and combining 2 pieces of code for testing parts of a larger project.
I'm supposed to comment and make the code very clear to people who will use it later and make sure ...
-1
votes
1
answer
156
views
Refactoring bad design?
Could someone help me how to refactor old very complex code ?
Original used just setup and everything else was in single ISR.
But as it was bigger and bigger, code is quite slow and quite random ...
1
vote
1
answer
2k
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
0
answers
450
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
votes
1
answer
1k
views
Optimizing for speed using const, static, constexpr etc in a function
In an ISR I want to keep things quick. At the same time I want to limit variable scope. Storage space is not a factor.
I have this line:
const char trigs[] = "aAbBcCdDeEfFgGhHiIjJ";
When this is ...