Wire.h conflict with VirtualWire, SoftwareSerial and RH_ASK.h

First of all, thank you for your time reading this.
It is realy appreciated!

I measure the PH for my aquarium with follwing board and arduino nano:

The sketch provided by Reza works fine on its own but as soon as one of the libraries mentioned in the titel is also used the PH output goes to "inf"
Main code provided by Reza:

#include <Wire.h>

#define PHADDRESS 0x4D
int RoomTempI2CAddress = B1001011;


float volt4 = 0.95;
float volt7 = 0.67;
float calibrationTempC = 20;

void setup()
{
}

void loop()
{
  Wire.begin(); //connects I2C
  Serial.begin(9600);
  Serial.print("avgMeasuredPH-");
  SetRoomTemperataureResolutionBits(12);//12 bits room temp resolution in celcius
  
  int sampleSize = 500;
  
  double avgMeasuredPH = 0;
  double avgRoomTempC = 0;
  double avgPHVolts = 0;
  double avgRoomTemperatureCompensatedMeasuredPH = 0;
  
  double tempAdjusted4;
  
  int x;
  for(x=0;x< sampleSize;x++)
  {
  
  double phVolt = getPHVolts();    
  tempAdjusted4 = adjustPHBasedOnTemp(4,calibrationTempC);
  double voltsPerPH = (abs(volt7-volt4)) / (7-tempAdjusted4);
  
  double realPHVolt = (volt7 - phVolt);
  double phUnits = realPHVolt / voltsPerPH;
  double measuredPH = 7 + phUnits;

  double roomTempC =  getRoomTemperatureC();
  double roomTempCompensatedMeasuredPH = adjustPHBasedOnTemp(measuredPH,roomTempC); 
  
  avgMeasuredPH+=measuredPH;
  avgRoomTemperatureCompensatedMeasuredPH+=roomTempCompensatedMeasuredPH;
  avgRoomTempC+=roomTempC;
  avgPHVolts += phVolt;
    
  
  }
  
  avgMeasuredPH/=sampleSize;
  avgRoomTemperatureCompensatedMeasuredPH/=sampleSize;
  avgRoomTempC/=sampleSize;
  avgPHVolts/=sampleSize;
  
  Serial.print("avgMeasuredPH-");
  Serial.print(avgMeasuredPH,4);
  Serial.print(" roomTempCompensatedPH-");
  Serial.print(avgRoomTemperatureCompensatedMeasuredPH,4);
  Serial.print(" avgRoomtTempC-");
  Serial.print(avgRoomTempC,4);
  Serial.print(" avgPhVolts-");
  Serial.print(avgPHVolts,4);
  
  Serial.print(" 7CalVolts-");
  Serial.print(volt7,4);
  Serial.print(" 4CalVolts-");
  Serial.print(volt4,4);    
  Serial.print(" 4CalTempAdjusted-");
  Serial.println(tempAdjusted4,4);


  delay(1000);  
  
  
  
}


float adjustPHBasedOnTemp(float PH, float temp)
{
   // http://www.omega.com/Green/pdf/pHbasics_REF.pdf
   // When the temperature is other than 25degC and the ph is other than 7
   // the temperature error is 0.03ph error/ph unit/10degC
   // which means error = 0.03*(ph away from 7)*(tempdiffC/10)
   
    float phDifference = abs(PH-7);
    float tempDifferenceC = abs(temp-25);
    float phAdjust = (0.03*phDifference)*(tempDifferenceC/10);
    
    if(PH>7 && temp<25)
      phAdjust=phAdjust;

    if(PH>7 && temp>25)
      phAdjust=phAdjust*-1;
 
    if(PH<7 && temp>25)
      phAdjust=phAdjust;
 
    if(PH<7 && temp<25)
      phAdjust=phAdjust*-1;
 
    float tempAdjustedPH = PH + phAdjust;
    return tempAdjustedPH;
}



