add comments

This commit is contained in:
2023-10-12 22:15:24 +01:00
parent eb97dc0f60
commit a602f20e01
4 changed files with 50 additions and 31 deletions

View File

@@ -286,6 +286,7 @@ enum states get_current_state()
// also speed impact probably doesn't matter much since setup() runs once per boot // also speed impact probably doesn't matter much since setup() runs once per boot
void initialiseEncoderStateMachine() void initialiseEncoderStateMachine()
{ {
// gets current state (initial) state and sets it
state = get_current_state(); state = get_current_state();
} }
@@ -294,11 +295,12 @@ void updateEncoderStateMachine()
{ {
enum states new_state = get_current_state(); // get new state, don't update state var yet so states can be compared enum states new_state = get_current_state(); // get new state, don't update state var yet so states can be compared
// check new state is valid, coming from state1, increment error count if not
// adjust count as appropriate if new state is valid
// repeat for all other state cases
switch (state) switch (state)
{ {
case state1: case state1:
// check new state is valid, coming from state1, increment error count if not
// adjust count as appropriate if new state is valid
switch (new_state) { switch (new_state) {
case state1: break; case state1: break;
case state2: count++; break; case state2: count++; break;

View File

@@ -70,8 +70,12 @@ enum states get_current_state()
} }
// doesn't have to be a fn anymore, but will be to maintain readability, expandability,
// compatibility with setup() fn
// also speed impact probably doesn't matter much since setup() runs once per boot
void initialiseEncoderStateMachine() void initialiseEncoderStateMachine()
{ {
// gets current state (initial) state and sets it
state = get_current_state(); state = get_current_state();
} }
@@ -80,11 +84,12 @@ void updateEncoderStateMachine()
{ {
enum states new_state = get_current_state(); // get new state, don't update state yet so states can be compared enum states new_state = get_current_state(); // get new state, don't update state yet so states can be compared
// check new state is valid, coming from state1, increment error count if not
// adjust count as appropriate if new state is valid
// repeat for all other state cases
switch (state) switch (state)
{ {
case state1: case state1:
// check new state is valid, coming from state1, increment error count if not
// adjust count as appropriate if new state is valid
switch (new_state) { switch (new_state) {
case state1: break; case state1: break;
case state2: count++; break; case state2: count++; break;

View File

@@ -9,8 +9,8 @@ float NISTmilliVoltsToDegCKtype(float tcEMFmV); // returns temp in degC assumin
float adc_to_voltage(float v_ref, int n_adc); float adc_to_voltage(float v_ref, int n_adc);
float kelvin_to_c(float k); float kelvin_to_c(float k);
float resistance_to_temperature(float r); float resistance_to_thermistor_temperature(float r);
float voltage_to_resistance(float v); float voltage_to_thermistor_resistance(float v);
float voltage_to_erc(float v); float voltage_to_erc(float v);
int main() int main()
@@ -23,12 +23,12 @@ int main()
scanf("%d %d", &thermistor_val, &thermocouple_val); scanf("%d %d", &thermistor_val, &thermocouple_val);
// Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v) // Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v)
thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_resistance(adc_to_voltage(v_ref, thermistor_val)))); thermistor_temp = kelvin_to_c(resistance_to_thermistor_temperature(voltage_to_thermistor_resistance(adc_to_voltage(v_ref, thermistor_val))));
// Calculate thermocouple temperature in degrees C ( Part c, i - iv) // Calculate thermocouple temperature in degrees C ( Part c, i - iv)
e_rc = 1000*voltage_to_erc(adc_to_voltage(v_ref, thermocouple_val)); // convert to millivolts e_rc = 1000*voltage_to_erc(adc_to_voltage(v_ref, thermocouple_val)); // convert to millivolts
e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); // eqn (6) lab prep sheet
thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); // eqn (7) lab prep sheet
// Output results // Output results
@@ -42,7 +42,7 @@ int main()
/* Write a function here to convert ADC value to voltages. (Part a, equation 1) /* Write a function here to convert ADC value to voltages. (Part a, equation 1)
Call it from the main() function above */ Call it from the main() function above */
float adc_to_voltage(float v_ref, int n_adc) { float adc_to_voltage(float v_ref, int n_adc) {
return (float)n_adc*v_ref/1024.0; return (float)n_adc*v_ref/1024.0; // eqn (1) lab prep sheet
} }
@@ -54,23 +54,29 @@ float kelvin_to_c(float k) {
} }
float resistance_to_temperature(float r) { // Convert Resistance (Ohms) to Temperature (Kelvin) (for thermistor)
float resistance_to_thermistor_temperature(float r) {
// Define Thermistor constants // Define Thermistor constants
float t_0 = 298.15; float t_0 = 298.15; // Kelvin
float r_0 = 10000; float r_0 = 10000; // Ohms
float b = 3975; float b = 3975; // Kelvin
return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); // eqn (3) lab prep sheet
} }
float voltage_to_resistance(float v) { // Convert Voltage (Volts) to Resistance (Ohms)
return 1000*((10.0*3.3/v)-10.0); float voltage_to_thermistor_resistance(float v) {
float pull_down_resistance = 10; // kOhms
float v_hi = 3.3; // Volts
return 1000*((pull_down_resistance*v_hi/v)-10.0); // eqn (4) lab prep sheet
} }
// Convert Voltage to E_RC
float voltage_to_erc(float v) { float voltage_to_erc(float v) {
return (v-0.35)/54.4; return (v-0.35)/54.4; // eqn (5) lab prep sheet
} }

View File

@@ -17,7 +17,7 @@
float adc_to_voltage(int n_adc); float adc_to_voltage(int n_adc);
float kelvin_to_c(float k); float kelvin_to_c(float k);
float resistance_to_temperature(float r); float resistance_to_temperature(float r);
float voltage_to_resistance(float v); float voltage_to_thermistor_resistance(float v);
float voltage_to_erc(float v); float voltage_to_erc(float v);
void setup() void setup()
@@ -34,12 +34,12 @@ void loop()
thermistor_val = analogRead(ThermistorPin); thermistor_val = analogRead(ThermistorPin);
thermocouple_val = analogRead(TCpin) thermocouple_val = analogRead(TCpin)
// Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v) // Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v)
thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_resistance(adc_to_voltage(thermistor_val)))); thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_thermistor_resistance(adc_to_voltage(thermistor_val))));
// Calculate thermocouple temperature in degrees C ( Part c, i - iv) // Calculate thermocouple temperature in degrees C ( Part c, i - iv)
e_rc = 1000*voltage_to_erc(adc_to_voltage(thermocouple_val)); // convert to millivolts e_rc = 1000*voltage_to_erc(adc_to_voltage(thermocouple_val)); // convert to millivolts
e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); // eqn (6) lab prep sheet
thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); // eqn (7) lab prep sheet
/* Display results. Don't use printf or formatting etc., they don't work on the Arduino. Just use /* Display results. Don't use printf or formatting etc., they don't work on the Arduino. Just use
the serial print statements given here, inserting your own code as needed */ the serial print statements given here, inserting your own code as needed */
@@ -55,7 +55,7 @@ void loop()
/* Write a function to convert ADC value to /* Write a function to convert ADC value to
voltage: put it here and use it in your code above*/ voltage: put it here and use it in your code above*/
float adc_to_voltage(int n_adc) { float adc_to_voltage(int n_adc) {
return (float)n_adc*V_REF/1024.0; return (float)n_adc*V_REF/1024.0; // eqn (1) lab prep sheet
} }
@@ -66,23 +66,29 @@ float kelvin_to_c(float k) {
} }
float resistance_to_temperature(float r) { // Convert Resistance (Ohms) to Temperature (Kelvin) (for thermistor)
float resistance_to_thermistor_temperature(float r) {
// Define Thermistor constants // Define Thermistor constants
float t_0 = 298.15; float t_0 = 298.15; // Kelvin
float r_0 = 10000; float r_0 = 10000; // Ohms
float b = 3975; float b = 3975; // Kelvin
return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); // eqn (3) lab prep sheet
} }
float voltage_to_resistance(float v) { // Convert Voltage (Volts) to Resistance (Ohms)
return 1000*((10.0*3.3/v)-10.0); float voltage_to_thermistor_resistance(float v) {
float pull_down_resistance = 10; // kOhms
float v_hi = 3.3; // Volts
return 1000*((pull_down_resistance*v_hi/v)-10.0); // eqn (4) lab prep sheet
} }
// Convert Voltage to E_RC
float voltage_to_erc(float v) { float voltage_to_erc(float v) {
return (v-0.35)/54.4; return (v-0.35)/54.4; // eqn (5) lab prep sheet
} }