I'm trying to use an ATtiny85 to read a MPX5010DP Freescale Pressure Sensor, then send the reading back to an UNO via I2C. It's just an analog read of the sensor.
I've looked at a number of examples, and this is a far as I've gotten with it. I can read the pin, map the 0-1023 reading down to 0-255 and send it as a byte using the below pair of sketches.
On the UNO:
#include "Wire.h";
int selection=0;
int ATtinyAddress=0x26;
unsigned long marktime;
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
if (millis()>marktime)
{
Wire.requestFrom(ATtinyAddress,1);
while(Wire.available())
{
byte v = Wire.read();
Serial.println(v);
}
marktime+=1000;
}
}
on the ATtiny85
#include "TinyWireS.h"
#define I2C_SLAVE_ADDR 0x26
#define Sensor_PIN 3
void setup(){
pinMode(Sensor_PIN,INPUT);
TinyWireS.begin(I2C_SLAVE_ADDR);
TinyWireS.onRequest(requestEvent);
}
void loop(){
}
void requestEvent()
{
int pressure=analogRead(Sensor_PIN);
byte pbyte=map(pressure,0,1023,0,255);
TinyWireS.send(pbyte);
}
I'd like the full resolution of the Tiny, though. I know I can use lowByte & highByte to get a pair of bytes, and I know I can word() the two back together once I have them on the UNO.
I'm lost at the part of requesting and receiving two bytes, instead of one, and assigning them usable values on the UNO side.
robtillaart:
its here - Arduino Playground - HomePage -
check requestFrom(address, count);
Thanks for pointing me in that direction. Between that, and plagerizing other code here from the forum (http://forum.arduino.cc/index.php/topic,160680.0.html), I now have a pair of sketches that work, at least. Despite it's function, I'm not fond of this code on the slave.
First, here's the master code so you can see it.
#include "Wire.h";
int ATtinyAddress=0x26;
unsigned long marktime;
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop(){
if (millis()>marktime)
{
byte hb;
byte lb;
Wire.requestFrom(ATtinyAddress,2);
if (Wire.available())
{
lb=Wire.read();
hb=Wire.read();
}
int pressure=word(hb,lb);
Serial.println (pressure);
marktime+=1000;
}
}
Here's the slave code that works.
#include "TinyWireS.h"
#define I2C_SLAVE_ADDR 0x26
#define Sensor_PIN 3
boolean firstbyte = true;
void setup(){
pinMode(Sensor_PIN,INPUT);
TinyWireS.begin(I2C_SLAVE_ADDR);
TinyWireS.onRequest(requestEvent);
}
void loop(){
}
void requestEvent()
{
int pressure=analogRead(Sensor_PIN);
byte plow;
byte phi;
if(firstbyte == true)
{ // on the first byte we do the math
plow=lowByte(pressure);
firstbyte = false; //so next time though we send the next byte
TinyWireS.send(plow);
}
else
{
phi=highByte(pressure);
TinyWireS.send(phi);
firstbyte = true;
}
}
I don't understand why this code doesn't work. What purpose does the boolean firstbyte serve in the above sketch?
void requestEvent()
{
int pressure=analogRead(Sensor_PIN);
byte plow;
byte phi;
plow=lowByte(pressure);
phi=highByte(pressure);
TinyWireS.send(plow);
TinyWireS.send(phi);
}
And, in contrast - this works perfectly if I use a Nano as a slave instead of a tiny.
#include "Wire.h"
#define I2C_SLAVE_ADDR 0x26
#define Sensor_PIN 3
int pressure;
void setup(){
pinMode(Sensor_PIN,INPUT);
Wire.begin(I2C_SLAVE_ADDR);
Wire.onRequest(requestEvent);
}
void loop()
{
}
void requestEvent()
{
pressure=analogRead(Sensor_PIN);
byte plow=lowByte(pressure);
byte phi=highByte(pressure);
byte data[]={plow, phi};
Wire.write(data, 2);
}