diff --git a/Flusssensor_C15/Flusssensor_C15.ino b/Flusssensor_C15/Flusssensor_C15.ino new file mode 100644 index 0000000000000000000000000000000000000000..a7e7220505aa65105ecb76ad089c2250dc17f0fc --- /dev/null +++ b/Flusssensor_C15/Flusssensor_C15.ino @@ -0,0 +1,145 @@ +const String version = "ReadFlow 1.2"; // Version of this script + +//-----------------------------------------Define variables and pins----------- + +const int flowChannelC15 = 0; +const int flowChannelRegen = 1; +const int tempChannelC15 = 2; +const int tempChannelRegen = 3; + +float rateRegen = 0.0; +float rateC15 = 0.0; +unsigned long startTime = 0.0; +unsigned long timeEnd = 0.0; + +float tempVoltageRegen = 0.0; +float tempVoltageC15 = 0.0; +int resistance = 20000; +float tempResistanceRegen = 0; +float tempResistanceC15 = 0; + +int rotationsRegen = 0; +int rotationValueRegen = 0; +int tempValueRegen = 0; +int rotationsC15 = 0; +int rotationValueC15 = 0; +int tempValueC15 = 0; +int lastValueRegen = 0; +int lastValueC15 = 0; +int firstCycle = 1; + +// sensor parameters +long measurements = 100; //How many times should the sensor be read for a single readout? +const int averaging = 50; // Number of times for the running average +int precision = 5; // Number of digits to print. +int highVoltage = 500; + +RunningAverage average0(averaging); //generate running average variable +RunningAverage average1(averaging); //generate running average variable +RunningAverage average2(averaging); //generate running average variable +RunningAverage average3(averaging); //generate running average variable + +//---------------------------------------Start loop---------------------------- + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + startTime = micros(); + for(int x=1; x <= measurements; x++) + { + rotationValueRegen = analogRead(flowChannelRegen) + tempValueRegen += analogRead(tempValueRegen) + rotationValueC15 = analogRead(flowChannelC15) + tempValueC15 += analogRead(tempChannelC15) + if(firstCycle==0): + { + if(lastvalueRegen < highVoltage && rotationValueRegen > highVoltage) + { + rotationsRegen++; + } + if(lastvalueRegen > highVoltage && rotationValueRegen < highVoltage) + { + rotationsRegen++; + } + if(lastvalueC15 < highVoltage && rotationValueC15 > highVoltage) + { + rotationsC15++; + } + if(lastvalueC15 > highVoltage && rotationValueC15 < highVoltage) + { + rotationsC15++; + } + } + lastValueC15 = rotationValueC15; + lastValueRegen = rotationValueRegen; + firstCycle = 0; + } + endTime = micros() + tempVoltageRegen = tempValueRegen / measurements * 5 / 1024; + tempVoltageC15 = tempValueC15 / measurements * 5 / 1024; + tempResistanceRegen = resistance * (5 - tempVoltageRegen) / tempVoltageRegen; + tempResistanceC15 = resistance * (5 - tempVoltageC15); + rateRegen = rotationsRegen / 2 / (endTime - startTime) * 1e6; + rateC15 = rotationsC15 / 2 / (endTime - startTime) * 1e6; + average0.addValue(rateC15) + average1.addValue(rateRegen) + average2.addValue(tempResistanceC15) + average3.addValue(tempResistanceRegen) + if (Serial.available()){ + char input[20]; + int len; + len = Serial.readBytesUntil('\n', input, 20); + input[len] = '\0'; // Null terminating the array to interpret it as a string + switch (input[0]){ + case 'p': // Ping + Serial.println("pong"); + break; + case 'r': // Send read average sensor data + sendSensorDataAverage(); + break; + case 'm': //Set number of measurements + measurements = atoi(&input[1]); + break; + case 'f': //Set the precision + precision = atoi(&input[1]); + break; + case 'd': // debug + Serial.println("debug"); + break; + case 'v': // Send Version + Serial.println(version); + break; + case 'l': // Send last value + lastData(); + }; +} + + +void sendSensorDataAverage(){ + // Send the running average sensor values. + Serial.print(average0.getAverage(), precision); + Serial.print("\t"); + Serial.print(average1.getAverage(), precision); + Serial.print("\t"); + Serial.print(average2.getAverage(), precision); + Serial.print("\t"); + Serial.print(average3.getAverage(), precision); + Serial.print("\t"); + Serial.println(); +} + +void lastData(){ + // Send the last value. + Serial.print(rateC15); + Serial.print("\t"); + Serial.print(rateRegen); + Serial.print("\t"); + Serial.print(tempResistanceC15); + Serial.print("\t"); + Serial.print(tempResistanceRegen); + Serial.println(); +} diff --git a/HumiditySensor/HumiditySensor.ino b/HumiditySensor/HumiditySensor.ino index fa5f2f810dcfd523be1129584626b03a4fde93b3..817beca5e21a3af7f24f9a4ab9663e1581c83af0 100644 --- a/HumiditySensor/HumiditySensor.ino +++ b/HumiditySensor/HumiditySensor.ino @@ -3,7 +3,9 @@ Read humidity sensor values and send them over serial connection. *************************** */ -const String version = "HumiditySensor 1.0"; // Version of this script +const String version = "1.1.0"; // Version of this script + +// *****************************OPERATIONAL PARAMETERS***************************** #include "Wire.h" int del = 10; // was 10 byte rval = 0x00; @@ -12,9 +14,6 @@ struct ArrayData { double array[2]; }; - -// *****************************OPERATIONAL PARAMETERS***************************** -// Arduino Due, PICO2: COM6, MYRES: COM31 // sensor parameters long measurements = 10; //How many times should the sensor be read for a single readout? old 100 const int averaging = 5; // Number of times for the running average old 50 @@ -357,10 +356,13 @@ void loop() { break; } case 'v': // Send Version - { - Serial.println(version); - break; - } + Serial.print(__FILE__); // file name at compilation + Serial.print(" "); + Serial.print(version); + Serial.print(" "); + Serial.print(__DATE__); // compilation date + Serial.println(); + break; }; } diff --git a/ReadSensors/ReadSensors.ino b/ReadSensors/ReadSensors.ino index 55ba79e3c35abb0f3d01fdd6322dbba939757839..9e8d58e7d9f87a592c8568bea20c0d369efd56c2 100644 --- a/ReadSensors/ReadSensors.ino +++ b/ReadSensors/ReadSensors.ino @@ -3,7 +3,7 @@ * Read sensor values and send them over serial connection. *************************** */ -const String version = "ReadSensors 1.1"; // Version of this script +const String version = "1.1.1"; // Version of this script. Filename is added before, date of compilation afterwards. // *****************************OPERATIONAL PARAMETERS***************************** @@ -114,7 +114,12 @@ void loop(){ Serial.println("debug"); break; case 'v': // Send Version - Serial.println(version); + Serial.print(__FILE__); // file name at compilation + Serial.print(" "); + Serial.print(version); + Serial.print(" "); + Serial.print(__DATE__); // compilation date + Serial.println(); break; }; } diff --git a/ServoShutter/ServoShutter.ino b/ServoShutter/ServoShutter.ino index 7de100c19e214dd00a39189132fe837a9c6195a9..d6b51a7be6e1f076808fde24693587f8e9804f42 100644 --- a/ServoShutter/ServoShutter.ino +++ b/ServoShutter/ServoShutter.ino @@ -8,7 +8,7 @@ You can send a command to set the shutter (o0, o1) or to switch to manual mode ( "l" returns the current state and manual mode (tab separated) **************************** */ -const String version = "ServoShutter 0.2.0"; // Version of this script +const String version = "1.0.0"; // Version of this script // Servo setup #include <Servo.h> @@ -48,7 +48,12 @@ void loop() Serial.println("pong"); break; case 'v': // Send Version - Serial.println(version); + Serial.print(__FILE__); // file name at compilation + Serial.print(" "); + Serial.print(version); + Serial.print(" "); + Serial.print(__DATE__); // compilation date + Serial.println(); break; case 'l': // return last state Serial.print(state); diff --git a/Shutterschalter/Shutterschalter.ino b/Shutterschalter/Shutterschalter.ino index 54320a7bc43d3d2271639fdb3ebd8e5c1228957a..66bb567ee6b2760f735a96db3296e911233f1b68 100644 --- a/Shutterschalter/Shutterschalter.ino +++ b/Shutterschalter/Shutterschalter.ino @@ -4,8 +4,8 @@ * Control the shutters of the Quanta-Ray **************************************** */ - -const String version = "Shutterschalter 0.1"; // Version of this script +//** PARAMETERS **// +const String version = "1.0.0"; // Version of this script #include <LiquidCrystal.h> //Library for LCD LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Initialize LCD Lib for our application //the numbers are the Digital Pins, which are used for the LCD @@ -21,6 +21,7 @@ const int shgLedPin = 8; // Constants const int HighLimit = 850; +//** VARIABLES **// // State variables int fundamentalShutter = LOW; int shgShutter = LOW; @@ -83,7 +84,12 @@ void loop(){ Serial.println("Debug"); break; case 'v': // Send Version - Serial.println(version); + Serial.print(__FILE__); // file name at compilation + Serial.print(" "); + Serial.print(version); + Serial.print(" "); + Serial.print(__DATE__); // compilation date + Serial.println(); break; } } diff --git a/TemperatureController/TemperatureController.ino b/TemperatureController/TemperatureController.ino index 79ac334be9122fab2589d37d155f4bc2d8dd3211..345a2711668f36042f7695092e49cfd121b2c523 100644 --- a/TemperatureController/TemperatureController.ino +++ b/TemperatureController/TemperatureController.ino @@ -2,13 +2,21 @@ /* *************************** * Read sensor values and calculate a PID value from sensor 0. - * Depending on this PID value, a relay is switched. + * Depending on this PID value, a control (relay, valve etc.) is switched. *************************** */ -const String version = "TemperatureController 1.0.1"; // Version of this script -//** CONSTANTS **// -const int cyclePeriod = 1000; //program cycle period in ms +//** CONSTANTS PARAMETERS change for your application **// +const String version = "1.2.3"; // Version of this script. Filename is added before, date of compilation afterwards. + +const int cyclePeriod = 10; //program cycle period in s + +const int emergencyPin = -1; // Analog channel to which the emergency sensor is connected. Set to -1 to deactivate +const int emergencyThreshold = 341; // Threshold value, when the control should be deactivated due to emergency activated +const int controlPin = 11; //Pin by which the control unit is controlled (11 or 12) + + +//** CONSTANTS OF CODE do not change **// // I/O-pins const int sensor0Pin = 0; //Channel to which the sensor 0 is connected @@ -18,12 +26,18 @@ const int sensor3Pin = 3; //Channel to which the sensor 2 is connected const int sensor4Pin = 4; //Channel to which the sensor 2 is connected const int sensor5Pin = 5; //Channel to which the sensor 2 is connected -const int relayPin = 11; //Pin by which the relay unit is controlled (11 or 12) -const int indicatorPin = 3; //Pin sending a control signal, indicating if the relay is closed -const int tooColdPin = 2; //Pin being set high, if the temperature is too low -const int tooHotPin = 4; //Pin being set high, if the temperature is too high +const int indicatorPin = 3; //Pin sending a control signal, indicating if the control is enabled +const int blueLedPin = 2; //Pin being set high, if the temperature is too low +const int redLedPin = 4; //Pin being set high, if the temperature is too high const int powerPin = 13; //Pin for indicating device is on; also used to indicate errors +// MISC +const long rolloverThreshold = 4294965000; //After how many milliseconds should we suspend normal operation to await millis()-rollover? +// (Should be at least cyclePeriod+digitalPinSetDelay below the maximum value of an unsigned long of 4.294.967.295) +// ******************************************************************************** + + +// ***********************************VARIABLES************************************ #include <EEPROM.h> const int floatsize = sizeof(float); @@ -32,13 +46,6 @@ long measurements = 100; //How many times should the sensor be read for a single const int averaging = 50; // Number of times for the running average int precision = 5; // Number of digits to print. - -// MISC -const long rolloverThreshold = 4294965000; //After how many milliseconds should we suspend normal operation to await millis()-rollover? (Should be at least cyclePeriod+digitalPinSetDelay below the maximum value of an unsigned long of 4.294.967.295) -// ******************************************************************************** - - -// ***********************************VARIABLES************************************ // Sensor values. float sensor0Value = 0; //Define sensor value variable float sensor1Value = 0; //Define sensor value variable @@ -57,8 +64,8 @@ RunningAverage average4(averaging); //generate running average variable RunningAverage average5(averaging); //generate running average variable // As we only want to do one measurement per second, we need to know, when we last ran. -unsigned long nextCycle = 3000; //set starting values for an initial delay of 5s to prevent racing conditions on start -unsigned long nextOff = 0; // When to switch off the heater the next time +unsigned long nextCycle = 3000; //set starting values for an initial delay of some seconds to prevent racing conditions on start +unsigned long nextOff = 0; // When to disable the control for the next time //******************************PID-CONTROL***************************************** // Werte werden aus EEPROM gelesen @@ -68,9 +75,8 @@ float PIDKi; //Wert für die PID Regelung (Integral-Teil) float integral; //Wert für die PID Integralteil -int controller = 2; // controller mode: 0 = off, 1 = control cooling only, 2 = full control +int controller = 1; // controller mode: 0 = off, 1 = on -boolean autoData=false; //****************************************************************************** @@ -78,10 +84,10 @@ void setup() { //Setup the serial connection and the pins Serial.begin(9600); - pinMode(tooColdPin,OUTPUT); - pinMode(tooHotPin,OUTPUT); + pinMode(blueLedPin,OUTPUT); + pinMode(redLedPin,OUTPUT); pinMode(indicatorPin, OUTPUT); - pinMode(relayPin, OUTPUT); + pinMode(controlPin, OUTPUT); pinMode(powerPin, OUTPUT); digitalWrite(powerPin, HIGH); @@ -137,10 +143,14 @@ void loop(){ average4.addValue(sensor4Value); average5.addValue(sensor5Value); - //Serial communication - if(autoData){ - sendSensorDataLast(); + int emergencyValue = analogRead(emergencyPin); + if (emergencyPin >= 0 and emergencyValue > emergencyThreshold){ + controller = 0; + enable_control(false); + digitalWrite(redLedPin, HIGH); } + + //Serial communication if ( Serial.available() ){ char input[20]; int len; @@ -162,16 +172,6 @@ void loop(){ case 'f': //Set the precision precision = atoi(&input[1]); break; - case 'a': // Automatically send sensor data - if ( input[1] <= '0' ){ - autoData= false; - Serial.println("ACK autoOff"); - } - else { - autoData=true; - Serial.println("ACK autoOn"); - } - break; case 'c': // Configure the controller controller = input[1]-'0'; Serial.println("ACK-controller"); @@ -194,14 +194,14 @@ void loop(){ Serial.println("ACK-setpoint"); } break; - case 'o': // control the relay itself + case 'o': // direct output control if ( input[1] <= '0' ){ - controlRelay( false ); - Serial.println("ACK openRelay"); + enable_control( false ); + Serial.println("ACK disable control"); } else { - controlRelay( true ); - Serial.println("ACK closeRelay"); + enable_control( true ); + Serial.println("ACK enable control"); } break; case 'd': // debug @@ -215,7 +215,12 @@ void loop(){ Serial.println(); break; case 'v': // Send Version - Serial.println(version); + Serial.print(__FILE__); // file name at compilation + Serial.print(" "); + Serial.print(version); + Serial.print(" "); + Serial.print(__DATE__); // compilation date + Serial.println(); break; case 'e': // Store values in EEPROM EEPROM.put(0 * floatsize, setpoint); @@ -224,24 +229,38 @@ void loop(){ EEPROM.put(3 * floatsize, integral); Serial.println("ACK EEPROM saved."); break; + case 'x': // Parameters, name TBD + Serial.print("cyclePeriod: "); + Serial.print(cyclePeriod); + Serial.print("s, "); + Serial.print("emergencyPin: "); + Serial.print(emergencyPin); + Serial.print(", "); + Serial.print("emergencyThreshold: "); + Serial.print(emergencyThreshold); + Serial.print(", "); + Serial.print("controlPin: "); + Serial.print(controlPin); + Serial.println(); + break; } } // Control temperature, if desired if ( millis() >= nextCycle ){ - if (controller & 2){ + if (controller > 0){ calculatePID(); } - nextCycle += cyclePeriod; + nextCycle += cyclePeriod * 1000; } if ( millis() >= nextOff && nextOff > 0){ - controlRelay(false); + enable_control(false); } //if we are within 2300ms of rollover of millis counter suspend normal operation if (millis() > rolloverThreshold){ - nextCycle = cyclePeriod; //set everything for continuing normal operation after rollover + nextCycle = cyclePeriod * 1000; //set everything for continuing normal operation after rollover while (millis() > rolloverThreshold){}; //wait for rollover to happen }; } @@ -287,8 +306,8 @@ void calculatePID(){ // Calculate the PID signal float error = setpoint - average0.getAverage(); integral += PIDKi * error; - if (integral > cyclePeriod){ - integral = cyclePeriod; + if (integral > 1000){ + integral = 1000; } else if (integral < 0){ integral = 0; @@ -296,21 +315,22 @@ void calculatePID(){ float pid = PIDKp * error + integral; if (pid < 0){ nextOff = 0; + enable_control( false ); } else { - nextOff = millis() + pid; - controlRelay( true ); + nextOff = millis() + cyclePeriod * pid; + enable_control( true ); } } -void controlRelay(const boolean close){ - //Open or close the relay - if ( close ){ - digitalWrite(relayPin, HIGH); +void enable_control(const boolean enabled){ + //Enable or disable the control (relay, valve...) + if ( enabled ){ + digitalWrite(controlPin, HIGH); digitalWrite(indicatorPin, HIGH); } else { - digitalWrite(relayPin, LOW); + digitalWrite(controlPin, LOW); digitalWrite(indicatorPin, LOW); } }