41 votes

Are global variables evil in Arduino?

They aren't evil per se, but they do tend to get overused where there is no good reason to use them. There are plenty of times when global variables are advantageous. Especially since programming an ...
Majenko's user avatar
  • 105k
24 votes

Are global variables evil in Arduino?

It's very difficult to give a definitive answer without seeing your actual code. Global variables are not evil, and they often make sense in an embedded environment where you typically do a lot of ...
Edgar Bonet's user avatar
  • 42.8k
17 votes
Accepted

Reading several rotary encoders

Library and schematics are at https://github.com/maxgerhardt/rotary-encoder-over-mcp23017. To read a rotary encoder (i.e., detect state changes of the knob), there are two possible implementations: ...
Maximilian Gerhardt's user avatar
16 votes
Accepted

Arduino Countdown without using delay()

Background The function millis () returns an unsigned long, which is the number of milliseconds since the processor was reset (until it overflows). See: http://arduino.cc/en/Reference/Millis ...
Nick Gammon's user avatar
  • 38.1k
16 votes
Accepted

Convert int to char[]

itoa is the best route: int num = 1234; char cstr[16]; itoa(num, cstr, 10); -> "1234" You can read more about using it here. If you want more control over the format of the number (and don't ...
Majenko's user avatar
  • 105k
16 votes
Accepted

Can I force Arduino not to be powered by USB?

The best way of doing this, if you want your USB connection to still work, is to remove the P-channel MOSFET T1 from the board. You can't just disconnect the power from the USB port since that power ...
Majenko's user avatar
  • 105k
15 votes

What is the best technique to design a 20 push button circuit

For 20 buttons you can use a Keypad Matrix arrangement (plenty of examples online) where you would use only 9 pins (5 columns of 4 rows is 5+4 pins). It's slightly more involved that simple 1:1 ...
Majenko's user avatar
  • 105k
12 votes
Accepted

ArduinoUNO/MEGA, Is there a way to exceed 256 in analogWrite

Section 16, "16-bit Timer/Counter1 with PWM" of Atmel-8271J-AVR- ATmega-Datasheet_11/2015 describes using Timer/Counter-1 for 16-bit PWM, including sample code in both C and assembler. It might take a ...
JRobert's user avatar
  • 15.2k
12 votes
Accepted

16x2 LCD showing blocks under text

The row of white blocks is what you get before the screen's initialized. It's normal. The shadow behind the characters is because you have the contrast turned up too high. Adjust it down a little.
Majenko's user avatar
  • 105k
12 votes
Accepted

What are these stray resistors on the Arduino Mega schematic?

Those are unused resistors in resistor arrays. The arrays have 4 resistors side by side in a single package. Not all of the elements are always needed, but they have to be in the schematic anyway ...
Majenko's user avatar
  • 105k
11 votes

Running Arduino mega on 5V power supply

The 6-12V range applies only to the onboard voltage regulator(if you provide power via Vin or the barrel jack). The Arduino itself runs on 5V and the regulator needs a bit headroom above that to do ...
chrisl's user avatar
  • 16.1k
10 votes
Accepted

Delay() and millis() functions don't work in cpp external files

The Arduino framework does some initializing for you before going into setup() and loop(), for example configuring Timer0 correctly for millis(), delay() and siblings. This is done in the main() ...
chrisl's user avatar
  • 16.1k
9 votes

ArduinoUNO/MEGA, Is there a way to exceed 256 in analogWrite

The simplest way is to change your value range to 0-255. You can't change analogWrite's range, since that is fixed in the core software. analogWrite(5, val >> 2); The >> 2 bit-shifts the ...
Majenko's user avatar
  • 105k
9 votes
Accepted

How to pass 'Serial' as an input argument in Arduino function?

The Serial object is the end of a chain of classes, each with a different set of functionality. Print -> Stream -> HardwareSerial => [Serial] Since you are reading from it the best class to ...
Majenko's user avatar
  • 105k
9 votes

My program is using too much memory

There are lots of repetitions in your code. This is something you should generally avoid, not only because of memory usage, but also, and most importantly, because of maintainability. The most common ...
Edgar Bonet's user avatar
  • 42.8k
8 votes
Accepted

Can I make delayMicroseconds more accurate?

As explained in the previous answers, your actual problem is not the accuracy of delayMicroseconds(), but rather the resolution of micros(). However, to answer your actual question, there is a more ...
Edgar Bonet's user avatar
  • 42.8k
8 votes
Accepted

What's the function of this transistor on Arduino Mega?

It's a P-channel MOSFET. It's job is to act as a "dropless" diode. The principle is this: MOSFETs have a built-in diode across them in reverse bias (an effect of the chemistry) The P-channel MOSFET ...
Majenko's user avatar
  • 105k
8 votes
Accepted

Faster Arduino Mega

You should begin working with 3.3V. 5V is getting rarer and rarer as far as "real world" applications go. Many devices that you have been using at 5V will also work at 3.3V (such as the MAX22500E). ...
Majenko's user avatar
  • 105k
7 votes

How do I split an incoming string?

Simplest solution is to use sscanf(). int id1, id2, id3; int pos1, pos2, pos3; char* buf = "1:90&2:80&3:180"; int n = sscanf(buf, "%d:%d&%d:%d&%d:%d", &id1, &pos1, &...
Mikael Patel's user avatar
  • 7,959
7 votes
Accepted

Proper use of * and & when passing objects in methods

The method you have implemented, passing by pointer, is one of the two normal methods, yes. The other is passing by reference and is specific to C++, not C. Under the hood the end result is the same ...
Majenko's user avatar
  • 105k
7 votes
Accepted

How to Sort Elements of Array in Arduino Code?

As mentioned in the comments, qsort is a good option: // qsort requires you to create a sort function int sort_desc(const void *cmp1, const void *cmp2) { // Need to cast the void * to int * int a ...
001's user avatar
  • 943
7 votes
Accepted

Confused between SPI and I2C for SSD1306 OLED

SCK and SCL are the same thing. Serial ClocK Serial CLock They seem to be interchangeable. The fact you have SDA (Serial DAta) as opposed to separate SDI / SDO or MOSI / MISO pins means it's I2C. ...
Majenko's user avatar
  • 105k
7 votes
Accepted

Testing Conditions on Port Bytes - ignoring certain bits?

You need to use boolean arithmetic. Use the AND operator (&) to combine the incoming value AND the "mask" value (the value of the bits you do care about) and see if it equals the mask bit: byte ...
Majenko's user avatar
  • 105k
7 votes
Accepted

How to find out the maximum used stack space?

Knowing the stack size is pretty easy: the stack pointer is initially set to RAMEND and goes down as the stack grows. Thus: static inline size_t stack_size() { return RAMEND - SP; } should I ...
Edgar Bonet's user avatar
  • 42.8k
7 votes

How to find out the maximum used stack space?

Initialize the entire SRAM space from the top of the heap to the bottom of the stack, either during setup, or in a modified version of the Arduino-provided main() function, with some unlikely pattern, ...
JRobert's user avatar
  • 15.2k
6 votes

NEO 6M GPS speed

The speed value is correct, like TisteAndi said. Your GPS location "wanders" around the same way. You can simply ignore small speed values. One problem you may not be aware of yet: the printing isn'...
slash-dev's user avatar
  • 2,029
6 votes

Arduino Mega + SIM808 GSM + GPS Module + Program Switch ON without pressing Push Button?

There is a simple way of power the module without pressing the button. There is pin call D9 at the bottom right corner of the module. Just power the pin by Arduino digital pin or 5v pin around 3, 4 ...
Senura's user avatar
  • 61
6 votes
Accepted

Using a lot of interrupts on a single Arduino

On a Uno, all of the digital and analog pins can trigger a pin-change interrupt. That gives you 20 inputs straight away. The Mega also has pin change interrupts but not on all its pins. The pins that ...
Nick Gammon's user avatar
  • 38.1k
6 votes

Voltage on analog pin arduino when arduino turned off

All the GPIO pins have protection diodes in them. The are arranged, effectively, like this: simulate this circuit – Schematic created using CircuitLab The internals of the ATMega chip can be ...
Majenko's user avatar
  • 105k
6 votes
Accepted

How Arduino IDE works?

You can turn on verbose compiling. This will show all the compiler commands used by Arduino IDE. The ino file is converted to a .cpp file (which you can see in the verbose output). Basically the ...
jantje's user avatar
  • 1,382

Only top scored, non community-wiki answers of a minimum length are eligible