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
1
vote
0answers
20 views

Can i run my python based virtual Assistant using esp32 I want it use it as a user endpoint? [closed]

I have a working project which is python based virtual assistant and now I am thinking to make it more accessible than carrying my laptop. I want to make a device like alexa echo dot more or less.. ...
1
vote
0answers
32 views

Expected initializer before 'myservo' [closed]

Hello i am getting into Arduino and want to get my code tidy. I dont know how but thought by #include "Stepper.h" and "Servo.h" would work. The programm shows it works. But now it ...
1
vote
0answers
42 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 ...
1
vote
1answer
46 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 ...
0
votes
1answer
19 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
2answers
192 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
0answers
35 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
0answers
49 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
1answer
41 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
3answers
56 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; ...
0
votes
2answers
64 views

Code Optimization: Interrupt when a value reaches 0

I have this working program that I wish to improve 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 using ...
3
votes
2answers
485 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 <= ...
2
votes
2answers
44 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
votes
1answer
46 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 ...
5
votes
1answer
306 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 ...
1
vote
0answers
45 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) ...
1
vote
1answer
63 views

Looking to optimize this code and add a loop

So I want to optimize this code and add a loop to the two red LEDs. The process is green LED is flashing, press button, yellow LED blink for 3 secs and then alternate flashing between the two red LEDs ...
0
votes
1answer
43 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 ...
0
votes
0answers
262 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
2answers
127 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 ...
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 ...
0
votes
0answers
31 views

combinate two codes [duplicate]

i am trying to put these two codes together someone would be so kind to help me. ""#include const int photores = A0; // Photo resistor input const int pot = A1; // Potentiometer input const int led =...
1
vote
0answers
31 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 ...
2
votes
1answer
53 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 ...
-3
votes
1answer
59 views

What is PWM actually? [closed]

tcommand=(pwm_value-480.0)/8.22; flapmag=(pwm_value-880)/41.0+10; In this piece of code used in arduino, I don't understand why PWM value is subtracted and divided like this. This is not my code. Any ...
1
vote
1answer
120 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 ...
0
votes
0answers
56 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 ...
2
votes
1answer
81 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
votes
2answers
34 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 ...
0
votes
1answer
70 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
1answer
80 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
1answer
444 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
votes
1answer
339 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 ...
1
vote
0answers
357 views

Optimizing Teensy 3.6 for data sampling?

So, I have a very basic project where I cycle through 8 IR individual transmitters. After setting the individual transmitter high, I cycle through all 8 IR receivers and print their values, RX_CYCLES ...
0
votes
2answers
172 views

28 BYJ stepper motor runs faster using delay() and little slower using millis(), why?

I'm working to develop a code for the stepper motor to use it in a project. And since using delay() isn't as efficient as millis(). So I first developed the code that use delay with 1ms delay between ...
0
votes
0answers
55 views

Combining sketches

I have two codes for my to sensors and they work separately. However, I need to combine them in one program please can you help? The pulse code /* PulseSensor Starter Project and Signal Tester * ...
1
vote
0answers
219 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
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
0answers
198 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
votes
1answer
331 views

how to code LED turning signals on smart car [closed]

this question is regarding how to add turn led signals on arduino powered smart car. I have 4 leds to give turn indications on my smart car(line follower, obstacle avoider, Bluetooth mobile controlled ...
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 ...
0
votes
2answers
77 views

Merging of two functions

This is my Arduino code that checks the water Level of two different water reservoirs via 2 water Level Sensors A and B: #define Water_Level_A 5; #define Water_Level_B 6; boolean Water_Level_Check_A(...
-1
votes
1answer
166 views

Need help optimizing snake game code for Arduino Nano

Here's the code: https://pastebin.com/ypWe4NZw This is the output when I compile: Sketch uses 13086 bytes (42%) of program storage space. Maximum is 30720 bytes. Global variables use 1456 bytes (71%...
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 ...
0
votes
3answers
710 views

Switch case does not work after booting system

int value1; int value2; int runs1 = 0; int runs2 = 0; #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x3F, 16, 2); int inPin = 6; int val = 0; int push_count = 0; ...
2
votes
3answers
148 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 + ...
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 ...
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 ...
2
votes
3answers
906 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 ...
-1
votes
2answers
3k views

Counting number of people inside a room [closed]

I want to count the number of people in a room using a microcontroller (Arduino Uno). I have 2 ultrasonic sensors at a doorway about a 1.5m apart from each other. I want to increment the number when ...