double getPHVolts()
{
  byte ad_high;
  byte ad_low;
  
  Wire.requestFrom(PHADDRESS, 2);        //requests 2 bytes
  while(Wire.available() < 2);         //while two bytes to receive
    
  ad_high = Wire.read();           
  ad_low = Wire.read();
  double units = (ad_high * 256) + ad_low;
  
  double volts =  (units /4096)*3; 
  return volts;  
}

 
double getRoomTemperatureC()
{
  Wire.requestFrom(RoomTempI2CAddress,2);
  byte MSB = Wire.read();
  byte LSB = Wire.read();
 
  int TemperatureSum = ((MSB << 8) | LSB) >> 4;
  double celsius = TemperatureSum*0.0625;
  
  return celsius;
}
 
void SetRoomTemperataureResolutionBits(int ResolutionBits)
{
  if (ResolutionBits < 9 || ResolutionBits > 12) exit;
  Wire.beginTransmission(RoomTempI2CAddress);
  Wire.write(B00000001); //addresses the configuration register
  Wire.write((ResolutionBits-9) << 5); //writes the resolution bits
  Wire.endTransmission();
 
  Wire.beginTransmission(RoomTempI2CAddress); //resets to reading the temperature
  Wire.write((byte)0x00);
  Wire.endTransmission();
}

In the past I tried to include SoftwareSerial (for some reason I can't remember) but skipped it since the sketch above stopped working.

This time I am trying to use the VirtualWire library (RF 433) on the same arduino but again the same issue I had before.
Tried the radiohead library (ask) as an alternative but same issue:
http://www.airspayce.com/mikem/arduino/RadioHead/index.html

Serial output stand alone:

avgMeasuredPH-avgMeasuredPH-6.9461 roomTempCompensatedPH-6.9465 avgRoomtTempC-27.7500 avgPhVolts-0.6750 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550

Serial output when using once of the libraries:

avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-27.8981 avgPhVolts-0.6755 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550

I tried and tried but my skill alre lacking to solve this one.
Reza did not reply so I try my luck here.
Thank you in advance.

as soon as one of the libraries mentioned in the titel is also used

Used for what?

The Wire library should not conflict with any of those other packages.

VirtualWire and/or radiohead for RF433 communication.
This second arduino activates a solenoid in the CO2 supply to the aquarium and thus lowering the PH.
Seperate code works fine. Together RF433 still functions but PH gives "inf"

Softserial I can't remember since that was a year ago but remebered it gave the same issue so thought it might be relevant. The more info the better right.

Can someone help me out?
My project is totaly on hold and can't find a way out
Thanks in advance.

To help out I added an example belowshows that there is a conflict somewhere:

Serial from orginal code provided from Reza

avgMeasuredPH-avgMeasuredPH-6.8364 roomTempCompensatedPH-6.8379 avgRoomtTempC-27.9375 avgPhVolts-0.6850 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-6.8363 roomTempCompensatedPH-6.8377 avgRoomtTempC-27.9375 avgPhVolts-0.6851 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-6.8361 roomTempCompensatedPH-6.8376 avgRoomtTempC-27.9375 avgPhVolts-0.6851 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-6.8364 roomTempCompensatedPH-6.8379 avgRoomtTempC-27.9375 avgPhVolts-0.6850 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-6.8363 roomTempCompensatedPH-6.8378 avgRoomtTempC-27.9375 avgPhVolts-0.6851 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-6.8360 roomTempCompensatedPH-6.8374 avgRoomtTempC-27.9375 avgPhVolts-0.6851 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550

Code with standard virtualwire code parsed in from:
https://www.pjrc.com/teensy/td_libs_VirtualWire.html

#include <Wire.h>
#include <VirtualWire.h>

// PH
#define PHADDRESS 0x4D
int RoomTempI2CAddress = B1001011;


float volt4 = 0.95;
float volt7 = 0.67;
float calibrationTempC = 20;


//VIRTUAL WIRE
const int led_pin = 13;
const int transmit_pin = 12;
const int receive_pin = 2;
const int transmit_en_pin = 3;



void setup()
{
  //VIRUTAL WIRE
  // Initialise the IO and ISR
  vw_set_tx_pin(transmit_pin);
  vw_set_rx_pin(receive_pin);
  vw_set_ptt_pin(transmit_en_pin);
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);   // Bits per sec
}

