Home‎ > ‎Unit 06‎ > ‎

Arduino Blink

Ok, so now we finally get to Physical Computing. We'll be controlling things in the real world with processing sketches. Copy the code below and paste into Processing. Run it and see what happens.
import processing.serial.*; //Import the serial library into your sketch

import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch

Arduino arduino;      //Create an instance of Arduino named arduino (can be any name) 

int ledPin = 13;  //Create a varibale ledPin and assigns a value of 13 to it.

void setup() {
  size(100, 100);    
  arduino = new Arduino(this, Arduino.list()[0], 57600); //defines arduino our board and sets the communication rate
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  }

void draw()
{
  arduino.digitalWrite(ledPin, Arduino.HIGH); // Tells ledPin to turn on, i.e. supply 5 volts
  delay(1000);                                // Wait 1000 milliseconds (1 second)
  arduino.digitalWrite(ledPin, Arduino.LOW);  // Tells ledPin to turn off (become a ground)
  delay(1000);
}

You should see the LED flashing on your Arduino board. This is basically the Arduino Blink sketch but it is being run by Processing rather than on the Arduino board itself.

There are several new pieces of code. Let's break it down.


The "import" lines add in the libraries we'll need. The first allows Processing to send and receive serial data it came with Processing when you downloaded it. The second one is the one you added.

The line Arduino arduino; creates an instance of Arduino that we name arduino. We could name it anything we want just like we do with variable names. This is kind of like a variable declaration. When we say int x; we're telling processing to create an integer variable with the name of x.


Two new lines in our void setup(). The first new one sets up the initial conditions for our Arduino. Don't worry too much about the structure and all the bits. We may explain them more fully latter in the course. The only part you need to know about right now is:

Arduino.list()[0]

The only part you really need to worry about here is the [0]. You must make sure the number is your Arduino's port number. Look back in Unit 5 for how to determine this.

arduino.pinMode(ledPin, Arduino.OUTPUT);

ledPin is a variable we declared. It has a value of 13. This line tells the Arduino to use pin 13 as an output. This means we will only send data out through it and will not try to receive data from it. Pins 0-13 on the Arduino can function as either digital outputs or inputs. We'll deal with inputs in the next unit.


Here we're just telling the LED to turn on and off in 1 second intervals. Digital outputs can be HIGH or LOW. HIGH means send out 5 volts from the pin. LOW means act like the negative side of a battery or ground.
Comments