👨‍💻 dev arduino cheat sheet codes

development

fukurou

the supreme coder
ADMIN
variables
C++:
int led = 13; // implicit var declaration
bool ledState1 = false;
const float pi = 3.14;
#define c1 4 // const declaration
String s = "hello";

N-dimentional arrays
C++:
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
int b[ 2 ][ 2 ];
b[0][0] = 10;// item in array

loops
C++:
for(int i=0; i<7; i++)  {}
while (someVariable < 200){}
do {}
while (someVariable ?? value);

array
C++:
// initialize an array:
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
char message[6] = "hello";
myInts[0] = 10;// itemm in array
sizeof(arr); // returns arrray or list size

switch
C++:
switch (var) {
  case 1:
    //do something when var equals 1
    break;
  case 2,3:
    // 2 or 3
    break;
  default:
    // if nothing else matches, do the (optional) default
    break;
}

strings
C++:
String stringThree = "I want ";
stringThree+=3; // string concate
 
Last edited:

fukurou

the supreme coder
ADMIN
conditionals
C++:
  if (condition) {}
  else if (condition2) {}
  else {}
/* Find max(a, b): */
max = ( a > b ) ? a : b;

functions
C++:
int triple(int a){
  return a *3;
}
//int arr[array_size]
// to pass array param
int times(int a, int b){
  return a * b;
}

random

C++:
int randNumber;
void setup() {
  Serial.begin(9600);
  // if analog input pin 0 is unconnected
  randomSeed(analogRead(0));
}

void loop() {
  Serial.println(random(300));// 0->300
  Serial.println(random(10, 20));//10->19
  delay(50);
}

exit
C++:
cli();
 

fukurou

the supreme coder
ADMIN
classes:

C++:
class Led{
    private:
          byte pin;
      public:
  Led(){this->pin = 13;}
  Led(byte pin){
      this->pin = pin;
  }
  void init(){
      pinMode(pin, OUTPUT);
  }
  void on(){
      digitalWrite(pin, HIGH);
  }
  void off(){
      digitalWrite(pin, LOW);
  }
};
Led led1; // default c'tor
Led led1(13); // overload c'tor
led1.on(); // invoke method
 
Last edited:
Top