Coding Arduino

Conditional

/* Global variable definition */
int A = 5 ;
int B = 9 ;
int c = 15;

Void setup () {


}

Void loop () {
   /* check the boolean condition /
   if (A > B) /* if condition is true then execute the following statement*/ {
      A++;
}
/* check the boolean condition /
else if ((A == B )||( B < c) ) / if condition is true then 
      execute the following statement*/ {
C = B* A;
}else
c++;
}
switch (phase) {
   case 0: Lo(); break;
   case 1: Mid(); break;
   case 2: Hi(); break;
   default: Message("Invalid state!");
}
/* Find max(a, b): */
max = ( a > b ) ? a : b;
/* Convert small letter to capital: */
/* (no parentheses are actually necessary) */
c = ( c >= 'a' && c <= 'z' ) ? ( c - 32 ) : c;
while(expression) {
   Block of statements;
}
while(expression) {
   Block of statements;
}
do { 
   Block of statements; 
} 
while (expression);
for(counter = 2;counter <= 9;counter++) {
   //statements block will executed 10 times
}

Function

for(counter = 0;counter <= 9;counter++) {
   //statements block will executed 10 times
   for(i = 0;i <= 99;i++) {
      //statements block will executed 100 times
   }
}
int sum_func (int x, int y) // function declaration {
   int z = 0;
   z = x+y ;
   return z; // return the value
}

void setup () {
   Statements // group of statements
}

Void loop () {
   int result = 0 ;
   result = Sum_func (5,6) ; // function call
}

String

void setup() {
   char my_str[6]; // an array big enough for a 5 character string
   Serial.begin(9600);
   my_str[0] = 'H'; // the string consists of 5 characters
   my_str[1] = 'e';
   my_str[2] = 'l';
   my_str[3] = 'l';
   my_str[4] = 'o';
   my_str[5] = 0; // 6th array element is a null terminator
   Serial.println(my_str);
}
void loop() {

}
void setup() {
   char my_str[] = "Hello";
   Serial.begin(9600);
   Serial.println(my_str);
}

void loop() {

}
void setup() {
   char like[] = "I like coffee and cake"; // create a string
   Serial.begin(9600);
   // (1) print the string
   Serial.println(like);
   // (2) delete part of the string
   like[13] = 0;
   Serial.println(like);
   // (3) substitute a word into the string
   like[13] = ' '; // replace the null terminator with a space
   like[18] = 't'; // insert the new word
   like[19] = 'e';
   like[20] = 'a';
   like[21] = 0; // terminate the string
   Serial.println(like);
}

void loop() {

}
void setup() {
   char str[] = "This is my string"; // create a string
   char out_str[40]; // output from string functions placed here
   int num; // general purpose integer
   Serial.begin(9600);

   // (1) print the string
   Serial.println(str);

   // (2) get the length of the string (excludes null terminator)
   num = strlen(str);
   Serial.print("String length is: ");
   Serial.println(num);

   // (3) get the length of the array (includes null terminator)
   num = sizeof(str); // sizeof() is not a C string function
   Serial.print("Size of the array: ");
   Serial.println(num);

   // (4) copy a string
   strcpy(out_str, str);
   Serial.println(out_str);

   // (5) add a string to the end of a string (append)
   strcat(out_str, " sketch.");
   Serial.println(out_str);
   num = strlen(out_str);
   Serial.print("String length is: ");
   Serial.println(num);
   num = sizeof(out_str);
   Serial.print("Size of the array out_str[]: ");
   Serial.println(num);
}

void loop() {

}

Time

    Delay
    /* Flashing LED
       * ------------
       * Turns on and off a light emitting diode(LED) connected to a digital
       * pin, in intervals of 2 seconds. *
    */
    int ledPin = 13; // LED connected to digital pin 13
    
    
    void setup() {
    pinMode(ledPin, OUTPUT); // sets the digital pin as output
    }
    
    
    void loop() {
    digitalWrite(ledPin, HIGH); // sets the LED on
    delay(1000); // waits for a second
    digitalWrite(ledPin, LOW); // sets the LED off
    delay(1000); // waits for a second
    }
    

    Delay Microseconds

    /* Flashing LED
       * ------------
       * Turns on and off a light emitting diode(LED) connected to a digital
       * pin, in intervals of 1 seconds. *
    */
    
    int ledPin = 13; // LED connected to digital pin 13
    
    void setup() {
       pinMode(ledPin, OUTPUT); // sets the digital pin as output
    }
    
    void loop() {
       digitalWrite(ledPin, HIGH); // sets the LED on
       delayMicroseconds(1000); // waits for a second
       digitalWrite(ledPin, LOW); // sets the LED off
       delayMicroseconds(1000); // waits for a second
    }
    

    millis

    unsigned long time; void setup() { 
       Serial.begin(9600); 
    } 
    
    void loop() { 
       Serial.print("Time:"); time = millis();
       //prints time since program started
       Serial.println(time); 
       // wait a second so as not to send massive amounts of data
       delay(1000); 
    }
    

    micros

    unsigned long time; void setup() { 
       Serial.begin(9600); 
    } 
    
    void loop() { 
       Serial.print("Time:");
       time = micros(); //prints time since program started
       Serial.println(time); // wait a second so as not to send massive amounts of data
       delay(1000); 
    }
    

Array

int n[ 10 ] ; // n is an array of 10 integers
void setup () {


}


void loop () {
for ( int i = 0; i < 10; ++i ) // initialize elements of array n to 0 {
n[ i ] = 0; // set element at location i to 0
Serial.print (i) ;
Serial.print (‘</span>r’) ;
}
for ( int j = 0; j < 10; ++j ) // output each array element's value {
Serial.print (n[j]) ;
Serial.print (‘</span>r’) ;
}
}

// n is an array of 10 integers
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 } ;

void setup () {

}

void loop () {
   for ( int i = 0; i < 10; ++i ) {
      Serial.print (i) ;
      Serial.print (‘\r’) ;
   }
   for ( int j = 0; j < 10; ++j ) // output each array element's value {
      Serial.print (n[j]) ;
      Serial.print (‘\r’) ;
   } 
}