Questions tagged [functions]

Segmenting code into functions allows a programmer to create modular pieces of code that perform a defined task and then return to the area of code from which the function was "called". The typical case for creating a function is when one needs to perform the same action multiple times in a program.

Filter by
Sorted by
Tagged with
5
votes
1answer
651 views

What does []() argument in function mean?

I am working in ESP8266 AsyncWebserver library and have been using this [](parameter) as an argument to some functions as shown below but does not really know what it means. Being a neophyte I am ...
4
votes
1answer
948 views

How to get variable from callback function?

There is a library davetcc / IoAbstraction. The only library working as expected with my rotary encoder. The problem that on rotation I can only see serial output with position data, but I cannot get ...
3
votes
1answer
60 views

Why no brackets after Interrupt routine inside attachInterrupt

I'm just curious as to why there are no brackets at the end of the ISR when attaching and assigning the interrupt command? attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE); ... ...
2
votes
1answer
166 views

What is touch_pad_get_status() supposed to return?

What I want to do: I have 7 touch buttons on my board (ESP32 TTGO T-Display) and I want to attach interrupts to everyone, calling the same function. In this function I would like to use ...
2
votes
4answers
404 views

Why do most arduino function return a -1 instead of a 0

Many arduino functions return -1 if something 'fails'. Serial.read for example returns -1 or the infamous ÿ when this function is called while the serial buffer is empty. As embedded C progammer, I ...
2
votes
2answers
2k views

How can I pass multiple variables to a function? [closed]

