# Another CheatSheet (Draft)

**URL:** <https://forum.arduino.cc/t/another-cheatsheet-draft/1032892>\
**Category:** Programming\
**Created:** [September 17, 2022, 8:38pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892 "2022-09-17T20:38:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 17, 2022, 8:38pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/1 "2022-09-17T20:38:35Z")

</div>

I created / modified an Arduino chestsheet to include those items I often forget the syntax. And to aid in keeping my code more consistent.

This is a draft, if you have a mind to, please provide constructive criticism and/or suggestions.

I am planning a rear sheet that will include the Arduino Reference Index. The goal is to have a quick reference for a complete list of functions etc.

[CheatSheets\_draft\_v01.pdf](https://forum.arduino.cc/uploads/short-url/i8f1EdZvWx9wDpTCp7ltmx5S7Z2.pdf) (420.5 KB)

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [September 17, 2022, 9:03pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/2 "2022-09-17T21:03:48Z")

</div>

```
 while loop return x; // Or ‘return;’ for voids.

```

They are _function_s not _void_s.

A void is a return type or a completely empty space. 😉

```
50 days overflow

```

of _millis_(). True but almost 100 percent irrelevant, if you do the maths correctly. Just sayin'.

```
 unsigned char (0 to 255) use byte

```

Nooo! Old school, sorry. My favorite data type. Known everywhere by everyone, I can actually type _unsigned char_ faster than I can type byte and be sure I remember what it means...

```
 volatile - use RAM (nice for ISR)

```

most variable variables use RAM, volatile has special use for ISR and, for all I know a few other niche areas. Just say for safe sharing ISR variables or something like tht.

```
* value pointed to

```

Maybe a cogent fleshier example, I'll ask the crew.

```
 //Write High to inputs to use pull-up res

```

Lose that... true but obsolete by INPUT\_PUKLUP. Comes in with direct port manipulation, which I think I see you get to…

except you leave off the DDRX register, which is the third leg along with PORTX and PINX.

_But a heroic effort draft document_, it is amazing how compact C is and how much of what many ppl ever do with an Arduino can fit like this on a reference card. Not _cheating_, no.

I am reminded of those laminated 8 x 11.5 cards covering, for example, organic chemistry. LOL, worthless.

Here, much more claim to usefulness.

Looking forward to being picky about the next version.

Oh, can you work

```
( ) parenthesis, parentheses
[] brackets
{ } braces

```

on in there somewhere? 😉

Hot dogs are up. Just in time, CU.

a7

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 17, 2022, 9:13pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/3 "2022-09-17T21:13:01Z")

</div>

Thanks John.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 17, 2022, 9:16pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/4 "2022-09-17T21:16:21Z")

</div>

Maybe add **Casting** examples too ? 🤔

Maybe how to turn off internal pull-ups also.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [September 17, 2022, 9:23pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/5 "2022-09-17T21:23:58Z")

</div>

> Write High to inputs to use pull-up resistors

AVR specific

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 17, 2022, 10:47pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/6 "2022-09-17T22:47:43Z")

</div>

> [@alto777](#):
>
> `unsigned char`

Thanks for the great suggestions. You've pointed some areas that need attention 🙂

about "unsigned char" vs "byte" I couldn't find anything that identified more that just preference. I have been using uint8\_t and am trying to move to more common terminology.  
BTW if I were to start using "unsigned char" I would:  
#define unsigned char uchar

I'll try to better describe volatile.

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 17, 2022, 10:49pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/7 "2022-09-17T22:49:29Z")

</div>

Thanks, I'll add Casting as soon as I figure how to clearly describe it (and understand it).

Turn off internal pull-ups is good 🙂

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 17, 2022, 10:53pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/8 "2022-09-17T22:53:27Z")

</div>

Thanks, I will either remove the "write High" option or add an AVR notation. Really I think removal is best else it could cause confusion.

In general the goal of this sheet is to make my code more consistent.

I also plan to expand the ++j and j++ section

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 17, 2022, 11:04pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/9 "2022-09-17T23:04:40Z")

