Ex: Temperature Sensor

This circuit works like a thermostat. As soon as the temperature goes higher than the 'hi-threshold', the led lights up to let you know it's too hot.

I tried using the alert bit to control the led at first. It didn't work too well, and very unpredictably. I doubt it even was working through the alert bit at all. So, for now it's just a brute force led turning up when the temperature goes higher than the value stored in the T_high register.

Ex: Temperature Sensor from naureenm on Vimeo.



// ARDUINO CODE

#include "wire.h" // (where " "=angled brackets)

int led = 2;

int ptrTemp =  0b00000000;         // TMP102 register pointers
int ptrConf =  0b00000001;
int ptrTLow =  0b00000010;
int ptrTHigh = 0b00000011;
int i2cAddr;                        // used to set address on the chip

void setup()
{
  pinMode(led,OUTPUT);
  Serial.begin(9600);
  Wire.begin();                     // join i2c bus (address optional for master)
  i2cAddr = 0b1001001;              // ADD0 pin connection selects one of four possible addresses
}

void loop()

//  REQUESTING TEMPERATURES
 /*  TEMPERATURE REGISTER
 */
  Wire.beginTransmission(i2cAddr);  // select temperature register
  Wire.send(ptrTemp);               // (if only temperature is needed this can
  Wire.endTransmission();           // be done once in setup() )

  Wire.requestFrom(i2cAddr, 2);     // request temperature
  byte byte1 = Wire.receive();
  byte byte2 = Wire.receive();   
 
 /*  T_LOW REGISTER
 */
  Wire.beginTransmission(i2cAddr);  // select temperature register
  Wire.send(ptrTLow);
  Wire.send(24);  // (if only temperature is needed this can
  Wire.endTransmission();           // be done once in setup() )

  Wire.requestFrom(i2cAddr, 2);     // request temperature
  byte lowbyte1 = Wire.receive();
  byte lowbyte2 = Wire.receive();   
 
 /*  T_High REGISTER
 */
  Wire.beginTransmission(i2cAddr);  // select temperature register
  Wire.send(ptrTHigh); 
  Wire.send(28);    // (if only temperature is needed this can
  Wire.endTransmission();           // be done once in setup() )

  Wire.requestFrom(i2cAddr, 2);     // request temperature
  byte hibyte1 = Wire.receive();
  byte hibyte2 = Wire.receive();   
 
//  CALCULATING TEMPERATURES
  int tempint = byte1 << 8;         // shift first byte to high byte in an int
  tempint = tempint | byte2;        // or the second byte into the int
  tempint = tempint >> 4;           // right shift the int 4 bits per chip doc
  float tempflt = float( tempint ) * .0625; // calculate actual temperature per chip doc
 
 //  Low_temp 
  int lowtempint = lowbyte1 << 8;         // shift first byte to high byte in an int
  lowtempint = lowtempint | lowbyte2;        // or the second byte into the int
  lowtempint = lowtempint >> 4;           // right shift the int 4 bits per chip doc
  float lowtempflt = float( lowtempint ) * .0625; // calculate actual temperature per chip doc

//  Hi_temp
  int hitempint = hibyte1 << 8;         // shift first byte to high byte in an int
  hitempint = hitempint | hibyte2;        // or the second byte into the int
  hitempint = hitempint >> 4;           // right shift the int 4 bits per chip doc
  float hitempflt = float( hitempint ) * .0625; // calculate actual temperature per chip doc


 // PRINTING TEMPERATURES
  Serial.print( "Temp: C = ");
  Serial.print( int( tempflt ) );
  int tempF = (tempflt * 9/5) + 32;
  Serial.print( " F = ");
  Serial.print( int( tempF ) );

  Serial.print( "   Low:  C = ");
  Serial.print( int( lowtempflt ) );
  int lowF = (lowtempflt * 9/5) + 32;
  Serial.print( " F = ");
  Serial.print( int( lowF ) );
 
  Serial.print( "   Hi:  C = ");
  Serial.print( int( hitempflt ) );
  int hiF = (hitempflt * 9/5) + 32;
  Serial.print( " F = ");
  Serial.println( int( hiF ) );
 
 
 //  LIGHTING UP THE LED IF TOO HOT
  if ( tempflt > hitempflt )
    digitalWrite(led,HIGH);
  else
    digitalWrite(led,LOW);
   
  delay(500);
}

Ex: Rock_Paper_Scissor w/ Arduinos Connected

Arduinos connected through serial connections. Using RGB in place of rock, paper and scissor.

R beats G
G beats B
B beats R

