Introduction to Arduino- II

Introduction to Arduino Programming - Part 2

This note expands upon the basic topics of Arduino programming, following the previous introduction. The topics covered include:

Basic Operators

  • Arithmetic Operators: +, -, *, /, % (modular division)

  • Comparison Operators: ==, !=, <, >, <=, >=

  • Boolean and Bitwise Operators: &&, ||, !, &, |, ^

  • Compound Operators: +=, -=, *=, /=, etc.

Control Statements

If-Else Statements
  • Syntax:

  if (condition) {
      // code if condition is true
  } else if (another_condition) {
      // code if another condition is true
  } else {
      // code if none are true
  }
Switch Case
  • Syntax:

  switch(variable) {
      case option1:
          // code for option 1
          break;
      case option2:
          // code for option 2
          break;
      default:
          // code if no case matches
          break;
  }
Conditional Operators
  • It's recommended to avoid using conditional operators in Arduino programming.

Loops

  • For Loop: Basic loop to repeat a block of code a fixed number of times.

  • While Loop: Repeats a block of code while a condition is true.

  • Do While Loop: Executes a block of code at least once, then repeats based on a condition.

  • Nested Loops: Loops within loops.

  • Infinite Loops: Keep running as long as the hardware is powered.

Arrays

  • Definition: A collection of elements of the same data type stored in adjacent memory locations.

  • Declaration: datatype arrayName[size];

  • Example: int arrayName[5];

  • You can also initialize arrays directly with values:

  • int arrayName[] = {0, 1, 2, 3, 4};

  • Multidimensional Arrays: Useful for images, represented as rows and columns for RGB values.

Strings

  • Strings in Arduino can be character arrays with a null terminator.

  • Common Functions:

  • toUpperCase(): Converts all characters to upper case.

  • replace(): Replaces substrings.

  • length(): Returns the length without counting the null character.

Math Library

  • To use mathematical functions, include math.h.

  • Functions include:

  • cos(), sin(), tan(), fabs(), modf(), exp(), log(), and pow().

Random Number Generation

  • Random Seed: Initializes the random number generator. Use randomSeed(value);.

  • Generate Random Numbers: Use random(min, max); to get a number in a specified range.

Interrupts

  • Defined as external signals that prompt the system to halt current processes to respond.

  • Types: Hardware interrupts and software interrupts.

  • Example: Pressing a switch can trigger an LED response.

Example Program: Traffic Control System

  • Create a rudimentary traffic light system using LEDs and Arduino.

  • Components needed: Arduino Board, 3 LEDs (red, green, yellow), resistors, connecting wires, and a breadboard.

  • LED control with digital write commands to simulate traffic lights.

  • Functionality: Each LED lights up in sequence with prints to the serial monitor to show status.

Conclusion

Through this session, we have covered essential operators, control statements, loops, arrays, strings, math operations, random number functions, and interrupts in Arduino programming. We built a simple traffic control system to illustrate these concepts in action.