Skip to main content

Day 13: 2-Dimensional Arrays || I2C Sensors

Lab: I2C Temperature Sensor


Figure 1.1
Serial monitor displaying temperature data
Code
//Reads Temp from I2C temperature sensor and prints it on the serial port
//Include Wire I2C library
#include <Wire.h>
int temp_address = 72; //1001000 written as decimal number
void setup()
{
 //Start serial communication at 9600 baud
 Serial.begin(9600);
 //Create a Wire object
 Wire.begin();
}
void loop()
{
 //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0);
 //Get the temp and read it into a variable
 int c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 int f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.println("F");
 delay(500);
}


Software


Arrays are passed directly into the function as an address rather than as a copy, similarly to pointers.
To define 2D array, number of rows and columns is specified in declaration statement.
int x[4][3]; indicates an array with 4 rows and 3 columns.
can also be initialized with declaration statement

Computations and Output


Homework

//Reads Temp from I2C temperature sensor and prints it on the serial port
//Include Wire I2C library
#include <Wire.h>
#include <LiquidCrystal.h>
int temp_address = 72; //1001000 written as decimal number
int btn=A0;
int tempMode=0;
int lastVal=0;

void setup()
{
  //Set up LCD number of column and rows
  lcd.begin(16,2);
  lcd.print("Day 13 Homework");
 //Start serial communication at 9600 baud
 Serial.begin(9600);
 //Create a Wire object
 Wire.begin();
}
void currentMode()
{
  int sensorVal=analogRead(btn);
  if(sensorVal==479 && lastVal!=1)
  {
    tempMode=1;
    lastVal=1;
  }
  if(sensorVal==0 && lastVal!=2)
  {
    tempMode=2;
    lastVal=2;
  }
}

void loop()
{
 //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0);
 //Get the temp and read it into a variable
 int c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 int f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.println("F");
 lcd.setCursor(0,1);
 currentMode();
 if(tempMode==1)
 {
  lcd.print(c);
 }
 if(tempMode==2)
 {
  lcd.print(f);
 }
 delay(500);
}

Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight const int red=11; const int green=10; const int blue=9; const int button=4; boolean lastbutton=LOW; boolean currentbutton=LOW; int ledMode=0; void setup() {   // put your setup code here, to run once:   pinMode(button,INPUT);   pinMode(red,OUTPUT);   pinMode(green,OUTPUT);   pinMode(blue,OUTPUT); } boolean debounce(boolean last) {   boolean current=digitalRead(button);   if(last!=current)   {     delay(5);     current=digitalRead(button);   }   return current; } void setMode(int mode) {   //RED   if(mode==1)   {     digitalWrite(red,HIGH);     digitalWrite(green,LOW);     digitalWrite(blue,LOW);   }   if(mode==2)   {     //orange     digitalWrite(red,HIGH);     analogWrite(green,40);     digitalWrite(blue,LOW);...