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
3answers
104 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 ...
0
votes
1answer
178 views

Shortening large chunks of code

Some of the code that I have is unbearably long and drives me insane. I don't know if there were simpler ways to doing what I am doing. long turn1; long turn2; long turn3; long turn4; long turn5; ...
0
votes
2answers
409 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: ...
0
votes
4answers
72 views

Refactor to save bytes?

I'm a painful 128 bytes of my max for this Trinket. Any refactoring I could do to get down the size? #include <Adafruit_NeoPixel.h> #include <Wire.h> #include <RTClib.h> #define ...
0
votes
3answers
83 views

Simple question about && vs ||

I want to skip all '\r' and '\n' chars and I have IF statment: char c = Serial.read(); if (c != '\r' || c != '\n') { // <- This line interesting mAnswer[mAnswerLength] = c; mAnswerLength++; ...
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(...
0
votes
3answers
445 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; ...
0
votes
1answer
1k views

Is it possible to pass HIGH and LOW values as parameter in a function?

I've created the following function: void runMotor(int _dir, int _step, int wait){ digitalWrite(_dir, HIGH); for (int x = 0; x < 3200; x++) { digitalWrite(_step, HIGH); ...
0
votes
2answers
349 views

How to prevent inclusion of unnecessary libraries?

How do you save space by preventing the inclusion of unnecessary binaries? When I compiled the ROS Blink example, I received the warning: Global variables use 1,951 bytes (76%) of dynamic memory, ...
0
votes
3answers
307 views

How to store IR data needed for AC controll in less space?

I'm trying to controll an AC unit but the issue is space - right now, I store the data like this: consts PROGMEM unsigned int Signal_Heat_Fan0_Temp30[] = {233,636,17884,3032,8944,500,496,492,1496,524,...
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. ...
0
votes
2answers
362 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 ...
0
votes
3answers
56 views

Merging strings and constants causing scope errors

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/...
0
votes
1answer
636 views

Faster and lighter approximate timing code without using timer

I want to use loop() iterations counter to execute some tasks periodically or tasks like debouncing. My other sketch is running every loop() at least 20 ms, and if i am using 5 ms time to wait to ...
0
votes
1answer
404 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 ...
0
votes
1answer
1k views

Multiple if statements - stuck

I have a robot that uses two motors to skid steer. The robot gets input from three ultrasonic sensors (left, middle, and right) and a passive infrared sensor. If the robot detects a person in front of ...
0
votes
3answers
16k 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
2k views

Using for loop to set pinMode

I notice problems with my output pins when I use this method of setting their pinMode: int allOutputPins[] = {3, 4, 9, 10, 5, A3, 11, 12, 7, 8, A1, A2}; for(int a = 0; a < sizeof(allOutputPins); ...
0
votes
1answer
83 views

Sweep with two shift registers

I am working with 8 dual color LED's and 2 shift registers. My connections are in place. But I do not understand how to work with 2 shift registers. So I am trying to make this: turn the first led on,...
0
votes
2answers
390 views

releasing memory in sub-function

I'm doing a program who get a line from the SD and shows it, this function is called getData() FULL CODE: #include <SD.h> #include <MemoryFree.h> File myFile; int stringIndex = 0; int ...
0
votes
1answer
126 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 ...
0
votes
2answers
109 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
3answers
252 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
3answers
3k 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
4answers
215 views

Can I short this code?

int r = 12; int g = 11; int b = 10; int sw = 4; int x; int c = 0; void setup() { pinMode(r, OUTPUT); pinMode(g, OUTPUT); pinMode(b, OUTPUT); pinMode(sw, INPUT); pinMode(13, OUTPUT)...
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 ...
0
votes
3answers
1k views

Arduino: how can I stop while() when bluetooth send a data?

I have learned Arduino for a month, and I want to bulid a car with 2 modes, Remote and Auto. Auto mode needs to have while() loop, so it can receive data from sensors. When I choose Auto mode via ...
0
votes
2answers
2k views

Program button to switch between two values

There is a function on one of my programs I want to be switch on and off by pressing a button. The button is unbiased otherwise I would set the function to read the button's status of HIGH or LOW. So ...
0
votes
1answer
710 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 ...
0
votes
1answer
41 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
16 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
1answer
27 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
2answers
26 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
54 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 ...
0
votes
0answers
156 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
0answers
43 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
2answers
354 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, ...
0
votes
2answers
62 views

Interrupt numbering

I'm using an Arduino Mega and four of its interrupts. Each interrupt is only used to start/stop a timer, depending if it's rising or falling, so effectively runs the same function. Does anyone know if ...
0
votes
1answer
190 views

How would I refactor this code, given how similar most of the lines are?

I have a set of buttons and a set of NeoPixel LEDs. There's lots of very similar constants (button1pin, button2pin, etc) so there's almost certainly a more efficient way of coding this. Any ideas? #...
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
votes
1answer
148 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
votes
1answer
784 views

Why is integer-to-string not working in this sketch?

I am using a Pro Mini to read analog values and to send them via serial. Goal of the sketch is to stream these values to Serial as fast as possible, to obtain a steady stream of at least 1000 ...
-1
votes
1answer
73 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
votes
1answer
153 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
votes
1answer
215 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
votes
2answers
1k views

SD Library writing char in a file

I'm trying to write into a file using this code from the example of the library: String dataString = ""; int sensor = 0; dataString += String(sensor); dataString += ","; sensor +=1; File dataFile = ...
-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 ...
-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 ...
-3
votes
1answer
136 views

How do I learn C? (was Computer project) [closed]

Can you help me writing a code for this question guys?? You have an Arduino Uno with 8 LEDs connected to digital pins 2 to 9. Create an array of 8 elements. Fill it with random numbers between 2 and 9 ...
-3
votes
1answer
41 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 ...