</div>

On top of what other said

- You have the Qualifiers twice, one is probably enough 😉

- On arrays, as this one is for pins

```cpp
int myPins[] = {2, 4, 8, 3, 6};

```

I would suggest

```cpp
const uint8_t myPins[] = {2, 4, 8, 3, 6};

```

As pins are usually constant and don’t require more than a byte to store…

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 17, 2022, 11:10pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/10 "2022-09-17T23:10:40Z")

</div>

Thanks I'll trash the second "Qualifiers" and instead duplicate "Constants" 🙂  
seriously thank you for noticing.

I'll made some note somehow that pins should/could be "const".

```cpp
int myPins[] = {2, 4, 8, 3, 6};

I would suggest

const uint8_t myPins[] = {2, 4, 8, 3, 6};
As pins are usually constant and don’t require more than a byte to store…

```

I was trying to get away from uintn\_t so it could be byte.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 17, 2022, 11:20pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/11 "2022-09-17T23:20:39Z")

</div>

> [@JohnRob](#):
>
> I was trying to get away from uintn\_t so it could be byte.

The advantage of uint8\_t is that it is part of C++  
byte is [just a #define for uint8\_t anyway](https://github.com/arduino/ArduinoCore-avr/blob/2ff00ae7d4e85fa422b7918ee12baf56a1f3006e/cores/arduino/Arduino.h#L127)… that’s arduino’s environment specific. (As [boolean instead of bool](https://github.com/arduino/ArduinoCore-avr/blob/2ff00ae7d4e85fa422b7918ee12baf56a1f3006e/cores/arduino/Arduino.h#L126))

Side note: some of your comments apply to AVR / 8 bit MCU, like the min and max values of int, an int would be 4 bytes on a MKR or ESP32 for example.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [September 17, 2022, 11:22pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/12 "2022-09-17T23:22:37Z")

</div>

> [@JohnRob](#):
>
> I have been using uint8\_t

I have been trying… _old habits die hard_. And in "regular" programming, you don't get all those standard names w/o including _stdint_._h_. It looks like Arduino rolls that in behind the scenes. But I def love the unambiguous and portable plain old declarations.

  

```
  PORTB |= bit(BP2) | (PB5); // same as above, recommended

```

should be

```
  PORTB |= bit(BP2) | bit(PB5); // same as above, recommended

```

  
 Maybe all anything to do with interrupts, including 

```
EIMSK |= bit (INT0); // set INT0 bit

```

should be in one bubble. Or... not on there at all! Interrupts could take a very big bubble…

But don't forget

```
digitalPinToInterrupt()

```

which can save some hair  
  
  
conspicuously absent, maybe you don't use

```
strcpy
strcmp
sprintf

```

and so forth.

a7

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 17, 2022, 11:37pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/13 "2022-09-17T23:37:55Z")

</div>

> [@alto777](#):
>
> conspicuously absent, maybe you don't use
> 
> ```arduino
> strcpy
> strcmp
> sprintf
> 
> ```

I would recommend their n or l versions (strncpy, strlcpy) then as well for safety

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 18, 2022, 5:37am UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/14 "2022-09-18T05:37:49Z")

</div>

> [@alto777](#):
>
> I have been trying… _old habits die hard_. And in "regular" programming, you don't get all those standard names w/o including _stdint_._h_. It looks like Arduino rolls that in behind the scenes. But I def love the unambiguous and portable plain old declarations.

I'm struggling here to define (for the consistency of my code). While i like int32\_t, I'm more comfortable with byte than uint8\_t. And I don't know the xxx\_t form of boolean.  
And I like "bool" but it seems Arduino pushes "boolean"

should be (fixed, thanks)

```arduino
  PORTB |= bit(BP2) | bit(PB5); // same as above, recommended

```

Maybe all anything to do with interrupts, including`EIMSK |= bit (INT0); // set INT0 bit`  
[/quote]  
I changed this to a different bubble. It was to show the syntax of setting a register bit not interrupt related.

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 18, 2022, 6:09am UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/15 "2022-09-18T06:09:44Z")

</div>

I've read C "strings" are evil. Not sure they should be here, the implication is that they are recommended.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 18, 2022, 7:42am UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/16 "2022-09-18T07:42:28Z")

</div>

> [@JohnRob](#):
>
> I've read C "strings" are evil

Do you mean this?

> **[The Evils of Arduino Strings](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/)**
>
> Due to Wordpress’s abysmal handling of code blocks this blog post is now hosted at Everyone, when they’re starting out on the Arduino and similar boards, learns to use the String object…

It’s about the String class, not cStrings (and is somewhat biased).

cStrings are not evil but require attention and care to not overflow the underlying buffer. As long as you take the necessary steps to test and catch that, you’ll be fine. That’s why I referred to strlcpy() or strncpy() where the buffer size or amount of bytes is taken into account.

Understanding cStrings is good for mastering arrays in general so a worthwhile time investment. They are also more memory and CPU efficient than using the String class which helps on small arduinos.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [September 18, 2022, 3:18pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/17 "2022-09-18T15:18:34Z")

</div>

> [@JohnRob](#):
>
> I was trying to get away from uintn\_t so it could be byte.

reconsider that. See what Arduino is using in [wiring\_digital.h](https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring_digital.c) and use that type.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [September 18, 2022, 4:47pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/18 "2022-09-18T16:47:23Z")

</div>

Just came across this while trying to catch up to the 21st century:

> `std::byte` cannot have arithmetic performed on it, which might be a deal breaker.

I'm not where I can run and see if this is true. If it is, def a deal breaker. Even if it is somehow not true for Arduino.

L8R:

So, a little incompetent experimentation has left me cold to the idea of using Arduino's unique data type _byte_ instead of either _unsigned_ _char_ or _uint8\_t_.

Just to keep as much the same in different programming circumstances.

The std::byte type does indeed restrict the kinds of things that can be done with it, the Arduino _byte_ behaves and must be typedef somewhere as _uint8\_t_. As _unsinged char_.

Now let's do _bool_ and _boolean_, which have some odd things about them!

As I said, old habits die hard. _true_ and _false_, no thanks. _bool_, not gonna use it. _byte_? whose byte?

_and_, _or_, _not_? I don't think so. Get over it.

a7

---

<div class="post-metadata">

**Author:** ![hammy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hammy/32/238813_2.png) [@hammy](https://forum.arduino.cc/u/hammy)\
**Post date:** [September 18, 2022, 6:01pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/19 "2022-09-18T18:01:26Z")

</div>

Hate to p\*\*s on your chips here - but there are a number of cheat sheets which you might like to look at

[Example](https://duckduckgo.com/?q=arduino+cheat+sheet&t=iphone&iax=images&ia=images&iai=https%3A%2F%2Fsites.google.com%2Fsite%2Fmechatronicsguy%2Farduinocheatsheet%2FArduino%2520cheet%2520sheet%2520v02bsmall.png)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 18, 2022, 8:34pm UTC](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892/20 "2022-09-18T20:34:58Z")

</div>

> [@alto777](#):
>
> std::byte

That’s [the C++ thingy](https://en.cppreference.com/w/cpp/types/byte) meant really for bitwise operations.

> `std::byte` is a distinct type that implements the concept of byte as specified in the C++ language definition.  
> Like char and unsigned char, it can be used to access raw memory occupied by other objects ([object representation](https://en.cppreference.com/w/cpp/language/object)), **but unlike those types, it is not a character type and is not an arithmetic type. A byte is only a collection of bits, and the only operators defined for it are the bitwise ones.**

Byte in arduino land is [just a #define for uint8\_t](https://github.com/arduino/ArduinoCore-avr/blob/2ff00ae7d4e85fa422b7918ee12baf56a1f3006e/cores/arduino/Arduino.h#L127) so an arithmetic type on which you can do maths

[Next page](https://forum.arduino.cc/t/another-cheatsheet-draft/1032892.md?page=2)
