Hello Everyone out there,
currently i m doing a fun project where the Motors Pump and Valv release pressure inorder to make desired pressure inside a system. At initial not making this complex. I had defined as float UserinputmmHg = 200; outside the setup and loop and the programs runs to make the desired pressure inside the system. For this i use I2C for pressure sensor.
Everythings works as long as the userinputmmHg given or defined at initial before uploading to arduino. Now the Problem started as long as i tried to give the desired pressure from serial monitor. I also tried only the sensor reading portion (I2C) as a function. Still didnt work . Is it possible to make this work by asking the user at initial the pressure and after entering the desired pressure the system creat that pressure and ask for the next pressure via serial monitor. Here is my code so far. You opinion would be very much appriciated. Thanks in advance.
#include "Wire.h"
#include <Arduino.h>
#include <EEPROM.h>
//#include "Filter.h"
// ExponentialFilter ADCFilter(10, 0);
#define HSCDRRN400MD2A3_I2C 0x28 // each I2C object has a unique bus address, the DS1307 is 0x68
#define OUTPUT_MIN 1640 // 1638 counts (10% of 2^14 counts or 0x0666)
#define OUTPUT_MAX 14715 // 14745 counts (90% of 2^14 counts or 0x3999)
#define PRESSURE_MIN 0 //-344.738 // min is 0 for sensors that give absolute values
#define PRESSURE_MAX 450.036 // 75541921
float Autozero = 0.69;
float CorrectedPressure;
int ena2 = 2; // For Strong Motor
int ena3 = 3; // Für Fine Motor
float UserinputmmHg = 200;
float hysteresis = 0.4;
float Threshold = 2.0; // how much near the user input value you want to activate the weak motor
float RemainingPressure;
void setup()
{
Wire.begin(); // wake up I2C bus
delay(10);
// put your setup code here, to run once:
analogWrite(ena2, 180);
analogWrite(ena3, 200);
// pinMode(SensorInput, INPUT);
pinMode(8, OUTPUT); // PIN for Strong Motor
pinMode(7, OUTPUT); // PIN for Strong Valv
pinMode(6, OUTPUT); // PIN for Fine Motor
pinMode(9, OUTPUT); // PIN for Fine Valv
Serial.begin(19200);
}
void loop()
{
float Average = 0;
int sample = 16;
float pressure;
// send a request
Wire.beginTransmission(HSCDRRN400MD2A3_I2C); // "Hey, CN75 @ 0x48! Message for you"
Wire.write(1); // send a bit asking for register one, the data register (as specified by the pdf)
Wire.endTransmission(); // "Thanks, goodbye..."
// now get the data from the sensor
delay(10);
Wire.requestFrom(HSCDRRN400MD2A3_I2C, 4);
while (Wire.available() == 0)
;
byte a = Wire.read(); // first received byte stored here ....Example bytes one: 00011001 10000000
byte b = Wire.read(); // second received byte stored here ....Example bytes two: 11100111 00000000
byte c = Wire.read(); // third received byte stored here
byte d = Wire.read(); // fourth received byte stored here
byte status1 = (a & 0xc0) >> 6; // first 2 bits from first byte
int bridge_data = ((a & 0x3f) << 8) + b;
// ADCFilter.Filter(bridge_data);
pressure = 1.0 * (bridge_data - OUTPUT_MIN) * (PRESSURE_MAX - PRESSURE_MIN) / (OUTPUT_MAX - OUTPUT_MIN) + PRESSURE_MIN;
for (int i = 0; i < sample; ++i)
{
Average += pressure;
delay(1);
}
Average /= sample;
CorrectedPressure = Average - Autozero;
while (CorrectedPressure < 0)
{
CorrectedPressure = 0;
}
Serial.print("CorrectedPressure (mmHg) ");
Serial.println(CorrectedPressure, 1);
delay(1);
CorrectedPressure = pressure - Autozero;
RemainingPressure = UserinputmmHg - CorrectedPressure;
if ((UserinputmmHg - hysteresis) > CorrectedPressure)
{
if (RemainingPressure > Threshold)
{
digitalWrite(8, HIGH); // Strong Motor ON
digitalWrite(6, LOW); // Fine Motor OFF
digitalWrite(7, HIGH); // Strong Valv CLOSE
digitalWrite(9, HIGH); // Fine Valv CLOSE
Serial.print("GROB PUMPEN ");
delay(10);
}
else
{
digitalWrite(8, LOW); // Strong Motor OFF
digitalWrite(6, HIGH); // Fine Motor ON
digitalWrite(7, HIGH); // Strong Valv CLOSE
digitalWrite(9, HIGH); // Fine Valv CLOSE
Serial.print("FINE PUMPEN ");
delay(10);
}
}
else if ((UserinputmmHg + hysteresis) < CorrectedPressure)
{
if (abs(CorrectedPressure - UserinputmmHg) < Threshold)
{
digitalWrite(8, LOW); // Strong Motor OFF
digitalWrite(6, LOW); // Fine Motor OFF
digitalWrite(9, HIGH); // Strong Valv CLOSE
digitalWrite(7, LOW); // Fine Valv OPEN
Serial.print("Entlüftung Fine ");
}
else
{
digitalWrite(8, LOW); // Strong Motor OFF
digitalWrite(6, LOW); // Fine Motor OFF
digitalWrite(9, LOW); // Strong Valv OPEN
digitalWrite(7, HIGH); // Fine Valv CLOSE
Serial.print("Entlüftung Grob ");
}
}
else
{
digitalWrite(8, LOW); // Strong Motor OFF
digitalWrite(6, LOW); // Fine Motor OFF
digitalWrite(7, HIGH); // Strong Valv CLOSE
digitalWrite(9, HIGH); // Fine Valv CLOSE
Serial.print("TEIL FINAL ");
delay(10);
}
delay(100);
}