Questions tagged [programming]

The process of designing and writing source code as part of a program (or sketch) for Arduino. For questions about uploading code to an Arduino board, use the [uploading] tag instead.

Use this tag for questions regarding programming (writing code) for Arduino. This can include problems with code you've written for Arduino or questions about how/why Arduino code acts in specific ways.

Where a question relates to a particular language, you can also use language-specific tags, such as .

Programming questions that are not specific to Arduino (e.g. general questions about a programming language) should be asked on Stack Overflow instead.

Please use the tag for questions regarding uploading sketches to Arduino devices.

Note: With this tag, syntax highlighting will be enabled. For more information, see the syntax highlighting help section.

1657 questions
13
votes
4 answers

Getting a truly random number in Arduino

What is the best method to get a truly (as opposed to pseudo) random number in Arduino, or at least the best possible approximation? From my understanding, the function randomSeed(analogRead(x)) it's not random enough. If possible the method should…
Rexcirus
  • 253
  • 2
  • 8
13
votes
6 answers

Code for one-time execution in Arduino

Question: Where do I put code if I want it to execute only once per Arduino startup? Background: I tend to use my Arduino to perform tasks that should be executed once per power cycle. (For example, my robot should only drive forward once every…
apnorton
  • 533
  • 1
  • 5
  • 22
11
votes
4 answers

What is the best way to define an I/O pin?

I read definitions like const int ledPin = 9; and also #define ledPin 9 I know that a definition like int ledPin = 9; is a bad practice if you're not going to change it (which you usually won't), though I've see this several times in Arduino…
Joris Groosman
  • 1,171
  • 3
  • 11
  • 25
10
votes
3 answers

What is the difference between declaring a variable outside of loop and declaring static inside loop?

These are two ways I can hold a variable outside of loop (or any function). First, I can declare it with global scope outside of loop: void setup() { Serial.begin(9600); } int count = 0; void loop() { Serial.println(count); count++; …
Cybergibbons
  • 5,350
  • 7
  • 33
  • 51
9
votes
2 answers

What will happen when I use an invalid pin number?

Related to: What happens if there is a runtime error? This question is similar to the one above, however this is an alternate situation: int pin = 999; pinMode(pin, OUTPUT); digitalWrite(pin, HIGH); What would happen in this instance? The…
Anonymous Penguin
  • 6,285
  • 10
  • 32
  • 62
8
votes
4 answers

Can't use enum as function argument

I declared my enum and called it Dir. I wanted to use value of that type in my function Move(). Here's the code: enum Dir { forward = 1, backward = 2, left = 3, right = 4 }; void Move ( Dir direction , int distance ) { direction =…
mnj
  • 209
  • 2
  • 5
6
votes
2 answers

How do I split a number into individual digits?

How would I split part1 into 2-3 digits and put them into num1, num2, and num3? int FMSTATION = 10250; int update_display() { int fm_station = FMSTATION ; int part1; int part2; part1 = (FMSTATION / 100); part2 = (FMSTATION % 100); …
Austinthemighty
  • 63
  • 1
  • 1
  • 3
5
votes
1 answer

Why use .h files with .cpp?

At school, our teacher always taught us to use .hpp (C++ Header files) with our .cpp (C++ files). But Arduino seems to use .h files (with .cpp links?). When someone is programming in C (without C++ compiler) they have the chance to get linked to a…
aaa
  • 2,695
  • 2
  • 24
  • 40
5
votes
1 answer

How do you combine 2 different sketches together

(Before you down-vote this, note that it is a "ringer" to answer a FAQ, with a self-answer.) This question comes up all the time. How do you combine 2 different sketches together? Say I have a sample sketch for a DTH temp/humidity sensor and another…
Duncan C
  • 5,682
  • 3
  • 17
  • 29
5
votes
2 answers

if/else statements without comparison operators and curly braces

I recently came across this code in a tutorial. The code works, but this form of syntax seems to differ quite a bit from https://www.arduino.cc/reference/en/language/structure/control-structure/if/, as it's lacking comparison operators and curly…
Erik
  • 261
  • 1
  • 12
5
votes
1 answer

How do I convert code between Arduino platforms?

What documentation is available regarding the differences between the different Arduino platforms? For example, I have some code written for an Arduino Teensy that I would like to run on an Arduino Mega. Is there documentation on the capabilities…
Mark Harrison
  • 559
  • 3
  • 14
  • 23
4
votes
1 answer

Huge nested loop in Arduino

I am completely new to programming and I am trying a brute force programming on my garage door opener. I want to use an Arduino for learning purposes. I wrote the following program that finally did what it should and now I am wondering whether there…
AndreasL
  • 41
  • 1
3
votes
2 answers

Waking Raspberry Pi on time schedule using Sleepy Pi

I'm have a Sleepy Pi and am having extreme difficulties trying to program a wake/sleep cycle. Sleepy Pi is an Arduino board with a RTC. My ultimate goal is for a time-lapse project. I have the Raspberry Pi programmed and operational to take a…
3
votes
1 answer

Unexpected behavior of ++myCount

I'm new to Arduino and C. This behavior is not what I'd expect: Serial.begin(9600); while ( ! Serial ); int myCount = 0; for (int i=0; i<10; i++) { myCount = min( ++myCount, 8 ); Serial.print( myCount ); Serial.print( " " ); } I…
StormDog
  • 31
  • 2
3
votes
3 answers

Can I improve this code (replacing delays with something else)?

Building upon my previously solved question Ultrasonic Sensor range finder help (Solved). Is there a better way to make the LED blink than using a delay and stopping all the rest of the code? For example, the LED is set to turn on, then shortly…
alex416
  • 33
  • 3
1
2 3
11 12