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
0
votes
2answers
24 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
25 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 ...
0
votes
3answers
15k 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
1answer
53 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
72 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 ...
0
votes
1answer
68 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
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
votes
1answer
124 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 ...
0
votes
0answers
130 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
104 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 ...
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 ...
0
votes
0answers
42 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 * ...
0
votes
3answers
2k 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 ...
0
votes
3answers
101 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 ...
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 ...
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
votes
1answer
203 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
639 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
53 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
140 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%...
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 ...
0
votes
3answers
408 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; ...
1
vote
1answer
497 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 ...
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 + ...
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 ...
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 ...
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
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 ...
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 ...
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 ...
0
votes
3answers
233 views

Breaking up for loop with ISR

I am curious if the following scenario is possible. EICRB |= (0<<ISC60)|(0<<ISC61); EIMSK |= (1<<INT6); ISR(INT6_vect) { myISR(); } void myISR() { digitalWrite(led_off, ...
0
votes
2answers
3k views

Controlling the Serial Monitor for 10 seconds

I am an amateur with Arduino and coding and I am learning in my lab class. My project consists of using a photoresistor to measure the brightness of a white LED. I have the following circuit built. ...
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
1answer
494 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
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 ...
0
votes
1answer
92 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
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 ...
0
votes
2answers
351 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 ...
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 ...
-3
votes
1answer
55 views

Create an a message from two inputs[Programming Help] [closed]

I'm a bit new to this and I have the following function that I'm having trouble getting it to compile. The intent is to create a message to write out to a software serial port. Inputs are two int ...
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] ...
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 ...
0
votes
1answer
398 views

Switch Statement Alternative

I am working on a project where I've mapped an IR remote control to certain functions. I am currently using a massive switch statement to control the program based on the IR remote button pressed. Is ...
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 ...
0
votes
2answers
387 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
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 ...
0
votes
2answers
4k views

Convert uint8_t array to ascii character for serial transmission

I am trying to send packets of via bluetooth data that include 6 bytes read from a sensor and stored in a uint8_t array. Instead of sending over the data as numerical digital, I would like to send the ...
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*...
0
votes
2answers
334 views

Is this example for playing music without delay correct?

I've followed the directions from this answer, to try and play music without delay. This is my code so far: #include "pitches.h" int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, ...