Skip to content
Snippets Groups Projects
Commit b992f9a2 authored by Karol Hennessy's avatar Karol Hennessy
Browse files

basic I/O

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