First Task: A Blinking Light

Arduino Uno connected to breadboard and LED

The first example project to familiarize oneself with Arduino code and circuit building is a simple program to control a blinking LED.

The code to perform this basic task follows:

  1. void setup() {
  2.   // initialize the digital pin as an output.
  3.   // Pin 13 has an LED connected on most Arduino boards:
  4.   pinMode(13, OUTPUT);
  5. }
  6.  
  7. void loop() {
  8.   digitalWrite(13, HIGH); // set the LED on
  9.   delay(1000); // wait for a second
  10.   digitalWrite(13, LOW); // set the LED off
  11.   delay(1000); // wait for a second
  12. }

Of note in this code example is the use of a few necessary functions.

The setup function gives the Arduino board instructions as to which of its electrical hook-ups are being used and what they are being used for. This function runs at the start of program and is not called or run again.

The loop function houses the main code of an Arduino program. It will run on continuous loop until the board is powered down or reprogrammed. Globally scoped variables can be used to trigger events on different loop iterations by defining these variables at the top of the program and outside of any of the main functions.

As might be expected, alternate functions can be called within the loop. The program above could be written as:

  1. void setup() {
  2.   // initialize the digital pin as an output.
  3.   // Pin 13 has an LED connected on most Arduino boards:
  4.   pinMode(13, OUTPUT);    
  5. }
  6.  
  7. void loop() {
  8.   blinkLight();
  9. }
  10.  
  11. void blinkLight() {
  12.   digitalWrite(13, HIGH);   // set the LED on
  13.   delay(1000);              // wait for a second
  14.   digitalWrite(13, LOW);    // set the LED off
  15.   delay(1000);              // wait for a second
  16. }

Also of note are the digitalWrite and pinMode functions. The former takes parameters for the pin (referring to an electrical connection on the physical Arduino board) and the voltage (in this example HIGH enables 5V of power to flow while LOW stops power). The pinMode function instructs the controller to use the designated pin (13 in this example) as an output or input. When input is selected the device will read the voltage value being sent to the pin to determine the states of external sensors.

Feeling the need to extend this basic project I decided make the time of the blinking variable depending on human input. As the controller was being powered by a computer's USB anyway it made the most sense to use the serial port functions of the Arduino to transmit the data. The goal of the experiment was to learn how to process string and character input in the Arduino language.

  1. int ledPin = 13;
  2. int defaultTimeDelay = 1000;
  3. int timeDelay = 1000;
  4. float timeInSeconds = 1;
  5. int incomingByte = 0;
  6.  
  7. void setup() {                
  8.   // initialize the digital pin as an output.
  9.   pinMode(ledPin, OUTPUT);
  10.   pinMode(8, OUTPUT);
  11.   //Start Serial connection
  12.   Serial.begin(9600);
  13.   Serial.println("Give me a number (in seconds) and I will make the light blink for that time...");
  14. }
  15.  
  16. void loop() {
  17.   if (Serial.available() > 0) {
  18.     int ca = Serial.available();
  19.     char inputCharArray[ca + 1];
  20.     for (int i = 0; i < ca; i++) {
  21.       // read the incoming byte:
  22.       int incomingByte = Serial.read();
  23.       char readableByte = char(incomingByte);
  24.       inputCharArray[i] = readableByte;
  25.     }
  26.  
  27.     //The final number entered by the user is:
  28.     timeInSeconds = atof(inputCharArray);
  29.     if (timeInSeconds > 0) {
  30.       Serial.print("I rounded your input to 2 decimal places: ");
  31.       Serial.println(timeInSeconds);
  32.       timeDelay = int(timeInSeconds * 1000);
  33.     }
  34.     else {
  35.       timeDelay = defaultTimeDelay;
  36.       Serial.println("Unable to convert your input to a number. Using defaults.");
  37.     }
  38.   }
  39.  
  40.   digitalWrite(ledPin, HIGH);  // set the LED on
  41.   digitalWrite(8, LOW);
  42.   delay(timeDelay);              // wait for a second
  43.   digitalWrite(ledPin, LOW);    // set the LED off
  44.   digitalWrite(8, HIGH);
  45.   delay(timeDelay);
  46. }

The program initiates a serial connection and sets a default blink time. Then it reads input bytes from the serial connection if any exist. It then converts the text to a floating point number and resets the blink duration to that number.

A few notable things can be seen in this program. First, the incoming bytes from the serial connection must be read from the serial buffer one at a time. For this reason we must store them in a character array. Second, we set the char array inputCharArray to be one value longer than the number of bytes in the serial buffer. The reason for this is that the last character in the serial buffer is a null character and was causing the values stored in the inputCharArray to be truncated. Finally, the use of the atof() function which is necessary to convert the character array into a numeric type. The Arduino language enforces strict typecasting so there is no simple way to do a string to float or string to integer conversion.