I have a basic question. I have received 2 bytes of data from I2C barometric sensor using this code:
#include<Wire.h>
#define sensor 0x28 //Unique bus address
void setup()
{
Wire.begin();//Wakes up I2C bus
Serial.begin(9600);
}
void getdata(byte *a, byte *b)
{
//Move register pointer back to first register
//Wire.beginTransmission(sensor);
//Wire.write(1);
//Wire.endTransmission();
Wire.requestFrom(sensor,2);//Sends content of first two registers
*a = Wire.read(); //first byte recieved stored here
*b = Wire.read(); //second byte recieved stored here
}
void showdata()
{
byte aa,bb;
float pressure =0;
getdata(&aa,&bb);
Serial.print("byte 1: ");Serial.println(aa,DEC);
Serial.print("byte 2 ");Serial.println(bb,DEC);
delay(1000);
}
void loop()
{
showdata();
}
I want to use readings not just for serial monitor, but for controlling the device. For example, if the first byte integer is less than 63, turn on digital pin 6.
I understand that it is a very basic question, but I really need a help with usage of those two bytes for controlling the process.
You obviously don't have experience with the Arduino platform. I suggest you start with easier project to get familiar with the platform and learn programming in this adapted C++ dialect. Try some examples that the IDE offers and extend them to make your own experience. Learn from the example sketches how variables are read and manipulated. If you then look at your above problem you'll see that it's quite easy and you've done it yourself.
Thank you for your reply. Yes, I am obviously don’t have experience. But I am here to get help. If you are experienced, is it really difficult for you to write a couple of lines of the code? I have requested a simple answer (for you): if the first byte equals 63 (maximum value in my case), make digital pin 6 high. How the code will look like? I am confused with this variables, pointers*, etc.
make for me? My arduino trinket does not have even a serial monitor. But I have to use readings from the sensor to drive digital output (to make it high when a certain pressure level is achieved).
#include <Wire.h>
void setup()
{
Wire.begin();
pinMode(6, OUTPUT); //digital pin-6 works as output
Wire.beginTransmission(0x28); //0x28 is the Sensor (device) address
Wire.write(0x01); //base address of the address counter of Sensor
Wire.endTransmission(); //queues data are transmitted
do
{
Wire.requestFrom(0x28, 2); //request to Sensor to send 2 byte data starting from location 0x01
byte x = Wire.read(); //1st byte which is 63 as you have said
}
while ( x != 63); //keep sending request until the 1st byte is 63
digitalWrite(6, HIGH); //1st byte is 63; assert Logic High at digital pin-6
............................ //do other things
}
void loop()
{
}
Oleksiy79:
I have requested a simple answer (for you): if the first byte equals 63 (maximum value in my case), make digital pin 6 high. How the code will look like?
Yes, it is easy for me. If you want me to just write it for you then I am for hire. How much are you willing to pay me to write your code for you?
If you want me to help you learn then I can do that for free. But it doesn't involve me writing code for you when your problem is obviously a conceptual one.
Oleksiy79:
What the code
getdata(&aa,&bb);
make for me?
It calls that getdata function that is defined here in your code:
void getdata(byte *a, byte *b)
{
//Move register pointer back to first register
//Wire.beginTransmission(sensor);
//Wire.write(1);
//Wire.endTransmission();
Wire.requestFrom(sensor,2);//Sends content of first two registers
*a = Wire.read(); //first byte recieved stored here
*b = Wire.read(); //second byte recieved stored here
}
passing aa as the a parameter and bb as the b. At the end of this function it puts the two bytes you're interested in into the aa and bb variables. So now you have them in variables. You printed them in the code you have. You don't have to print them. You can do whatever you want, including use them in an if statement.
GolamMostafa:
Simplified version of your codes (untested):
GolamMostafa, thank you for your code. I've got an error saying 'x' was not declared in this scope. I have added the line
int x
after the line
Wire.begin();
and obtained another error during the uploading:
expected initializer before 'void'. I think, it has something to do with the fact that monitoring of the first byte takes place in the setup, not in the loop. Am I correct?
You may consult the attached file (a working program for BMP180 Temperature sensor using Wire.h Library only) to tailor your program that is dealing with Barometric Pressure. Which sensor (BMP180 or BM280) are you using?
BTW: Do you know the meaning of 'data type'? It refers to storage class, data size, and scope. The error in my codes of the stated post is related with scope property. So, declare x before the do loop. In programming, syntax/semantic errors do occur, and these are removed with patience.
You may consult the attached file (a working program for BMP180 Temperature sensor using Wire.h Library only) to tailor your program that is dealing with Barometric Pressure. Which sensor (BMP180 or BM280) are you using?
BTW: Do you know the meaning of 'data type'? It refers to storage class, data size, and scope. The error in my codes of the stated post is related with scope property. So, declare x before the do loop. In programming, syntax/semantic errors do occur, and these are removed with patience.
I use ABPDLNN100MG2A3 Honeywell sensor. And I took a code from here:
which also works for me. The code shows two first bytes at the serial monitor. Thanks to you and Delta, I have learned how to make operations with this aa variable, but I am still blind because of the absence of the serial monitor. Can you also answer what data type is aa if I use it directly in the code? Shouldn't I convert it first to decimal before telling the board to turn on the pin? Since I don't know what is going on, my board does not react to this level 63.
You may consult this write up to learn a bit about various data types.
Once you get some idea on data types, you will be able to figure out the relationship between the following two variables which are present in your Original Post (OP).