-------- ARDUINO CODE -------------

 int RxPin = 0;
 int TxPin = 1;

 int button1Pin = 2;    // select the input pin for the potentiometer
 int button2Pin = 4;

 int p1color = -1;
 int b1currState = 0;
 int b1prevState = 0;

 int b2count = 0;
 int b2currState = 0;
 int b2prevState = 0;

 int ledPin1 = 11;
 int ledPin2 = 10;
 int ledPin3 = 9;

 int ledWinPin = 5;
 int ledLosPin = 6;

 boolean hold = true;
 int checkbit = 0;
 int p2color = -1;

 void setup() {  
   pinMode(button1Pin, INPUT);
   pinMode(button2Pin, INPUT);
  
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);
   pinMode(ledPin3, OUTPUT);
  
   pinMode(ledWinPin, OUTPUT);
   pinMode(ledLosPin, OUTPUT);

//   pinMode(RxPin, INPUT);
//   pinMode(TxPin, OUTPUT);
  
   Serial.begin(9600);
 }


void loop()
{
  b1currState  = digitalRead(button1Pin);
  b2currState  = digitalRead(button2Pin);

/*  STEP 1: CHOOSING WHICH COLOR
    0 = nothing - cannot send when LED is in this state
    1 = R
    2 = G
    3 = B   
*/
  // getting input from the RGB LED button
  // to learn which LED is chosen
  // and saaving the chosen color in p1color
 
   if (b1currState != b1prevState)
   {
       if (b1currState == HIGH)
       {
         if (p1color == 2) p1color = 0;
         else  p1color++;
         Serial.print("RGB LED state:  ");
         Serial.println(p1color, DEC);
       }
       b1prevState = b1currState; 
       delay (100);   
   }
  
   if (p1color == 0)
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
   } else
   if (p1color == 1)
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
   } else
   if (p1color == 2)
   {
    digitalWrite(ledPin1, HIGH);  
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
   }else
   {
    digitalWrite(ledPin1, LOW);  
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
   }
  
/*  STEP 2: SENDING CHOSEN COLOR TO OTHER PLAYER
*/
   if (b2currState != b2prevState)
   {
       if (b2currState == HIGH)
       {
         Serial.print("Sending state = "); Serial.print(p1color, DEC);
         Serial.println(" to other player's Arduino...");
                
//         digitalWrite(TxPin, 3);
//         digitalWrite(TxPin, p1color);
        
         Serial.write(255);
         Serial.write(p1color);
       }
       b2prevState = b2currState;   
       delay(100);   
   }
  
  
/*  STEP 3: HOLDING UNTIL COLOR RECEIVED
            FROM OTHER PLAYER
*/
  if (Serial.available())
  {
//    p2color = digitalRead(RxPin); //Serial.read();
    p2color = Serial.read();
   
    if (!hold)
    {     
      if ( (p1color == 0 && p2color == 1)
         ||(p1color == 1 && p2color == 2)
         ||(p1color == 2 && p2color == 0)  )
      {
        digitalWrite(ledWinPin,HIGH);
        digitalWrite(ledLosPin,LOW);
      }
      else    
      if (p2color == p1color)
      {
        digitalWrite(ledWinPin,HIGH);
        digitalWrite(ledLosPin,HIGH);
      }
      else
      {
        digitalWrite(ledWinPin,LOW);
        digitalWrite(ledLosPin,HIGH);
      }
     
      delay (20000);
     
      // resetting after some time
      hold = true;     
      p2color = -1;
      digitalWrite(ledWinPin,LOW);
      digitalWrite(ledLosPin,LOW);
    }
   
    if (p2color == 255) //255
      hold = false; 
  }
}










Ex: Blinker / Dimmer in Serial Connection





The Arduino Code:


 const int ledPin = 9;      // pin that the LED is attached to
 byte brightness = 0;
 byte blink_rate = 0;
 byte checkbit = 0;

 int state = 0;

 int ledState = LOW;        // to store LED states
 long previousMillis = 0;   // to store last time LED was updated


 void setup()
 {
   // initialize the serial communication:
   Serial.begin(9600);
   // initialize the ledPin as an output:
   pinMode(ledPin, OUTPUT);
 }

 void loop()
 {
   unsigned long currentMillis = millis();

   // check if data has been sent from the computer:
   if (Serial.available())
   {
     // read the most recent byte (from 0 to 255):
    checkbit = Serial.read();
    if (state == 2)
    {
      blink_rate = checkbit;
      state = 3;
    }
    if (state == 1)
    {
      brightness = checkbit;    
      state = 2;
    }
    if (checkbit == 255)
    {
        state = 1;
    }
   } 
  

  if(currentMillis - previousMillis > blink_rate)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
       ledState = HIGH;
       analogWrite(ledPin, brightness);
    }
    else
    {
        ledState = LOW;
        analogWrite(ledPin, 0);
    }
  }
}

........................................................................................
Processing code:



  import processing.serial.*;
  Serial port;
