diff --git a/arduino/basic_io/src/johan.cpp b/arduino/basic_io/src/johan.cpp index ed098cffe187dcdae3e467e754757a85f9ef98bc..938a384f52e3abbaf03c1d6ff9c10ecf21fb15b8 100644 --- a/arduino/basic_io/src/johan.cpp +++ b/arduino/basic_io/src/johan.cpp @@ -4,21 +4,21 @@ #include "Adafruit_MCP9808.h" #include <INA.h> - #define DEBUG +#define DEBUG #ifdef DEBUG - #define DEBUG_SERIAL(x) Serial.begin (x) - #define DEBUG_PRINT(x) Serial.print (x) - #define DEBUG_PRINTLN(x) Serial.println (x) - #define DEBUG_PRINT2(x,y) Serial.print (x,y) - #define DEBUG_PRINTLN2(x,y) Serial.println (x,y) - +#define DEBUG_SERIAL(x) Serial.begin (x) +#define DEBUG_PRINT(x) Serial.print (x) +#define DEBUG_PRINTLN(x) Serial.println (x) +#define DEBUG_PRINT2(x,y) Serial.print (x,y) +#define DEBUG_PRINTLN2(x,y) Serial.println (x,y) + #else - #define DEBUG_SERIAL(x) - #define DEBUG_PRINT(x) - #define DEBUG_PRINTLN(x) - #define DEBUG_PRINT2(x,y) - #define DEBUG_PRINTLN2(x,y) +#define DEBUG_SERIAL(x) +#define DEBUG_PRINT(x) +#define DEBUG_PRINTLN(x) +#define DEBUG_PRINT2(x,y) +#define DEBUG_PRINTLN2(x,y) #endif @@ -64,167 +64,167 @@ const int p_o2_regulated = 14; // uint8_t devicesFound = 0; ///< Number of INAs found - INA_Class INA; - +INA_Class INA; + TwoWire I2CMCP9808 = TwoWire(0); void setup() { - Serial.begin(9600); - //Wire.begin(SDA,SCL); - Wire.begin(22,23); - I2CMCP9808.begin(22,23); - -if (!tempsensor.begin(0x18, &I2CMCP9808)) { - Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct."); - while (1); - } - - tempsensor.setResolution(3); - - - - // NodeMCU32 - // pwm - ledcSetup(0, freq, 8); - - ledcSetup(1,2200,8); // buzzer frequency - - //ledcAttachPin(pin_valve_o2_in , 0); - ledcAttachPin(pin_valve_inhale , 0); - - ledcAttachPin(buzzer , 1); - - ledcWrite(1, 128); //buzzer - - pinMode(A13, INPUT); // potentiometer - - // pressure sensors IO config - - pinMode(p_patient, INPUT); - pinMode(p_buffer, INPUT); - pinMode(p_air_supply, INPUT); - pinMode(p_air_regulated, INPUT); - pinMode(p_inhale, INPUT); - pinMode(p_o2_supply, INPUT); - pinMode(p_o2_regulated, INPUT); - - -// Valves IO config - - pinMode(pin_valve_air_in,OUTPUT); - pinMode(pin_valve_exhale,OUTPUT); - pinMode(pin_valve_purge,OUTPUT); - pinMode(pin_valve_o2_in,OUTPUT); - -// LEDs IO config - - pinMode(redled,OUTPUT); - pinMode(yellowled,OUTPUT); - pinMode(greenled,OUTPUT); - - - digitalWrite(pin_valve_air_in, HIGH); - digitalWrite(pin_valve_exhale, LOW); - digitalWrite(pin_valve_purge, LOW); - digitalWrite(pin_valve_o2_in, HIGH); - - digitalWrite(redled, HIGH); - digitalWrite(yellowled, HIGH); - digitalWrite(greenled, HIGH); - - DEBUG_PRINT("Hello"); -Serial.println("Hello"); - - - -// current sensor configuration - - devicesFound = INA.begin(1,500000); // Set to an expected 1 Amp maximum and a 100000 microOhm resistor - while (INA.begin(1, 500000) == 0) - { - DEBUG_PRINTLN("No INA device found, retrying in 10s..."); - delay(1000); // Wait 10 seconds before retrying - } // while no devices detected - DEBUG_PRINT(" - Detected "); - DEBUG_PRINT(devicesFound); - DEBUG_PRINTLN(" INA devices on the I2C bus"); - INA.setBusConversion(8500); // Maximum conversion time 8.244ms - INA.setShuntConversion(8500); // Maximum conversion time 8.244ms - INA.setAveraging(128); // Average each reading n-times - INA.setMode(INA_MODE_CONTINUOUS_BOTH); // Bus/shunt measured continuously - INA.AlertOnBusOverVoltage(true,5000); // Trigger alert if over 5V on bus - - - - + Serial.begin(9600); + //Wire.begin(SDA,SCL); + Wire.begin(22,23); + I2CMCP9808.begin(22,23); + + if (!tempsensor.begin(0x18, &I2CMCP9808)) { + Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct."); + while (1); + } + + tempsensor.setResolution(3); + + + + // NodeMCU32 + // pwm + ledcSetup(0, freq, 8); + + ledcSetup(1,2200,8); // buzzer frequency + + //ledcAttachPin(pin_valve_o2_in , 0); + ledcAttachPin(pin_valve_inhale , 0); + + ledcAttachPin(buzzer , 1); + + ledcWrite(1, 128); //buzzer + + pinMode(A13, INPUT); // potentiometer + + // pressure sensors IO config + + pinMode(p_patient, INPUT); + pinMode(p_buffer, INPUT); + pinMode(p_air_supply, INPUT); + pinMode(p_air_regulated, INPUT); + pinMode(p_inhale, INPUT); + pinMode(p_o2_supply, INPUT); + pinMode(p_o2_regulated, INPUT); + + + // Valves IO config + + pinMode(pin_valve_air_in,OUTPUT); + pinMode(pin_valve_exhale,OUTPUT); + pinMode(pin_valve_purge,OUTPUT); + pinMode(pin_valve_o2_in,OUTPUT); + + // LEDs IO config + + pinMode(redled,OUTPUT); + pinMode(yellowled,OUTPUT); + pinMode(greenled,OUTPUT); + + + digitalWrite(pin_valve_air_in, HIGH); + digitalWrite(pin_valve_exhale, LOW); + digitalWrite(pin_valve_purge, LOW); + digitalWrite(pin_valve_o2_in, HIGH); + + digitalWrite(redled, HIGH); + digitalWrite(yellowled, HIGH); + digitalWrite(greenled, HIGH); + + DEBUG_PRINT("Hello"); + Serial.println("Hello"); + + + + // current sensor configuration + + devicesFound = INA.begin(1,500000); // Set to an expected 1 Amp maximum and a 100000 microOhm resistor + while (INA.begin(1, 500000) == 0) + { + DEBUG_PRINTLN("No INA device found, retrying in 10s..."); + delay(1000); // Wait 10 seconds before retrying + } // while no devices detected + DEBUG_PRINT(" - Detected "); + DEBUG_PRINT(devicesFound); + DEBUG_PRINTLN(" INA devices on the I2C bus"); + INA.setBusConversion(8500); // Maximum conversion time 8.244ms + INA.setShuntConversion(8500); // Maximum conversion time 8.244ms + INA.setAveraging(128); // Average each reading n-times + INA.setMode(INA_MODE_CONTINUOUS_BOTH); // Bus/shunt measured continuously + INA.AlertOnBusOverVoltage(true,5000); // Trigger alert if over 5V on bus + + + + } void loop() { - - - float res = analogRead(A13); //12 bit ADC on esp32 - float pressure_patient = analogRead(p_patient); //12 bit ADC on esp32 - float pressure_buffer = analogRead(p_buffer); //12 bit ADC on esp32 -float pressure_asupply = analogRead(p_air_supply); //12 bit ADC on esp32 -float pressure_aregulated = analogRead(p_air_regulated); //12 bit ADC on esp32 -float pressure_inhale = analogRead(p_inhale); //12 bit ADC on esp32 -float pressure_o2supply = analogRead(p_o2_supply); //12 bit ADC on esp32 -float pressure_o2regulated = analogRead(p_o2_regulated); //12 bit ADC on esp32 - - -tempsensor.wake(); //sensor on -float c = tempsensor.readTempC(); - -tempsensor.shutdown_wake(1); //sensor off - - duty_cycle = res/4096.0; - - if (duty_cycle > max_duty_cycle){ - duty_cycle = max_duty_cycle; - } - - int val = (int)(255.0*duty_cycle); - - Serial.print("Pot "); - Serial.print(String((int)res)); - Serial.print(" p_patient "); - Serial.print(String((int)pressure_patient)); - Serial.print(" p_buffer "); - Serial.print(String((int)pressure_buffer)); - Serial.print(" p_as "); - Serial.print(String((int)pressure_asupply)); - Serial.print(" p_ar "); - Serial.print(String((int)pressure_aregulated)); - Serial.print(" p_inhale "); - Serial.print(String((int)pressure_inhale)); - Serial.print(" p_o2s "); - Serial.print(String((int)pressure_o2supply)); - Serial.print(" p_o2r "); - Serial.print(String((int)pressure_o2regulated)); - -Serial.print(" Temp: "); - Serial.print(c, 4); - - -// measuring Valves voltage and current - Serial.print(" "); - Serial.print((float)INA.getBusMilliVolts(0)/1000.0,4); - DEBUG_PRINT("V "); - DEBUG_PRINT2((float)INA.getShuntMicroVolts(0)/5,0); - DEBUG_PRINT("mA "); - DEBUG_PRINT2((float)INA.getShuntMicroVolts(1)/5,0); - DEBUG_PRINT("mA "); -DEBUG_PRINT2((float)INA.getShuntMicroVolts(2)/5,0); - DEBUG_PRINT("mA "); - DEBUG_PRINT2((float)INA.getShuntMicroVolts(3)/5,0); - DEBUG_PRINT("mA "); - - Serial.print(" duty cycle "); - Serial.println(String(duty_cycle)); - - ledcWrite(0, val); - - delay(1000); -} \ No newline at end of file + + + float res = analogRead(A13); //12 bit ADC on esp32 + float pressure_patient = analogRead(p_patient); //12 bit ADC on esp32 + float pressure_buffer = analogRead(p_buffer); //12 bit ADC on esp32 + float pressure_asupply = analogRead(p_air_supply); //12 bit ADC on esp32 + float pressure_aregulated = analogRead(p_air_regulated); //12 bit ADC on esp32 + float pressure_inhale = analogRead(p_inhale); //12 bit ADC on esp32 + float pressure_o2supply = analogRead(p_o2_supply); //12 bit ADC on esp32 + float pressure_o2regulated = analogRead(p_o2_regulated); //12 bit ADC on esp32 + + + tempsensor.wake(); //sensor on + float c = tempsensor.readTempC(); + + tempsensor.shutdown_wake(1); //sensor off + + duty_cycle = res/4096.0; + + if (duty_cycle > max_duty_cycle){ + duty_cycle = max_duty_cycle; + } + + int val = (int)(255.0*duty_cycle); + + Serial.print("Pot "); + Serial.print(String((int)res)); + Serial.print(" p_patient "); + Serial.print(String((int)pressure_patient)); + Serial.print(" p_buffer "); + Serial.print(String((int)pressure_buffer)); + Serial.print(" p_as "); + Serial.print(String((int)pressure_asupply)); + Serial.print(" p_ar "); + Serial.print(String((int)pressure_aregulated)); + Serial.print(" p_inhale "); + Serial.print(String((int)pressure_inhale)); + Serial.print(" p_o2s "); + Serial.print(String((int)pressure_o2supply)); + Serial.print(" p_o2r "); + Serial.print(String((int)pressure_o2regulated)); + + Serial.print(" Temp: "); + Serial.print(c, 4); + + + // measuring Valves voltage and current + Serial.print(" "); + Serial.print((float)INA.getBusMilliVolts(0)/1000.0,4); + DEBUG_PRINT("V "); + DEBUG_PRINT2((float)INA.getShuntMicroVolts(0)/5,0); + DEBUG_PRINT("mA "); + DEBUG_PRINT2((float)INA.getShuntMicroVolts(1)/5,0); + DEBUG_PRINT("mA "); + DEBUG_PRINT2((float)INA.getShuntMicroVolts(2)/5,0); + DEBUG_PRINT("mA "); + DEBUG_PRINT2((float)INA.getShuntMicroVolts(3)/5,0); + DEBUG_PRINT("mA "); + + Serial.print(" duty cycle "); + Serial.println(String(duty_cycle)); + + ledcWrite(0, val); + + delay(1000); +}