Using the FastLED library, I want to light up multiple lights at once. Currently I am passing in just one light to the function. spell( int LEDNumber ) { FastLED.clear(); leds[LEDNumber] = ...
2
votes
1answer
33 views

Input state is stuck HIGH when function called using input also uses same input to call another function

I'm writing a choose-your-own-adventure style project for a class project. My Arduino Uno is set up with a 16-digit LCD display and two pushbuttons with pulldown resistors. I'm wanting each function ...
2
votes
1answer
4k views

Cannot convert 'int (*)[size]' to 'int**' [closed]

Analyzing this and this answers under Arduino, which should be the proper way to pass a 2D array into a function? The function should be applicable to different sizes of 2D arrays. // NOT COMPILING ...
2
votes
1answer
61 views

Remove Function at Preprocessor Time

I have a single function for debugging messages, used through all the project. When KEYDEBUG is defined as 1 or 0, the function is enabled or disabled as required. #define KEYDEBUG 0 void debugging(...
2
votes
1answer
22 views

Serial doesent work when calling function from timer

You can try the code on your arduino UNO to better understand what is hapening in serial monitor! I want to calculate jacobian matrix from rotation matrix! float jakMATB[3][3] = { {0, 0, 0}, {0, 0, 0},...
2
votes
1answer
147 views

Remove blocking delay() toneMelody function?

I would like to change the delay() into a nonblocking function in the code below. I've implemented it (a part of a tutorial from arduino.cc) in my code, but the delay() is blocking the rest of my code....
2
votes
2answers
67 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 =...
2
votes
0answers
21 views

How to properly pass struct as argument? [duplicate]

This is my first question! i'm sorry if i'm doing something wrong. I'm trying to write a code that blinks some leds, but i cant quite work out how to use structs as arguments. This is the code: void ...
1
vote
3answers
206 views

Formatting variables from a variadic function

I have a function that concatenates a print line, however I am having trouble getting some of the strings to format correctly. Using Serial.print formats the output from keypad.getKey() correctly, ...
1
vote
1answer
161 views

Serial.print skips or hangs inside a "if" statement. Trying to call a function VIA serial uart

I have two microcontrollers talking to each other using serial UART pins. Now for some reason which I can not figure out why... doing a Serial.print() does not work in an "if" statement. And ...
1
vote
4answers
660 views

How do arrays work?

It's been days now I'm trying to figure out how arrays work in Arduino IDE. For better understanding, I assembled some pieces of code I found here and there to write this sketch: #define arrayLength(...
1
vote
2answers
124 views

Fail to return int value from a function

I have created myJSON library to simplify read and write values from/ to FLASH of my ESP8266 NodeMCU/ Wemos Mini. I do not want to "mess up" the question, posting my entire code/ library - but to try ...
1
vote
2answers
318 views

Initializing variables of a function [closed]

By playing with some examples I found the following construct: void Show(int nav = 0) // -1 top, 1 bottom and I wondered what effect it has to initialize the handover parameters in the function ...
1
vote
1answer
96 views

Can you create an array for functions? [closed]

Let's say I want to create 10 functions, named Function1, Function2 until 10. Each function is used if a certain variable is equal to its number. Rather than using individual functions, I was ...
1
vote
2answers
91 views

How to reset a variable just before entering a function? [closed]

I am facing a problem where the if condition in the function waterNowrun is able to execute once after the program starts as the variable v is 0 at the start, but upon starting it again the if does ...
1
vote
2answers
78 views

Arduino - Function -> return Cstr instead of string

I have following function for my Arduino: String readLine() { String received = ""; char ch; while (myFile.available()) { ch = myFile.read(); if (ch == '\n' or ch == '\r') { ...
1
vote
1answer
252 views

How do i improve this Code? (or how to pass a Char String into a Function to behave like a Variable)

Sorry i am a beginner and amateur when it comes to coding, can somebody help me to make the code inside the loop() less "monkey-like" by using a proper function? I have tried a lot and at ...
1
vote
1answer
79 views

Doing several things at the same time with functions, objects or interrupts?

First of all, thank you for taking the time to read this. I am a newbie, so apologies (done examples on arduino website, basic C++). I'm trying to get an idea of what the best way to structure a ...
1
vote
1answer
36 views

Receiver sleep mode RDA5807M

Good afternoon guys! I have a radio receiver connected to an arduino and I want it not to work all the time, but to turn on by the arduino timer for a certain time, and the rest of the time the ...
1
vote
2answers
1k views

How do you remove an item from array arduino?

Is there a way that can change {3, 5, 3, 6} to {3, 5, 6} with a function? I checked all the arduino forums but they don't give the answer i need (just delete 1 item from an array with a function like ...
1
vote
1answer
67 views

Running commands from string

I have a string: string s="digitalWrite(8,LOW);" Is there any way to run it as a code?
1
vote
2answers
497 views

7-Segment Display with WS2312B and FastLED

I thought of the idea of using the WS2312B LED strip to drive a custom 7-Segment display. The project will require a large number of these displays, and driving them with a single Arduino (I'm ...
1
vote
1answer
366 views

Function to control servos

I'm trying to control 6 servos while writing a lean code. I think the function call isn't correctly yet and I think that the variables (pos_n, open_n, servo_1) in the for loop do not get inserted, but ...
1
vote
0answers
48 views

Arduino PID Library

I hope you're doing well. I have some questions regarding the PID functions. Can you please explain to me the following commands? I shall be very thankful to you. PID myPIDX(&InputX, &OutputX,...
1
vote
1answer
131 views

Using functions in header files [closed]

I want to include a function in my sketch I moved to a .h file. I did it as follows: My code: #include "header.h" void setup() { fct(); } void loop() { } My header: #include "...
1
vote
0answers
42 views

Peculiar relay behavior from POST

I am trying to toggle on/off between two separate HTTP request. One submit button to turn it on, and another separate submit button to turn it off. I expect the device to launch in the off/low 0V ...
1
vote
0answers
77 views

Get value from protected member of class

i'm really new to Arduino. I have Arduino Mega, a USB host shield and a barcode scanner. I'm using this library >> link and modified it example so barcode scanner worked (Got it from here) to read ...
0
votes
2answers
633 views

Problem with a function having a parameter with a default value

I've got a problem using a default value for a function parameter. This code gives "'blink' was not declared in this scope": void loop(void) { blink(12, 2, 1000); } void blink(const ...
0
votes
2answers
778 views

can't return float value from function

I want to put a 2 seconds delay to temperature reading but the millis() delay is not applying. #include <OneWire.h> #include <LiquidCrystal_I2C.h> #include <DS3231.h> #include <...
0
votes
1answer
112 views

How to use this confing function

For INA3221 current sensor, I found this library, however, I don't understand how to use the INA3221SetConfig() function as defined in the .cpp file link. void SDL_Arduino_INA3221::INA3221SetConfig(...
0
votes
3answers
329 views

Run multiple function with different argument/return structure by one function pointer

Edit: I tried a simple code in Arduino IDE which should've done in the first place instead in doing it in a desktop compiler. Because the results are different by experiment. In the desktop C compiler,...
0
votes
2answers
74 views

Why is function mentioned in global declaration?

I came accross this code for writing data into EEPROM in arduino. Why is void printTemp();, void clearEEPROM(); and void writeTemp(); functions are mentioned in global declaration? How will this help? ...
0
votes
1answer
42 views

How to access variables by variable? (I.e. variable variables?)

Can Arduino handle variable variables? function getLetterCode(char input) { int letter_a = 17; int letter_b = 42; int letter_c = 50; return letter_$$input$$; //pseudocode } This is ...
0
votes
2answers
142 views

Is it possible that function will get a parameter that can be int OR char?

I'm trying to create a function that enters a key/value into a JSON file. But, since value can be an int or char, I don't want to create 2 functions for each type. Is there a way ? void setValue(...
0
votes
1answer
37 views

This website in Arduino lists language references like functions and structures. What makes structures there different from functions?

https://www.arduino.cc/reference/en/ Language Reference Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure. functions For ...
0
votes
1answer
134 views

Passing a time_t array as a function parameter

I'm trying to log reconnections and disconnections of a network. For that I've created 2 time_t arrays to store such occurances: const int logSize = 10; time_t conLog[logSize]; time_t disconLog[...
0
votes
1answer
180 views

Error taking address of temporary array when passing compound literal array

I have an Arduino sketch that passes an array into a method as a compound literal, but for some reason I keep getting the following error: void setup() { } void printConcatLine(char chunks[][20]) { } ...
0
votes
3answers
53 views

problem using one function to blink different leds

I wrote a function to blink an led using millis and want to blink multiple leds with the same function but with different delay intervals. When I call this function thrice to blink 3 different leds, ...
0
votes
1answer
75 views

Function pointers that call one function

I'm reading a library file timer.h for STM core library that I just installed in Arduino hardware folder. Of course, there has to be a lot of questions for me as a beginner about many approaches/...
0
votes
1answer
2k views

Is there an Adafruit function to invert the colors of my screen for Arduino language?

I am trying to invert the color of my OLED display screen once a button is pushed. I have the button situation figured out but I can not find a function that inverts the color. I am using these two ...
0
votes
1answer
43 views

Compute the size of an array that was passed as a parameter to a function

When passing an array to a function, you usually pass the length of the array along with it. You can compute the size of the array with sizeof(myArray)/sizeof(myArray[0]), which works just fine. ...
0
votes
1answer
58 views

Strange artifact in writing to serial while executing loop

Hope you can see this. https://gist.github.com/DevilWAH/60ad144d2858f53845ac2e80f84bb070 The main file in this is "morse-led-rfid" which is taking reading data of an RFID tag and then displaying ...
0
votes
1answer
54 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 ...
0
votes
1answer
33 views

CPP Function param use function with multi params [closed]

I want to specify a function with parameters for a function parameter, but it doesn't come together. //////////////////////////////////////////////////////////////////////// // Example funch... void ...
0
votes
1answer
83 views

Error Compiling for board Arduino Nano while using Adafruit GFX

Whys is this happening to me?? I have to meet a deadline please help!! Here is my code. #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h&...