byte count = 1;

void loop()
{
  //VIRTUAL WIRE
 char msg[7] = {'h','e','l','l','o',' ','#'};

  msg[6] = count;
  digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
  vw_send((uint8_t *)msg, 7);
  vw_wait_tx(); // Wait until the whole message is gone
  digitalWrite(led_pin, LOW);
  delay(1000);
  count = count + 1;


  //PH
  Wire.begin(); //connects I2C
  Serial.begin(9600);
  Serial.print("avgMeasuredPH-");
  SetRoomTemperataureResolutionBits(12);//12 bits room temp resolution in celcius

  int sampleSize = 500;

  double avgMeasuredPH = 0;
  double avgRoomTempC = 0;
  double avgPHVolts = 0;
  double avgRoomTemperatureCompensatedMeasuredPH = 0;

  double tempAdjusted4;

  int x;
  for (x = 0; x < sampleSize; x++)
  {

    double phVolt = getPHVolts();
    tempAdjusted4 = adjustPHBasedOnTemp(4, calibrationTempC);
    double voltsPerPH = (abs(volt7 - volt4)) / (7 - tempAdjusted4);

    double realPHVolt = (volt7 - phVolt);
    double phUnits = realPHVolt / voltsPerPH;
    double measuredPH = 7 + phUnits;

    double roomTempC =  getRoomTemperatureC();
    double roomTempCompensatedMeasuredPH = adjustPHBasedOnTemp(measuredPH, roomTempC);

    avgMeasuredPH += measuredPH;
    avgRoomTemperatureCompensatedMeasuredPH += roomTempCompensatedMeasuredPH;
    avgRoomTempC += roomTempC;
    avgPHVolts += phVolt;


  }

  avgMeasuredPH /= sampleSize;
  avgRoomTemperatureCompensatedMeasuredPH /= sampleSize;
  avgRoomTempC /= sampleSize;
  avgPHVolts /= sampleSize;

  Serial.print("avgMeasuredPH-");
  Serial.print(avgMeasuredPH, 4);
  Serial.print(" roomTempCompensatedPH-");
  Serial.print(avgRoomTemperatureCompensatedMeasuredPH, 4);
  Serial.print(" avgRoomtTempC-");
  Serial.print(avgRoomTempC, 4);
  Serial.print(" avgPhVolts-");
  Serial.print(avgPHVolts, 4);

  Serial.print(" 7CalVolts-");
  Serial.print(volt7, 4);
  Serial.print(" 4CalVolts-");
  Serial.print(volt4, 4);
  Serial.print(" 4CalTempAdjusted-");
  Serial.println(tempAdjusted4, 4);


  delay(1000);



}


float adjustPHBasedOnTemp(float PH, float temp)
{
  // http://www.omega.com/Green/pdf/pHbasics_REF.pdf
  // When the temperature is other than 25degC and the ph is other than 7
  // the temperature error is 0.03ph error/ph unit/10degC
  // which means error = 0.03*(ph away from 7)*(tempdiffC/10)

  float phDifference = abs(PH - 7);
  float tempDifferenceC = abs(temp - 25);
  float phAdjust = (0.03 * phDifference) * (tempDifferenceC / 10);

  if (PH > 7 && temp < 25)
    phAdjust = phAdjust;

  if (PH > 7 && temp > 25)
    phAdjust = phAdjust * -1;

  if (PH < 7 && temp > 25)
    phAdjust = phAdjust;

  if (PH < 7 && temp < 25)
    phAdjust = phAdjust * -1;

  float tempAdjustedPH = PH + phAdjust;
  return tempAdjustedPH;
}



double getPHVolts()
{
  byte ad_high;
  byte ad_low;

  Wire.requestFrom(PHADDRESS, 2);        //requests 2 bytes
  while (Wire.available() < 2);        //while two bytes to receive

  ad_high = Wire.read();
  ad_low = Wire.read();
  double units = (ad_high * 256) + ad_low;

  double volts =  (units / 4096) * 3;
  return volts;
}


