Monday, January 28, 2013

Intro to Arduino - Two Tasks : #1) 8 Sequentially Blinking LEDs #2) Ambient Temperature Sensor

Documentation by Andrew Samuels

We completed two tasks assigned to us in our introduction to the Arduino microcontroller. The first task was to instruct the Arduino to blink 8 LEDs in sequence without the Arduino programming language's delay() function. The second task was to measure the ambient temperature sensor using the TMP36 sensor. Arduino code is included for both tasks.

#1) 8 Blinking LEDs in Sequence



/* 8 blinking LEDS w/ no delay func
hopefully this fixes the random LED problem
*/

//int timer = 100; //higher number, slower timing

//changing variables
int ledState = LOW; //used to set the LED
long previousMillis = millis(); //will store last time LED was updated

long interval = 500; //interval at which to blink
int thisPin = 5;
int startPin = 6;
void setup(){
//initialize each pin as output
for(int thisPin = 6; thisPin < 13; thisPin++){
    pinMode(thisPin, OUTPUT);
  }
}

void loop(){
//loop from the lowest pin to the highest pin
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval){
      //save the last time you blinked the LED
      previousMillis = currentMillis;
      digitalWrite(thisPin, LOW);
      thisPin++;
      
      //set the LED with ledState
      if (thisPin == 14){
      digitalWrite(startPin, HIGH);
      thisPin = startPin;
    }
      else{
      digitalWrite(thisPin, HIGH);
      }
      //If LED is off turn it on, if on..turn off
    }
    
      
    
    }

#2) Ambient Temperature Sensor (TMP36)


/* Temperature Sensor with TMP36
Measures the ambient temperature of the room.
*/

int sensPin = 0; //analog pin for TMP36

void setup(){
  Serial.begin(9600); //initialize serial connection with computer
     //to view results
}


void loop(){
//get the voltage reading from the temp sensor
int reading = analogRead(sensPin);

//convert reading to voltage
float voltage = reading * 5.0;
voltage /= 1024.0; //divide and assign voltage

//print voltage
Serial.print(voltage);
Serial.println(" volts");

//convert voltage to tempurature in Celsius, then print
float temperatureC = (voltage - 0.5) * 100; //convert 10mV per degree
//with 500 mV offset to degrees 
Serial.print(temperatureC);
Serial.println(" degrees C");

delay(1000);

}

No comments:

Post a Comment