C for Arduino

A sequence of simple sketches for illustrating the C programming language on the Arduino.

  1. The most basic sketch:

    /* Pin 13 has an LED connected on most Arduino boards.
       give it a name:  */
    
    int LEDPIN = 13;
    
    // the setup routine runs once when you press reset:
    void setup() {  
        pinMode(LEDPIN, OUTPUT);
    }
    
    // initialize the digital pin as an output.
    void loop() {  
        digitalWrite(LEDPIN, HIGH);
    }
    
  2. Testing with if:

    int LEDPIN = 13;
    
    void setup() {  
        pinMode(LEDPIN, OUTPUT);
    }
    
    int b = 1;
    
    void loop() {  
        if (b < 10) {
            digitalWrite(LEDPIN, HIGH);
        }
    
        else {
            digitalWrite(LEDPIN, LOW);
        }
    }
    
  3. Saving state with a variable:

    void setup() {  
        pinMode(LEDPIN, OUTPUT);
    }
    
    int b = 1;
    
    void loop() {  
        if (b == 1) {
            digitalWrite(LEDPIN, HIGH);
            b = 0;
        }
    
        else {
            digitalWrite(LEDPIN, LOW);
            b = 1;
        }
    
        delay(500);
    }
    
  4. Looping with while:

    int LEDPIN = 13;
    
    void setup() {
        pinMode(LEDPIN, OUTPUT);
    }
    
    void loop() {
        int foo = 0;
    
        while (foo < 3)
        {
            digitalWrite(LEDPIN, HIGH);
            delay(500);
    
            digitalWrite(LEDPIN, LOW);
            delay(500);
    
            foo = foo + 1;
        }
    
        delay(2000);
    }
    
  5. Looping with for:

    int LEDPIN = 13;
    
    void setup() {
        pinMode(LEDPIN, OUTPUT);
    }
    
    void loop() {
        int foo;
    
        for (foo = 0; foo < 3; foo = foo + 1)
        {
            digitalWrite(LEDPIN, HIGH);
            delay(500);
    
            digitalWrite(LEDPIN, LOW);
            delay(500);
        }
    
        delay(2000);
    }
    
  6. Defining a function:

    int LEDPIN = 13;
    
    void setup() {
        pinMode(LEDPIN, OUTPUT);
    }
    
    void blink_led(int number_blinks) {
        int i;
    
        for (i = 0; i < number_blinks; i++) {
            digitalWrite(LEDPIN, HIGH);
            delay(500);
    
            digitalWrite(LEDPIN, LOW);
            delay(500);
        }
    }
    
    void loop() {
        blink_led(3);
        delay(2000);
    }
    
  7. Using C++ objects:

    void setup()
    {
        Serial.begin(19200);
        Serial.println("we are in setup");
    }
    
    void loop()
    {
        Serial.println("we are in loop");
        delay(2000);
    }
    
  8. Using datatypes like float, and putting it all together:

    /*
      simplesketch #1e - using datatypes (like float)
    
      This series of sketches demonstrates some aspects of C/C++ programming on Arduino
    
      This sketch reads a value from an analog pin, and converts it to a floating point
      number between 0.0 and 1.0.
      This demonstrates the use of "int" and "float" datatypes.
    
      Hardware setup:
    
      An Arduino connected to your computer.
      A 10K potentiometer wired so that the middle pin goes to analog pin 4,
      the top pin goes to +5 volts and the bottom pin to GND.
    
      Revision history:
      - apr 24, 2014 - rolf
        created.
    
      Author: Rolf Widenfelt (c) 2014 - Some Rights Reserved.
    
      Note: This source code is licensed under a Creative Commons License, CC-by-sa.
         (attribution, share-alike)
             See http://creativecommons.org/licenses/by-sa/4.0/ for details.
    */
    
    // this defines the analog pin to use
    const int SENSORPIN = A4;
    
    // the setup routine runs once
    void setup() {
    
      // set the baud rate
      Serial.begin(19200);
    }
    
    // the loop routine runs over and over
    void loop() {
      int sensor;      // the sensor value, as an integer
      float sum, avg;  // the sum and average, as floating point values
    
      // we read the analog pin 5 times, convert to "float", and take the average
    
      sum = 0.0;
      for (int i = 0; i < 5; i++) {
        sensor = analogRead(SENSORPIN);  // returns a value from 0 to 1023
        sum = sum + sensor;       // Collect new value from sensor in sum.
    
        Serial.print(sensor);
        Serial.print(" ");
    
        delay(10);
      }
      Serial.println("");
    
      avg = sum/5.0;           // calculate the average
      Serial.println(avg);
    
      delay(2000);
    }