<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7422250244359211400</id><updated>2011-08-01T19:23:36.584-04:00</updated><title type='text'>Physical Computing Blog</title><subtitle type='html'>Exploring Computational Environments</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kinleyphycom.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://kinleyphycom.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Kinley Tshering</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://4.bp.blogspot.com/_v34GvbHhf3A/SfPZY0kDICI/AAAAAAAAAKg/_TkGTNHg9us/S220/ava.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7422250244359211400.post-2630487224025440130</id><published>2009-05-19T12:18:00.006-04:00</published><updated>2009-05-20T22:28:51.973-04:00</updated><title type='text'>Java and Arduino Game</title><content type='html'>For this project, we are suppose to make arduino talk to another machine (another arduino, computer, etc) through the use of sensors. I made a simple game where the player will have to dodge the flying red monsters and stay safe.&lt;br /&gt;&lt;br /&gt;In order to achieve the criteria specified by our professor, I made the arduino to get physical value through an IR sensor and pass it to the computer by using serial communication. I made a costume event handler where an event will be thrown every time the value of the variable which holds the IR sensor's value changes. So basically when the user interacts with the IR sensor, the arduino gets the value from the IR sensor and passes the appropriate value to the SensorGuy object. Once the value is received by SensorGuy, it converts the value to char from byte and then calls the appropriate method in the SensorEvent object. Once the value changes in the SensorEvent object, it creates a SensorObject object and passes the object to all the classes who have registered to be notified.&lt;br /&gt;&lt;br /&gt;In this game, the main GUI is the DodgeMaster object. This object registers itself to the SensorEvent when it is being created and implements the SensorListener interface which has the necessary methods to be created. When the SensorEvent notifies the change in value of the sensor, the main GUI receives the value and this value in turn determines the behavior of the player. The overall working of the project is shown in the UML diagram below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_v34GvbHhf3A/ShSz0XuLYTI/AAAAAAAAAMQ/vT9PK-X0fpw/s1600-h/uml.GIF"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 461px; height: 284px;" src="http://3.bp.blogspot.com/_v34GvbHhf3A/ShSz0XuLYTI/AAAAAAAAAMQ/vT9PK-X0fpw/s320/uml.GIF" alt="" id="BLOGGER_PHOTO_ID_5338089170643149106" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The codes necessary for the working of this project is given below:&lt;br /&gt;&lt;br /&gt;1. SensorListener.java, which is an event interface.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/**&lt;br /&gt;* This class is the interface which has to be implemented by any classes&lt;br /&gt;* who want to get events from the SensorEvent class.&lt;br /&gt;* @author kinley tshering&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public interface SensorListener &lt;br /&gt;{&lt;br /&gt;    public void valueRecieved( SensorEvent event );&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;2. SensorEvent.java, the event object.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/**&lt;br /&gt;* This class is the event objectwhich has to be created every time&lt;br /&gt;* an event is fired by the SensorEvent object. &lt;br /&gt;* @author kinley tshering&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import java.util.EventObject;&lt;br /&gt;&lt;br /&gt;public class SensorEvent extends EventObject {&lt;br /&gt;    private char sensor;&lt;br /&gt;&lt;br /&gt;    public SensorEvent( Object source, char _sensor ) {&lt;br /&gt;        super( source );&lt;br /&gt;        sensor = _sensor;&lt;br /&gt;    }&lt;br /&gt;    public int getSensor() {&lt;br /&gt;        return sensor;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;3. SensorObject.java, the event source object.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/**&lt;br /&gt;* This class is the event sourcewhich which will generate events every time&lt;br /&gt;* the value of the sensor changes. &lt;br /&gt;* @author kinley tshering&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;public class SensorObject {&lt;br /&gt;&lt;br /&gt;    private static char sensorValue = 0;&lt;br /&gt;    private List _listeners = new ArrayList();&lt;br /&gt;    public synchronized void receiveValue(char _senValue) {&lt;br /&gt; //get sensor value and fire events&lt;br /&gt; sensorValue = _senValue;&lt;br /&gt; fireEvent();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public synchronized void addSensorListener( SensorListener sl ) {&lt;br /&gt;        _listeners.add( sl );&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public synchronized void removeSensorListener( SensorListener sl ) {&lt;br /&gt;        _listeners.remove( sl );&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private synchronized void fireEvent() {&lt;br /&gt;        SensorEvent sen = new SensorEvent( this, sensorValue );&lt;br /&gt;        Iterator listeners = _listeners.iterator();&lt;br /&gt;        while( listeners.hasNext() ) {&lt;br /&gt;            ( (SensorListener) listeners.next() ).valueRecieved( sen );&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;4. SensorGuy.java, this class bridges the arduino and java together by using the serial communication to receive data.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/**&lt;br /&gt;* This class gets the sensor value from arduino and notifies the event source.&lt;br /&gt;* this code is adopted from the java sample provided by arduino on arduino site.&lt;br /&gt;* @author kinley tshering&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import gnu.io.CommPortIdentifier;&lt;br /&gt;import gnu.io.SerialPort;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import java.io.OutputStream;&lt;br /&gt;import processing.app.Preferences;&lt;br /&gt;&lt;br /&gt;public class SensorGuy extends Thread{&lt;br /&gt;&lt;br /&gt;    static InputStream input;&lt;br /&gt;    static OutputStream output;&lt;br /&gt;    SensorObject sen;&lt;br /&gt;&lt;br /&gt;    public SensorGuy(SensorObject _sen)  {&lt;br /&gt;  sen = _sen;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void run() {&lt;br /&gt;  try {&lt;br /&gt;  Preferences.init();&lt;br /&gt;&lt;br /&gt;  CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(&lt;br /&gt;    Preferences.get("serial.port"));&lt;br /&gt;&lt;br /&gt;  SerialPort port = (SerialPort)portId.open("serial madness", 4000);&lt;br /&gt;        input = port.getInputStream();&lt;br /&gt;        output = port.getOutputStream();&lt;br /&gt;        port.setSerialPortParams(&lt;br /&gt;         Preferences.getInteger("serial.debug_rate"),&lt;br /&gt;         SerialPort.DATABITS_8,&lt;br /&gt;         SerialPort.STOPBITS_1,&lt;br /&gt;         SerialPort.PARITY_NONE);&lt;br /&gt;  while(true){&lt;br /&gt;   while (input.available()&gt;0)  {&lt;br /&gt;    //System.out.print((char)(input.read()));&lt;br /&gt;    char value = (char)input.read();&lt;br /&gt;    sen.receiveValue(value);&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; catch(Exception exc)  {&lt;br /&gt;  System.err.println("ERROR: we're sorry, cannot execute further");&lt;br /&gt; }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;5. DodgeMaster.java, the main GUI of the game where the results of the user interaction is shown on the screen using Frames and 2D graphics. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;import java.awt.event.*;&lt;br /&gt;import java.util.*;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * DodgeMaster.java&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @author Kinley Tshering&lt;br /&gt; * @adopted from a code sample from programmers Heaven...&lt;br /&gt; */&lt;br /&gt;public class DodgeMaster extends JFrame implements ActionListener, SensorListener {&lt;br /&gt;&lt;br /&gt;    /** Creates new form DodgeMaster */&lt;br /&gt;    public DodgeMaster() {&lt;br /&gt;        initComponents();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    private void initComponents() {&lt;br /&gt;  //start the Timer event&lt;br /&gt;        timer1 = new javax.swing.Timer(timer, this);&lt;br /&gt;        timer1.start();&lt;br /&gt;&lt;br /&gt;  senObj = new SensorObject();&lt;br /&gt;  senObj.addSensorListener(this);&lt;br /&gt;&lt;br /&gt;  //start the Thread&lt;br /&gt;  test = new SensorGuy(senObj);&lt;br /&gt;  test.start();&lt;br /&gt;&lt;br /&gt;        this.setIconImage(carImage);&lt;br /&gt;        jPanel1 = new javax.swing.JPanel();&lt;br /&gt;&lt;br /&gt;        setResizable(false);&lt;br /&gt;        addKeyListener(new java.awt.event.KeyAdapter() {&lt;br /&gt;&lt;br /&gt;            public void keyPressed(java.awt.event.KeyEvent evt) {&lt;br /&gt;                formKeyPressed(evt);&lt;br /&gt;            }&lt;br /&gt;        });&lt;br /&gt;&lt;br /&gt;        addWindowListener(new java.awt.event.WindowAdapter() {&lt;br /&gt;&lt;br /&gt;            public void windowClosing(java.awt.event.WindowEvent evt) {&lt;br /&gt;                exitForm(evt);&lt;br /&gt;            }&lt;br /&gt;        });&lt;br /&gt;&lt;br /&gt;        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);&lt;br /&gt;&lt;br /&gt;        pack();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt; //implementation of ActionListener&lt;br /&gt;    public void actionPerformed(ActionEvent e) {&lt;br /&gt;        //get graphics&lt;br /&gt;        g = jPanel1.getGraphics();&lt;br /&gt;        //draw road&lt;br /&gt;        drawBackground(backX, backY, backWidth, backHeight);&lt;br /&gt;        //draw enemy car&lt;br /&gt;        for (int i = 0; i &lt; enemyY.length; i++) {&lt;br /&gt;            //move cars vertically&lt;br /&gt;            enemyY[i] += enemySpeed[i] - playerSpeed;&lt;br /&gt;&lt;br /&gt;            if(enemyY[i] &gt; formDimension[2] + enemyplayerHeight)  {&lt;br /&gt;    enemyY[i] = - enemyplayerHeight;&lt;br /&gt;    enemyX[i] = ran.nextInt(formDimension[3]);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;            drawEnemy(enemyX[i], enemyY[i], enemyplayerWidth, enemyplayerHeight, i);&lt;br /&gt;        }&lt;br /&gt;        //draw the player&lt;br /&gt;        drawPlayer(playerX, playerY, playerWidth, playerHeight);&lt;br /&gt;        //watch for collisions&lt;br /&gt;        crashes();&lt;br /&gt;        //draw teh panel and other infnormation&lt;br /&gt;        drawPanel();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt; //implementation of SensorListener&lt;br /&gt; public void valueRecieved( SensorEvent event )  {&lt;br /&gt;  char key = (char) event.getSensor();&lt;br /&gt;  if (key == '1' &amp;&amp; playerX &gt; formDimension[0]) {&lt;br /&gt;      playerX -= 2;&lt;br /&gt;  }&lt;br /&gt;  else if (key == '2' &amp;&amp; playerX &lt; formDimension[2] + formDimension[0] - playerWidth) {&lt;br /&gt;      playerX += 2;&lt;br /&gt;        }&lt;br /&gt;        else {}&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; //implementation of keyBoardListener (Adapter).&lt;br /&gt;    private void formKeyPressed(java.awt.event.KeyEvent evt) {&lt;br /&gt;        // 32 - space bar&lt;br /&gt;        keyCode = evt.getKeyCode();&lt;br /&gt;&lt;br /&gt;        if (keyCode == 32) {&lt;br /&gt;            newGame();&lt;br /&gt;            timer1.start();&lt;br /&gt;        }&lt;br /&gt;    else {}&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /** Exit the Application */&lt;br /&gt;    private void exitForm(java.awt.event.WindowEvent evt) {&lt;br /&gt;        System.exit(0);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static void main(String args[]) {&lt;br /&gt;        new CarRace().setVisible(true);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public Dimension getPreferredSize() {&lt;br /&gt;        return frameSize;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void drawPlayer(int a, int b, int c, int d) {&lt;br /&gt;        backY += playerSpeed;&lt;br /&gt;        if (backY &gt; formDimension[3]) {&lt;br /&gt;            backY = 10;&lt;br /&gt;        }&lt;br /&gt;        //draw sikilmish image on the car blyad!!!&lt;br /&gt;        g.drawImage(carImage, a, b, c, d, null);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void drawBackground(int a, int b, int c, int d) {&lt;br /&gt;        //draw the road&lt;br /&gt;        g.setColor(Color.gray);&lt;br /&gt;        g.fillRect(formDimension[0], formDimension[1], formDimension[2], formDimension[3]);&lt;br /&gt;        //draw the stripes on the road&lt;br /&gt;        g.setColor(roadColor);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void drawEnemy(int a, int b, int c, int d, int e) {&lt;br /&gt;        g.drawImage(enemyImage, a, b, c, d, null);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void crashes() {&lt;br /&gt;        //check collision between enemy car and the player&lt;br /&gt;        for (int i = 0; i &lt; enemyY.length; i++) {&lt;br /&gt;            if (enemyX[i] &gt; playerX - (enemyplayerWidth - 10) &amp;&amp; enemyX[i] &lt; (playerX + &lt;br /&gt;                playerWidth - 10) &amp;&amp; enemyY[i] &gt; playerY - (enemyplayerHeight - 10) &amp;&amp;&lt;br /&gt;                enemyY[i] &lt; playerY + (playerWidth + 10)) {&lt;br /&gt;                timer1.stop() ;&lt;br /&gt;                g.setColor(panelColor);&lt;br /&gt;                g.drawString("Sorry! You had a collision. Try Again!!", panelX, panelY);&lt;br /&gt;                timeLeft = 2000;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        //check the collision among the enemy cars&lt;br /&gt;        for (int i = 0; i &lt; enemyY.length; i++) {&lt;br /&gt;            for (int z = 0; z &lt;= 4; z++) {&lt;br /&gt;                if (z != i &amp;&amp; enemyX[z] &gt; enemyX[i] - enemyplayerWidth &amp;&amp; enemyX[z] &lt;&lt;br /&gt;                   enemyX[i] + enemyplayerWidth &amp;&amp; enemyY[z] &gt; enemyY[i] - enemyplayerHeight &amp;&amp; &lt;br /&gt;                   enemyY[z] &lt; enemyY[i] + enemyplayerWidth) {&lt;br /&gt;                    enemyX[i] = enemyX[z] + enemyplayerWidth;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void drawPanel() {&lt;br /&gt;        g.setColor(panelColor);&lt;br /&gt;        if (timeLeft &gt; 0) {&lt;br /&gt;            timeLeft--;&lt;br /&gt;        } else {&lt;br /&gt;   //next level, increase the timer speed (speed of attacking cars)&lt;br /&gt;            g.drawString("Well Done!! Good Luck With the next level.", panelX, panelY + 20);&lt;br /&gt;            level++;&lt;br /&gt;            timer-=10;&lt;br /&gt;            timeLeft = 1000;&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;        g.drawString("Level " + level, 150, 150);&lt;br /&gt;        g.drawString("Time Left: " + timeLeft, 150, 130);&lt;br /&gt;    }&lt;br /&gt;    //craete a new game&lt;br /&gt;    private void newGame() {&lt;br /&gt;        //drivers car parameters&lt;br /&gt;        playerX = formDimension[2] / 2;&lt;br /&gt;        playerY = formDimension[3] - playerHeight - 50;&lt;br /&gt;        playerSpeed = 0;&lt;br /&gt;        //enemy car parameters&lt;br /&gt;        timeLeft = 2000;&lt;br /&gt;        //road parameters&lt;br /&gt;        backX = 300;&lt;br /&gt;        backY = -backHeight;&lt;br /&gt;        keyCode = 0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private javax.swing.JPanel jPanel1;&lt;br /&gt;    private javax.swing.Timer timer1;&lt;br /&gt;    private final int[] formDimension = { 0, 0, 500, 710};&lt;br /&gt;    private Graphics g = null;&lt;br /&gt;&lt;br /&gt;    //drivers car parameters&lt;br /&gt;    private final int playerWidth = 40;&lt;br /&gt;    private final int playerHeight = 80;&lt;br /&gt;    private int playerX = Math.round(formDimension[2] / 2);&lt;br /&gt;    private int playerY = formDimension[3] - playerHeight - 50;&lt;br /&gt;    private int playerSpeed = 0;&lt;br /&gt;    private final int maxplayerSpeed = 100;&lt;br /&gt;    private final int minplayerSpeed = 5;&lt;br /&gt;    private int timeLeft = 1000;&lt;br /&gt;    private final Image carImage = Toolkit.getDefaultToolkit().getImage("player.gif");&lt;br /&gt;&lt;br /&gt;    //enemy car parameters&lt;br /&gt;    private int enemyplayerWidth = 40;&lt;br /&gt;    private int enemyplayerHeight = 80;&lt;br /&gt;    private final int defaultEnemyplayerWidth = 40;&lt;br /&gt;    private final int defaultEnemyplayerHeight = 80;&lt;br /&gt;    private int[] defaultEnemyX = { 400, 200, 250, 300, 350, 150};&lt;br /&gt;    private int[] enemyX = { 400, 200, 250, 300, 350, 150};&lt;br /&gt;    private final int defaultEnemyplayerY = formDimension[1] + enemyplayerHeight - 50;&lt;br /&gt;    private int[] enemyY = { defaultEnemyplayerY, defaultEnemyplayerY, defaultEnemyplayerY,&lt;br /&gt;                           defaultEnemyplayerY, defaultEnemyplayerY, defaultEnemyplayerY};&lt;br /&gt;    private final int[] enemySpeed = { 18, 10, 25, 15, 30, 20};&lt;br /&gt;    private final Image enemyImage = Toolkit.getDefaultToolkit().getImage("ememy.gif");&lt;br /&gt;&lt;br /&gt;    //road parameters&lt;br /&gt;    private final int backWidth = 10;&lt;br /&gt;    private final int backHeight = 40;&lt;br /&gt;    private int backX = 300;&lt;br /&gt;    private int backY = -backHeight;&lt;br /&gt;    private final int stripesDelay = 200;&lt;br /&gt;    private final Color roadColor = new Color(255, 255, 255);&lt;br /&gt;&lt;br /&gt;    //panel parameters&lt;br /&gt;    private final Color panelColor = new Color(255, 255, 255);&lt;br /&gt;    private final int panelX = formDimension[2] - 150;&lt;br /&gt;    private final int panelY = formDimension[3] - 50;&lt;br /&gt;    private int keyCode = 0;&lt;br /&gt;    private Dimension frameSize = new Dimension(formDimension[2], formDimension[3] + 20);&lt;br /&gt;    private int timer = 100;&lt;br /&gt;    private Random ran = new Random();&lt;br /&gt;    private int level = 0;&lt;br /&gt;    private SensorObject senObj;&lt;br /&gt;    private SensorGuy test;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;6. GetSensorValue.pde, the code which is to be loaded onto arduino board.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/**&lt;br /&gt; * this code gets the value from the IR sensors and passes it to the&lt;br /&gt; * serial port.&lt;br /&gt; * @author Kinley Tshering&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;int irPin1 = 1;   //pin for sensor 1&lt;br /&gt;int irPin2 = 3;   //pin for sensor 2&lt;br /&gt;int sensor1Value = 0;&lt;br /&gt;int sensor2Value = 0;&lt;br /&gt;&lt;br /&gt;void setup() {&lt;br /&gt;  Serial.begin(9600); // Set serial out if we want debugging&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void loop() {&lt;br /&gt;  sensor1Value = analogRead(irPin1);&lt;br /&gt;  sensor2Value = analogRead(irPin2);&lt;br /&gt;  &lt;br /&gt;  if(sensor1Value &gt; 150)  {&lt;br /&gt;      Serial.println('1', BYTE);&lt;br /&gt;   }&lt;br /&gt;   if(sensor2Value &gt; 150)  {&lt;br /&gt;      Serial.println('2' , BYTE);&lt;br /&gt;   }&lt;br /&gt;   delayMicroseconds(100);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Follow the following steps in order to compile and run the game.&lt;br /&gt;&lt;br /&gt;1. go to a command prompt.&lt;br /&gt;2. cd to your arduino directory (i.e. type "cd \arduino")&lt;br /&gt;3. Issue the following command:&lt;br /&gt;   &lt;pre&gt;copy run.bat testrun.bat&lt;/pre&gt;&lt;br /&gt;4. Issue the following command to edit the new file.&lt;br /&gt;   &lt;pre&gt;notepad testrun.bat&lt;/pre&gt;&lt;br /&gt; Once open, make the following changes:&lt;br /&gt;a. add . to the beginning of the classpath (thats a period)&lt;br /&gt;&lt;pre&gt;set CLASSPATH=.;java\lib\rt.jar;etc...&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;b. change JAVA_HOME\java processing.app.Base&lt;br /&gt;&lt;br /&gt;to:&lt;pre&gt; rem JAVA_HOME\java processing.app.Base&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;c. on a new line after rem JAVA_HOME\java processing.app.Base&lt;br /&gt;&lt;br /&gt;add: &lt;pre&gt;%1 %2 %3&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;5. save and exit testrun.bat&lt;br /&gt;&lt;br /&gt;6. Run the following commands:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;testrun jikes SensorObject.java&lt;br /&gt;testrun jikes SensorEvent.java&lt;br /&gt;testrun jikes SensorListener.java&lt;br /&gt;testrun jikes SensorGuy.java&lt;br /&gt;testrun jikes DodgeMaster.java&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;7. Run the following command:&lt;br /&gt;&lt;pre&gt;testrun java DodgeMaster&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you have followed all the procedures correctly, your game should is running now.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7422250244359211400-2630487224025440130?l=kinleyphycom.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kinleyphycom.blogspot.com/feeds/2630487224025440130/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kinleyphycom.blogspot.com/2009/05/java-and-arduino-game.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/2630487224025440130'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/2630487224025440130'/><link rel='alternate' type='text/html' href='http://kinleyphycom.blogspot.com/2009/05/java-and-arduino-game.html' title='Java and Arduino Game'/><author><name>Kinley Tshering</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://4.bp.blogspot.com/_v34GvbHhf3A/SfPZY0kDICI/AAAAAAAAAKg/_TkGTNHg9us/S220/ava.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_v34GvbHhf3A/ShSz0XuLYTI/AAAAAAAAAMQ/vT9PK-X0fpw/s72-c/uml.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7422250244359211400.post-8465533177022260184</id><published>2009-04-28T04:26:00.026-04:00</published><updated>2009-05-20T22:30:20.310-04:00</updated><title type='text'>Connecting Arduino and Java using Serial Communications in Windows Environment</title><content type='html'>“Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.” The board can be loaded with programs simple programs developed using Arduino programming language – which is based on wiring – and Arduino IDE – based on processing, which in turn is based on Java. Arduino is mainly used for designing interactive prototypes and has the capability to be communicate with software (e.g. Flash, Java, Processing, etc) running on a computer using serial communication.&lt;br /&gt;In this tutorial, I will be using &lt;a href="http://silveiraneto.net/2009/03/01/arduino-and-java/"&gt;Silveira Neto's tutorial &lt;/a&gt; as a reference and as mentioned in the title, I'll focus only to Windows environment.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 1: Install Arduino IDE&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you haven't installed the Arduino IDE yet, download the IDE from &lt;a href="http://arduino.googlecode.com/files/arduino-0015-win.zip"&gt;here&lt;/a&gt;.  This is the latest version of Arduino as of April, 2009.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 2: Setup Arduino&lt;/span&gt;&lt;br /&gt;Connect the Arduino board to a serial port on your computer using a USB. Start the Arduino program and copy the following code.&lt;br /&gt;&lt;pre&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;blockquote style="color: rgb(0, 0, 0);"&gt;void setup(){&lt;br /&gt;Serial.begin(9600);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void loop(){&lt;br /&gt;Serial.println("Hello Java, Hi Arduino");&lt;br /&gt;delay(1000);&lt;br /&gt;}&lt;/blockquote&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This simple program basically sets up a serial communication at boud rate of  9600 and send the String “Hello Java, Hi Arduino"every 1000 milliseconds.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_v34GvbHhf3A/Sfa_a7cB-5I/AAAAAAAAALQ/G-ROolS9oDU/s1600-h/board.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 266px; height: 320px;" src="http://1.bp.blogspot.com/_v34GvbHhf3A/Sfa_a7cB-5I/AAAAAAAAALQ/G-ROolS9oDU/s320/board.JPG" alt="" id="BLOGGER_PHOTO_ID_5329657678392327058" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Upload the program onto the board by clicking the button which is marked red below. If you get any errors at this stage, check you have selected the right COM port to which your Arduino board is connected. Change the COM port by going to Tools → Serial Port on the menu bar.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 3: Install RXTX Library&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We will be using RXTX library to for serial communication. Check if the Arduino IDE you downloaded have two important files which you will need to create a serial communication: &lt;span style="font-style: italic;"&gt;rxtxSerial.dll&lt;/span&gt;, which will be in the main folder, and a jar file called &lt;span style="font-style: italic;"&gt;RXTXcomm.jar&lt;/span&gt;, which will be in the lib folder. If those files are missing, download then from &lt;a href="http://rxtx.qbang.org/pub/rxtx/rxtx-2.1-7-bins-r2.zip"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 4: Create a new NetBeans project&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;NetBeans is a open-source IDE by Sun Microsystems. The NetBeans Platform allows applications to be developed from a set of modular software components called modules. A module is a Java archive file that contains Java classes written to interact with the NetBeans Open APIs and a manifest file that identifies it as a module. If you don't have one yet, download from &lt;a href="http://www.netbeans.org/downloads"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Create a new project at File → New Project and choose at Java at Categories and Java Application at Projects.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_v34GvbHhf3A/Sfa_nEoey9I/AAAAAAAAALY/1Q-w4j4jCLw/s1600-h/new.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 218px;" src="http://3.bp.blogspot.com/_v34GvbHhf3A/Sfa_nEoey9I/AAAAAAAAALY/1Q-w4j4jCLw/s320/new.JPG" alt="" id="BLOGGER_PHOTO_ID_5329657887018896338" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_v34GvbHhf3A/Sfa_wviCekI/AAAAAAAAALg/740QVtg1wK0/s1600-h/project.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 206px;" src="http://1.bp.blogspot.com/_v34GvbHhf3A/Sfa_wviCekI/AAAAAAAAALg/740QVtg1wK0/s320/project.JPG" alt="" id="BLOGGER_PHOTO_ID_5329658053153421890" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Chose a name for your project. In the following screenshot, the name of project is ArduinoJava&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_v34GvbHhf3A/Sfa_2UHUcgI/AAAAAAAAALo/jJBdXpIZbFY/s1600-h/name.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 206px;" src="http://2.bp.blogspot.com/_v34GvbHhf3A/Sfa_2UHUcgI/AAAAAAAAALo/jJBdXpIZbFY/s320/name.JPG" alt="" id="BLOGGER_PHOTO_ID_5329658148872811010" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Click Finish.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 5: Adding external Libraries to NetBeans and setting up the working Directory&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;On NetBeans the Projects tab, right-click your project and choose Properties.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_v34GvbHhf3A/SfbAKlW0ZSI/AAAAAAAAAL4/h2RtAlub2HE/s1600-h/properties.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 234px;" src="http://3.bp.blogspot.com/_v34GvbHhf3A/SfbAKlW0ZSI/AAAAAAAAAL4/h2RtAlub2HE/s320/properties.JPG" alt="" id="BLOGGER_PHOTO_ID_5329658497098605858" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;On the Project Properties window select the Libraries on the Categories panel and click the Add JAR/Folder button. Find where you placed your Arduino IDE installation. Inside this directory there’s a lib directory will some JAR files. Select all them and click OK.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_v34GvbHhf3A/Sfa__6Pt_AI/AAAAAAAAALw/RFVZqJSy_Hk/s1600-h/add.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 146px;" src="http://1.bp.blogspot.com/_v34GvbHhf3A/Sfa__6Pt_AI/AAAAAAAAALw/RFVZqJSy_Hk/s320/add.JPG" alt="" id="BLOGGER_PHOTO_ID_5329658313727409154" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now, we will be using the Arduino IDE configuration for our program. So, in order to do so, the program need to know the location of configuration files. This can be achieved by assigning the current working directory to the directory of Arduino IDE. In the Project Properties window select Run at Categories panel. At Working Directory click in the Browse button and select the directory of your Arduino IDE.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_v34GvbHhf3A/SfbARQyHEvI/AAAAAAAAAMA/VhrQZljkZ0M/s1600-h/path.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 201px;" src="http://1.bp.blogspot.com/_v34GvbHhf3A/SfbARQyHEvI/AAAAAAAAAMA/VhrQZljkZ0M/s320/path.JPG" alt="" id="BLOGGER_PHOTO_ID_5329658611835015922" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Close the Project Properties window.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 6: Writing codes and running the program.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Copy the codes given below to the Main.java file (which is the a default file created by NetBeans with a simple class declaration and the main method.)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-style: italic; color: rgb(153, 0, 0);"&gt;&lt;blockquote&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* This class will get the input from arduino board and output&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* the data to the scanner&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* Code adopted from Silveira Neto&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;package arduinojava;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import gnu.io.CommPortIdentifier;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import gnu.io.SerialPort;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import java.io.InputStream;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import java.io.OutputStream;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import processing.app.Preferences;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* @author kinley tshering&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class Main {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;static InputStream input;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;static OutputStream output;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;static CommPortIdentifier portId;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;static SerialPort port;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* @param args the command line arguments&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public static void main(String[] args) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  //code application logic here&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     Preferences.init();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     System.out.println("Using port: " + Preferences.get("serial.port"));&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    try {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            portId = CommPortIdentifier.getPortIdentifier(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                   Preferences.get("serial.port"));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          //port = (SerialPort)portId.open("serial talk", 4000);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          port = (SerialPort)portId.open("", 4500);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          input = port.getInputStream();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          output = port.getOutputStream();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                  SerialPort.DATABITS_8,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                  SerialPort.STOPBITS_1,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                  SerialPort.PARITY_NONE);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;          while(true){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                      while(input.available() &gt; 0) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                                   System.out.print((char)(input.read()));&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                       }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  catch(gnu.io.NoSuchPortException nsp)  {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       System.err.println("ERROR: " + nsp.getMessage());&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  catch(gnu.io.UnsupportedCommOperationException usp)  {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       System.err.println("ERROR: " + usp.getMessage());&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   catch(gnu.io.PortInUseException pie)  {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       System.err.println("ERROR: Port " + port + " is already in use\nCLose the port and restart.");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   catch(java.io.IOException ioe) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       System.err.println("IO ERROR: " + ioe.getMessage() );&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   catch(Exception exe) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       System.err.println("ERROR: Unexpected error occured \n" + exe.getMessage() );&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;/span&gt; &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Compile the code by going to Run → Built Main Project or press F11 key. After the code compiles, run the program by going to Run → Run Main Project or press F6 key. If you followed every step correctly, you will see “ Hello Java, Hi Arduino” printed every 1000 milliseconds (one second).&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_v34GvbHhf3A/SfbAZGGYl7I/AAAAAAAAAMI/abhmMXJ8PgY/s1600-h/output.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 239px;" src="http://1.bp.blogspot.com/_v34GvbHhf3A/SfbAZGGYl7I/AAAAAAAAAMI/abhmMXJ8PgY/s320/output.JPG" alt="" id="BLOGGER_PHOTO_ID_5329658746406213554" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step 7: Go ahead and do whatever you like.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7422250244359211400-8465533177022260184?l=kinleyphycom.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kinleyphycom.blogspot.com/feeds/8465533177022260184/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kinleyphycom.blogspot.com/2009/04/connecting-arduino-and-java-in-windows.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/8465533177022260184'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/8465533177022260184'/><link rel='alternate' type='text/html' href='http://kinleyphycom.blogspot.com/2009/04/connecting-arduino-and-java-in-windows.html' title='Connecting Arduino and Java using Serial Communications in Windows Environment'/><author><name>Kinley Tshering</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://4.bp.blogspot.com/_v34GvbHhf3A/SfPZY0kDICI/AAAAAAAAAKg/_TkGTNHg9us/S220/ava.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_v34GvbHhf3A/Sfa_a7cB-5I/AAAAAAAAALQ/G-ROolS9oDU/s72-c/board.JPG' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7422250244359211400.post-1278613996612783494</id><published>2009-04-22T11:09:00.009-04:00</published><updated>2009-04-29T12:03:11.026-04:00</updated><title type='text'>Smart House Prototype</title><content type='html'>For this project, I am supposed to control motors/fan by using Arduino. I have built a prototype of a smart house. This prototype if made practical will be able to prevent unnecessary energy wastage. The lights will turn on only when there is some person inside the room/house and the fan/AC will turn on when the room temperature reaches some limit and will turn off once the room temperature falls below certain level.&lt;br /&gt;&lt;br /&gt;In order to achieve the above mentioned functions, I used an IR sensor to keep track of objects and a thermistor to measure the change in temperature. In order to prevent unnecessary waste of power, I used a PNP transistor which will act as a switch for the fan/AC. In doing so, I made it sure that the circuit will be completed only when the temperature reaches the critical level. Once the temperature reaches a critical level, I sent a digital signal to the base of PNP transistor which will result in the flow of current between emitter and collector of the transistor.&lt;br /&gt;&lt;br /&gt;The main controlling component of the system is the Arduino system. The micro-controller board is loaded with the program written below in arduino language. The code is provided below:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-family: courier new;font-family:Courier;color:blue;"  &gt;/*&lt;br /&gt;* SmartHouse(Project III: Physical Computing)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;* Analog input: One IR sensor, One Thermistor&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;* Digital output: Fan controlled by NPN transistor, LED&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;* &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;* @author Kinley Tshering&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;* @instructor Prof. Harris&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255); font-family: courier new;font-size:100%;" &gt;&lt;span style="color: rgb(51, 51, 255);"&gt;int transistorPin = 3;   // to transistor base&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;int ledPin = 5;               // to LED&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;int presence = 0;          //value obtained from the IR distance sensor&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;int heat = 0;                  //value obtained from thermistors&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;//set up the system&lt;br /&gt;void setup() {&lt;br /&gt;   //instantiate the serial communication at 96000 baud rate&lt;br /&gt;  Serial.begin(9600);&lt;br /&gt; // set  the transistor pin as output&lt;br /&gt; pinMode(transistorPin, OUTPUT);&lt;br /&gt; // set  the LED pin as output&lt;br /&gt; pinMode(ledPin, OUTPUT);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void loop() {&lt;br /&gt;&lt;br /&gt; //read the value of IR sensor&lt;br /&gt; presence = analogRead(3);&lt;br /&gt;&lt;br /&gt; //read the value of Thermistor&lt;br /&gt; heat = analogRead(2);&lt;br /&gt;&lt;br /&gt; //digitalWrite(ledPin, LOW);&lt;br /&gt;&lt;br /&gt; if(presence &gt; 150) {&lt;br /&gt;   digitalWrite(ledPin, LOW);&lt;br /&gt; &lt;br /&gt;   if(heat &gt; 40) {&lt;br /&gt;       digitalWrite(transistorPin,  HIGH);&lt;br /&gt;       delay(10);&lt;br /&gt;       digitalWrite(transistorPin, LOW);&lt;br /&gt;       delay(10);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; else {&lt;br /&gt;   digitalWrite(ledPin, HIGH);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The connections and set up is shown below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_v34GvbHhf3A/SfTIuE1qZQI/AAAAAAAAALI/X3Xqp6OMs_M/s1600-h/IMG_0629.JPG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;" src="http://1.bp.blogspot.com/_v34GvbHhf3A/SfTIuE1qZQI/AAAAAAAAALI/X3Xqp6OMs_M/s320/IMG_0629.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5329104952984495362" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7422250244359211400-1278613996612783494?l=kinleyphycom.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kinleyphycom.blogspot.com/feeds/1278613996612783494/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kinleyphycom.blogspot.com/2009/04/smart-house-prototype.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/1278613996612783494'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/1278613996612783494'/><link rel='alternate' type='text/html' href='http://kinleyphycom.blogspot.com/2009/04/smart-house-prototype.html' title='Smart House Prototype'/><author><name>Kinley Tshering</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://4.bp.blogspot.com/_v34GvbHhf3A/SfPZY0kDICI/AAAAAAAAAKg/_TkGTNHg9us/S220/ava.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_v34GvbHhf3A/SfTIuE1qZQI/AAAAAAAAALI/X3Xqp6OMs_M/s72-c/IMG_0629.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7422250244359211400.post-854476079755678726</id><published>2009-04-12T06:18:00.007-04:00</published><updated>2009-04-29T12:03:18.547-04:00</updated><title type='text'>MarcoPolo</title><content type='html'>This is the second project for this course. We have to turn an existing physical behavior into an interactive experience using a person's clothing or worn accessories. The electronic enhancements will have to be achieved through the use of transistors or Integrated circuit (IC).&lt;br /&gt;&lt;br /&gt;For this project, I decided to make a simple device (a system of sensors and speakers) to help the blind people to commute. I think I will really name it MarcoPolo – as suggested by few of my classmates. Well, basically I have a system which is a collection of sensors, speakers, and transistors which are all centrally controlled by a micro-controller (Arduino).  When the person wearing the device is approaches a solid object, – about 0.5 meters – the Infrared distance sensor will activate the transistor's base, which in turn will supply power to the speaker. Upon hearing the signal from the speaker, the person can avoid hitting any solid objects.&lt;br /&gt;&lt;br /&gt;The code written in processing is provided below:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=";font-family:Courier;color:blue;"  &gt;/*&lt;br /&gt;* MarcoPolo(Project II: Physical Computing)&lt;br /&gt;*&lt;br /&gt;* Analog input: Two IR distance sensors&lt;br /&gt;* Analog output: Speaker controlled by transistor&lt;br /&gt;*&lt;br /&gt;* @author Kinley Tshering&lt;br /&gt;* @instructor Prof. Harris&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int speakerOut = 9;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; //the speaker pin&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int irPin1 = 0;   //pin for sensor 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int irPin2 = 1;   //pin for sensor 2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int sensor1Value = 0;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; //variable to store the value from first sensor &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int sensor2Value = 0;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; //variable to store the value from second sensor &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;void setup() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  pinMode(speakerOut, OUTPUT);&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;  ///make the speaker pin as output.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  Serial.begin(9600); // Set serial out if we want debugging&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;// Set length of pause between notes&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int pause = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;// Loop variable to increase Rest length&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int rest_count = 100; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;// Initialize core variables&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;int tone = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;long duration  = 50000;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;// Pulse the speaker to play a tone for a particular duration&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;void playTone() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  long elapsed_time = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  if (tone &gt; 0) { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;    while (elapsed_time &lt;&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;       digitalWrite(speakerOut,HIGH);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      delayMicroseconds(tone / 2);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      // DOWN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      digitalWrite(speakerOut, LOW);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      delayMicroseconds(tone / 2);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      // Keep track of how long we pulsed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      elapsed_time += (tone);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  else { // Rest beat; loop times delay&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;    for (int j = 0; j &lt;&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      delayMicroseconds(duration);  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;    }                                &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  }  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;void loop() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  sensor1Value = analogRead(irPin1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;   sensor2Value = analogRead(irPin2);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;  if(sensor1Value &gt; 150 || sensor2Value &gt; 150)  {&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;  //check for values&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;      tone = 1000;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;   else {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;    tone = 0;  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;   }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;   playTone();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;   delayMicroseconds(1000);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;[Note: The detailed working (including setup) and demonstration is provided in the video which I still being uploaded by Google. ]&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7422250244359211400-854476079755678726?l=kinleyphycom.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kinleyphycom.blogspot.com/feeds/854476079755678726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kinleyphycom.blogspot.com/2009/04/marcopolo.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/854476079755678726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/854476079755678726'/><link rel='alternate' type='text/html' href='http://kinleyphycom.blogspot.com/2009/04/marcopolo.html' title='MarcoPolo'/><author><name>Kinley Tshering</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://4.bp.blogspot.com/_v34GvbHhf3A/SfPZY0kDICI/AAAAAAAAAKg/_TkGTNHg9us/S220/ava.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7422250244359211400.post-8697343037946599973</id><published>2009-03-18T00:18:00.005-04:00</published><updated>2009-04-29T12:03:26.001-04:00</updated><title type='text'>Project I</title><content type='html'>&lt;object width="401" height="307" class="BLOG_video_class" id="BLOG_video-57d33bf0fb97e085" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;&lt;param name="bgcolor" value="#FFFFFF"&gt;&lt;param name="allowfullscreen" value="true"&gt;&lt;param name="flashvars" value="flvurl=http://v3.nonxt7.googlevideo.com/videoplayback?id%3D57d33bf0fb97e085%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1331162754%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D23B4B1F5C8B640EBDCEEF3AE61CBA2C777A40C43.33D8ED38DF46871DA6D14C452D033A3C5BA4D459%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D57d33bf0fb97e085%26offsetms%3D5000%26itag%3Dw160%26sigh%3DHoxO3d55TU7_nJCa3Wq7oYdpP1A&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"width="401" height="307" bgcolor="#FFFFFF"flashvars="flvurl=http://v3.nonxt7.googlevideo.com/videoplayback?id%3D57d33bf0fb97e085%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1331162754%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D23B4B1F5C8B640EBDCEEF3AE61CBA2C777A40C43.33D8ED38DF46871DA6D14C452D033A3C5BA4D459%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D57d33bf0fb97e085%26offsetms%3D5000%26itag%3Dw160%26sigh%3DHoxO3d55TU7_nJCa3Wq7oYdpP1A&amp;autoplay=0&amp;ps=blogger"allowFullScreen="true" /&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;It has only been one week (two classes of two hours each to be precise) and we have to submit the first project on the third day. In the first project, we are supposed to use Arduino inputs to control LEDs. The LED should be controlled both by a digital input as well as an analog control system.&lt;br /&gt;For my project, I chose to control a series of LEDs by a switch and a Light Dependant Resistor&lt;br /&gt;(LDR). The switch is the main control system – the system will work only when the switch is on) – and the time interval between the blinking of LEDs is controlled by the LDR. LDRs can sense the surrounding temperature and give a value to the program that calls it.&lt;br /&gt;&lt;br /&gt;&lt;font style="" color="blue" face="Courier"&gt;//The program reads the analog value from pin 3.&lt;/font&gt;&lt;br /&gt;   &lt;font style="" color="blue" face="Courier"&gt;input = analogRead(3);  &lt;/font&gt;&lt;br /&gt;&lt;br /&gt;So basically the three components (LED series, LDR and circuit switch) are independent of each other. They are all connected to the Arduino which controls and makes them work in one final system.&lt;br /&gt;&lt;br /&gt;The code - written in processing - is given below:&lt;br /&gt;&lt;br /&gt;&lt;font style="" color="blue" face="Courier"&gt;/*&lt;br /&gt;* Blink (Project I: Physical Computing)&lt;br /&gt;*&lt;br /&gt;* Digital input: switch.&lt;br /&gt;* Analog input: LDR (Light Dependant Resistor)&lt;br /&gt;* Digitial output: 4 LEDs(Light Emiting Diodes)&lt;br /&gt;* (green, yellow and red LEDs).&lt;br /&gt;* @author Kinley Tshering&lt;br /&gt;* @instructor Prof. Harris&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;// the first LED to be lit/the LED which is lit currently&lt;br /&gt;int currentPin = 2;&lt;br /&gt;&lt;br /&gt;// the direction of light&lt;br /&gt;boolean increase = true;&lt;br /&gt;&lt;br /&gt;// interval between the light&lt;br /&gt;float intv = 150.0;&lt;br /&gt;&lt;br /&gt;// variable to store the value obtained from the LDR&lt;br /&gt;int ldr;&lt;br /&gt;&lt;br /&gt;// the pin which has the switch.&lt;br /&gt;int switchPin = 12;&lt;br /&gt;&lt;br /&gt;void setup(){&lt;br /&gt;Serial.begin(9600);&lt;br /&gt;pinMode(2,OUTPUT);&lt;br /&gt;pinMode(3,OUTPUT);&lt;br /&gt;pinMode(4,OUTPUT);&lt;br /&gt;pinMode(5,OUTPUT);&lt;br /&gt;pinMode(switchPin,ldr);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void loop(){       //loop through it&lt;br /&gt;&lt;br /&gt;// if the circuit is complete around the swtich......&lt;br /&gt;//light up the LCDs adjust the direction of light in the LEDs&lt;br /&gt;if (digitalRead(switchPin) == HIGH){&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;font style="" color="blue" face="Courier"&gt; //if the currentPin is 5 (i.e the largest value of LED),&lt;br /&gt;//set increase to false&lt;/font&gt;&lt;br /&gt;&lt;font style="" color="blue" face="Courier"&gt;    if (currentPin == 5){&lt;br /&gt;     increase = false;&lt;br /&gt; }&lt;br /&gt;&lt;/font&gt;&lt;font style="" color="blue" face="Courier"&gt;//else make the increase to true. &lt;/font&gt;&lt;br /&gt;&lt;font style="" color="blue" face="Courier"&gt;    else if (currentPin == 2){&lt;br /&gt;     increase = true;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; if (increase){&lt;br /&gt;     currentPin++; //increment the LED to be lit&lt;br /&gt; }&lt;br /&gt; else{&lt;br /&gt;     currentPin--;  //Decrement the LED to be lit&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; // turn the LED off&lt;br /&gt; digitalWrite(currentPin, LOW);&lt;br /&gt;&lt;br /&gt; // Set the delayRate based on the LDR&lt;br /&gt; setInterval();&lt;br /&gt;&lt;br /&gt; // and wait that long...&lt;br /&gt; delay(intv);&lt;br /&gt;&lt;br /&gt; // turn the LCD on.&lt;br /&gt; digitalWrite(currentPin, HIGH);&lt;br /&gt;&lt;br /&gt;} else { //the switch is off&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void setInterval(){&lt;br /&gt;//makes sure that the minimum value is 100.&lt;br /&gt;&lt;/font&gt;&lt;font style="" color="blue" face="Courier"&gt;//get the value(0 to 1023) from the LDR which is in pin 3.&lt;/font&gt;&lt;br /&gt;&lt;font style="" color="blue" face="Courier"&gt;  ldr = 100 + analogRead(3) / 10;&lt;br /&gt;intv = (float)ldr;&lt;br /&gt;Serial.println(ldr);&lt;br /&gt;}&lt;/font&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7422250244359211400-8697343037946599973?l=kinleyphycom.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='enclosure' type='video/mp4' href='http://www.blogger.com/video-play.mp4?contentId=57d33bf0fb97e085&amp;type=video%2Fmp4' length='0'/><link rel='replies' type='application/atom+xml' href='http://kinleyphycom.blogspot.com/feeds/8697343037946599973/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kinleyphycom.blogspot.com/2009/03/project-i.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/8697343037946599973'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/8697343037946599973'/><link rel='alternate' type='text/html' href='http://kinleyphycom.blogspot.com/2009/03/project-i.html' title='Project I'/><author><name>Kinley Tshering</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://4.bp.blogspot.com/_v34GvbHhf3A/SfPZY0kDICI/AAAAAAAAAKg/_TkGTNHg9us/S220/ava.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7422250244359211400.post-3481537918589461638</id><published>2009-03-17T15:51:00.002-04:00</published><updated>2009-04-29T12:03:34.078-04:00</updated><title type='text'>Introduction</title><content type='html'>I have created this blog for a class I am taking this quarter - Physical Computing. It is required for me to update my blog regularly about the progress of assignments, projects and in-class discussions. &lt;br /&gt;&lt;br /&gt;Physical Computing (a.k.a Embedded Computing) is a branch of computing in which interactive physical systems are developed using hardware and software that can sense and respond to the analog (the actual world we are living in) world. So, sensors - senors which can sense touch, motion, light, temperature, humidity, etc - are the main component of physical computing. &lt;br /&gt;&lt;br /&gt;Actually embedded systems are everywhere nowadays. You stay in a room for about five minutes without moving and the light turns off. You go to a mall and as soon as you are near the door, the door automatically opens itself for you. Those systems are all using sensors which which senses the action and responds accordingly.&lt;br /&gt;&lt;br /&gt;I will be posting about my projects and assignments - design diagrams, codes, explanations, etc - using images and videos wherever possible. As of now, bye.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7422250244359211400-3481537918589461638?l=kinleyphycom.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kinleyphycom.blogspot.com/feeds/3481537918589461638/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kinleyphycom.blogspot.com/2009/03/introduction.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/3481537918589461638'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7422250244359211400/posts/default/3481537918589461638'/><link rel='alternate' type='text/html' href='http://kinleyphycom.blogspot.com/2009/03/introduction.html' title='Introduction'/><author><name>Kinley Tshering</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://4.bp.blogspot.com/_v34GvbHhf3A/SfPZY0kDICI/AAAAAAAAAKg/_TkGTNHg9us/S220/ava.JPG'/></author><thr:total>0</thr:total></entry></feed>
