Steven Harrap

Arduino board: starting out

During my university exchange course to Sweden I purchased a device called the Arduino Board. The Arduino featured in a large part of the course syllabus. The board connects to a computer by the USB port and an IDE allows one to upload C based programs. The board then executes code and controls the attached bells and whistles. The Arduino can communicate with the computer and take commands from it to extend functionality. Very Neat. This was my first decent example project.

This project is an example of using a dimmer (potentiometer) to turn a row of lights on and off. If the dimmer is at a certain level and a push button is pushed the lights will flash. The board will also accept a serial input of the integer "1" to make the lights flash at any time.

The Arduino board has control chip that executes C code. This chip controls all the output and input and makes programing very easy. The program code looks something like this:

  • Initiate program variables
  • Start a never ending loop:
    • check the value of the dimmer
    • turn on the number of lights that equals the dimmers value
    • if the dimmer is high enough and the button is pressed then flash lights
    • if 1 is recieved from the computer then flash lights

The Arduino C Code

      int pinLow = 2;
      int pinHigh = 9;
      int pins = pinHigh - pinLow + 1;
      int maxLevel = 1023;
      int range = maxLevel / pins;
      void setup() {
      //begin the serial connection and make the pin 13 (analog) the
      //input from the pot resistor. This value will be between 0 and 1023
      Serial.begin(9600);
      pinMode(13, INPUT);
      //set the pins 2 through 9 for output (the LEDs are attached to those)
      for (int i=pinLow; i<=pinHigh; i++){
      pinMode(i, OUTPUT);
      }
      }
      void loop() {
      //if a key was pressed
      //the explode (flash lights)
      //
      //else if the pot resistor at its least resistance
      //(wound right down) so the level appears high then pressing the button
      //on the board will make the lights flash.
      //
      //else if sets the number of lights that should appear on as a ratio
      //of the level of the pot resistor (somewhere between 0 and 1023
      //
      //For people who just want to have lights follow the level of the pot
      //then just the for-loop in the last 'else' would work,
      int level = analogRead(1);
      int explode = digitalRead(13);
      int pinLevel = level / range;
      if (Serial.available()) {
      int key = Serial.read();
      Serial.println(key, DEC);
      if (key == 49) {
      exploding();
      }
      } else if ((explode) && (level == maxLevel)) {
      exploding();
      } else {
      for (int i=0; i<=pins; i++){
      if (i < pinLevel) {
      digitalWrite(pinLow + i, HIGH);
      } else {
      digitalWrite(pinLow + i, LOW);
      }
      }
      }
      }
      void exploding() {
      int onOff = LOW;
      for (int e=0; e<50; e++) {
      onOff = !onOff;
      for (int i=pinLow; i<=pinHigh; i++) {
      digitalWrite(i, onOff);
      }
      delay(50);
      }
      }