//  byte start;
 
  void setup()
  {
  size(254, 150);
 
  println("Available serial ports:");
  println(Serial.list());
 
  // Uses the second port in this list (number 1). 
  port = new Serial(this, Serial.list()[1], 9600); 
  }
 
  void draw()
  {
  // draw a gradient from black to white
  for (int i = 0; i < 254; i++) {
  stroke(i);
  line(i, 0, i, 150);
  }
 
  // write the current X & Y-position of the mouse to the
  // serial port as a single byte




    port.write(255);
    port.write(mouseX); println("X: " + mouseX);
    port.write(mouseY); println("Y: " + mouseY);
  }
 

Ex: Sensor / Actuator Walk

I took these images walking around between the Computer Science, Architecture and Visualization buildings:

Tap / Swipe Credit card to pay. The machine recognizes certain credit cards which can be tappedinstead of swiped to make payments.




Automatic-Doors (Motion sensors):



Card-Reader with a timed-unlock mechanism, and a button to automatically open the door (also timed):


 

Bill-reader, starts pulling a bill inside if it senses the right amount of a dollar bill being slipped into the reader.
 

Motion-sensor Automatic Dispensers:


Ex: Push-button LED states

I kept having a problem with the push-buttons while working on the exercise. The circuit was pretty simple/fast to set up but, the coding didn't seem to be perfect for quite some time. The button pushes were being recognized and read properly by the program but, they weren't corresponding to the events assigned to them. I realized that was probably due to the button bouncing.

De-bouncing: I added/edited/re-edited and then finally removed the de-bouncing code in attempts to fix the problem. Adding the de-bouncing code just made the system become much slower (it wouldn't start recognizing button-pushes until several minutes after the code had finished uploading).
Eventually, I just added a 'delay(100)' at the end of the code.Which appeared to work very good as a de-bouncer, at least for this program! ..it might not be the best way to go about it but, it's working for me for now.

Then I also went ahead and added additional effects to the states of each button-push-event:
0 pushes: no lights
1 push: green light with slow blinking
2 pushes: blue light with faster blinks
3 pushes: red light blinking very fast






MAIN LOOP AREA PRINTED BELOW:
------------------------------


void loop()
{
  currState  = digitalRead(buttonPin);

  // compare the currState to its previous state
   if (currState != prevState)
   {
       if (currState == HIGH)
       {
         if (count == 3) count = 0;
         else  count++;
         Serial.print("number of button pushes:  ");
         Serial.println(count, DEC);
       }
       prevState = currState;   
   }
 
   if (count == 1)
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, 50);
    delay(100);                  // wait for a second
    digitalWrite(ledPin3, LOW);    // set the LED off
    delay(100);                  // wait for a second
   } else
   if (count == 2)
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin2, 50);
    delay(50);                  // wait for a second
    digitalWrite(ledPin2, LOW);    // set the LED off
    delay(50);
   } else
   if (count == 3)
   {
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin1, 50);
    delay(10);                  // wait for a second
    digitalWrite(ledPin1, LOW);    // set the LED off
    delay(10);   
  
   } else
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);  
   }
 
  delay (100);
}

Proj 1: (Updated) The Weather Man

The idea is to have a painting of an animated character on an LCD digital frame (like the ones used as picture frames for displaying digital picture slide-shows). This picture frame is to be hung inside a house as art. The animations of the character are controlled and changed depending on input received from a few wireless sensors outside the house.


Input will be received from a temperature sensor, humidity / precipitation sensor, and a wind pressure sensor:
  1. Temperature would affect the character's mood (happy at moderate temperatures, sad or very sad at extreme cold or hot temperatures), his clothes (suiting the weather outside) and actions (fanning self when it's too hot, or shivering in extreme cold). 
  2. Precipitation or humidity will result in the character pulling out an umbrella on a rainy day.
  3. Wind pressure would show the character trying not to blow away in a heavy wind when there is high winds outdoors.
The piece will be meant solely for weather information that can be always available to anyone within a room in the form of an interesting looking art animated piece of art on the wall. The animation will continue changing throughout the day depending on the temperature. And when the temperature becomes stable for some time, the animation will be limited to the character just blinking and shifting on his  feet or looking around at the corners of the frame.

Proj 1: Temperature/Light Controlled Animated Painting

The basic idea is to have a simple image of a field/landscape displayed in a frame on a wall. The frame is connected to a couple of sensors outdoors that gauge the temperature and the light intensity outdoors.

Temperature Sensor:
Depending on what the temperature is like outside, the landscape will transform into something like a desert (at hi temperatures), or becomes very green with plants and trees to depicts spring (at mi-range temperatures), or transforms into a cold / snow-filled landscape (at very low temperatures).

Light Sensor:
The painting also depicts day and night depending on whether there's any light outside or not. I don't know if it can accurately measure differences in light-intensity if the sky is overcast etc. but, if so, that would be a welcome addition too.



More sensors that can be added:
Humidity:
With a sensor for humidity added to the system, there can be an additional animation of rain or snow (depending on the temperature) in the painting.

Air pressure:
With a sensor for air pressure added, the system could also display something like a storm, windy day etc.