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
-1
votes
1
answer
248
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
0
answers
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
3
answers
832
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
3
answers
194
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
1
answer
135
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
2
answers
588
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
3
answers
2k
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
2
answers
4k
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
2
answers
2k
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 ...
1
vote
4
answers
255
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
3
answers
1k
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
2
answers
5k
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.
I ...
1
vote
1
answer
2k
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
I ...
1
vote
1
answer
3k
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 ...
1
vote
2
answers
535
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
5
answers
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 ...
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() {
...
-3
votes
1
answer
92
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
1
answer
89
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]
...
0
votes
1
answer
716
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
1
answer
351
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 ...
1
vote
2
answers
1k
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
2
answers
2k
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 ...
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 ...
0
votes
2
answers
961
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, ...
1
vote
1
answer
762
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 ...
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
2k
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
1
answer
1k
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
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
3
answers
89
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/...
2
votes
0
answers
579
views
Faster output of Serial.print() data coming from TSL1402R sensor [closed]
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 ...
3
votes
1
answer
135
views
Read time digits from DS1307 and run code based on return
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 ...
2
votes
1
answer
599
views
PCF8547 Wire.write explanations
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....
2
votes
1
answer
518
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
1
answer
2k
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);
...
1
vote
1
answer
168
views
Code Optimisation [closed]
I have tried fading the RGB Led with different colour format and sequence.
Working of Code :-
When value of number is 1, the Red LED fades gradually and glow up with the delay interval of 10ms with ...
2
votes
1
answer
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
/**********************************************************************...
1
vote
1
answer
97
views
ble_write slows down my sketch causing problems (Blend Micro & NRF8001)
I'm working on a prototype which uses the RedBearLabs Blend Micro with the NSC's nrf8001 ble chip, but have ran into a problem that I'm not sure how to solve.
I have two tactile buttons connected to ...
1
vote
1
answer
467
views
Help avoiding floating point math
I'm using a Texas Instruments DRV2605 haptic controller in my project to drive a LRA vibration motor. I would like to calculate the LRA rated voltage during run time using the formula below as ...
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 ...
1
vote
3
answers
379
views
A more elegant way to deal with timers without using delay()?
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 ...
0
votes
1
answer
893
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
...
3
votes
1
answer
2k
views
OOP vs procedural programming on arduino
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?
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 ...
6
votes
2
answers
572
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 ...
2
votes
3
answers
6k
views
Storing an array of function pointers
I have some Arduino code that looks like:
char packet = ser.read();
switch(packet){
case 'a':
someobj.somefunc();
break;
case 'b':
otherobj.otherfunc();
...
0
votes
2
answers
113
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
4
answers
125
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 ...
2
votes
2
answers
439
views
Possible to condense this if statement?
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 ...