double getRoomTemperatureC()
{
  Wire.requestFrom(RoomTempI2CAddress, 2);
  byte MSB = Wire.read();
  byte LSB = Wire.read();

  int TemperatureSum = ((MSB << 8) | LSB) >> 4;
  double celsius = TemperatureSum * 0.0625;

  return celsius;
}

void SetRoomTemperataureResolutionBits(int ResolutionBits)
{
  if (ResolutionBits < 9 || ResolutionBits > 12) exit;
  Wire.beginTransmission(RoomTempI2CAddress);
  Wire.write(B00000001); //addresses the configuration register
  Wire.write((ResolutionBits - 9) << 5); //writes the resolution bits
  Wire.endTransmission();

  Wire.beginTransmission(RoomTempI2CAddress); //resets to reading the temperature
  Wire.write((byte)0x00);
  Wire.endTransmission();
}

Serial output from code above:

avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6850 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6850 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6849 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6849 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6845 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6844 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6843 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-28.1250 avgPhVolts-0.6843 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550

Receive end of of virtual wire is fine so no issues there.
Thank you for you time

Did I not supply enough information for anyone the help out?

It is unlikely that anyone on this forum has your combination of hardware, so the code can't be tested by us.

You need to figure out why you are getting "inf" as a result.

Place print statements throughout the code to determine which variables do not have the values you expect.

jremington:
It is unlikely that anyone on this forum has your combination of hardware, so the code can't be tested by us.

You need to figure out why you are getting "inf" as a result.

Place print statements throughout the code to determine which variables do not have the values you expect.

I understand what you mean and I tried but no luck.
My skills are just lacking.

As soon as I add #include <VirtualWire.h> i get the "inf" as a result.
I was hoping someone could just tell by knowledge of the libraries.

jremington:
It is unlikely that anyone on this forum has your combination of hardware, so the code can't be tested by us.

You need to figure out why you are getting "inf" as a result.

Place print statements throughout the code to determine which variables do not have the values you expect.

Followed you advice and after a lot of trial and error found the issue.
function: abs()

I changed:

double voltsPerPH = (volt7 - volt4) / (7 - tempAdjusted4);

To:

double voltsPerPH = (abs(volt7 - volt4)) / (7 - tempAdjusted4);

This fixed it but the abs() function was there for a reason but even after reading up on it I don't know how to work around it

A pH of -6.9 is pretty damned acidic! That should have been a tipoff.

For some reason he set up serial printing that way. "-" instead of ":"
No idea why you would do something like that.

-PH is kind of extreme.

Ah its getting late here so I'll keep it short.
Have been playing around with the RH_ASK library on an attiny84 so included the software serial library to output debugging data. Not got round to tracing down why, but when the software serial library is included the RH_ASK instance outputs its TX on the software serials TX pin and when waitPacketSent is called will hang forever.

As a guess the RH_ASK library is using the USART hardware itself so when software serial is included there's some conflict.... somewhere...

Anyway know its not much help to you but thanks for the info, should help me with my own issues (and maybe someone else)

Also virtual wire is a little out of date but still works perfectly fine. But if you've got atmega based arduinos both sides the little bit more ram/progmem used by the RH_ASK library shouldn't be an issue.

Edit: HAH yes, well have rx/tx working now.... will look through your code tomorrow to see if i can see anything wrong...
oh and side note, my tx Imgur: The magic of the Internet (now working... ish)

Ok, re-reading the thread i cant quite tell if the added/removed abs() call fixed it :S

The INF output from what my googling suggested is a divide by zero issue cant really say more about it than that. The use of abs on a double (in theory) shouldn't change the output. Some quick testing suggests it would change the number of decimal places? abs(1.23456789)=1.23 or abs(1.1)=1.10 tested on my nano

Also would very much agree with jremington, MORE PRINT STATMENTS! if something is going wrong will help you find out where.

Some quick testing suggests it would change the number of decimal places? abs(1.23456789)=1.23 or abs(1.1)=1.10 tested on my nano

"print()" defaults to 2 decimal places.

Thanks for clearing that up, guess that's why the code uses print(var,4). Didn't imagine it would round values but I've never printed a float with more than 2 decimal places.