Skip to content
Snippets Groups Projects
Commit 40680b51 authored by Myres's avatar Myres
Browse files

Merge branch 'master' of git.rwth-aachen.de:nloqo/arduino-sketches

parents 949bfe51 07815c88
No related branches found
No related tags found
No related merge requests found
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();
}
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
Read humidity sensor values and send them over serial connection. 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" #include "Wire.h"
int del = 10; // was 10 int del = 10; // was 10
byte rval = 0x00; byte rval = 0x00;
...@@ -12,9 +14,6 @@ struct ArrayData { ...@@ -12,9 +14,6 @@ struct ArrayData {
double array[2]; double array[2];
}; };
// *****************************OPERATIONAL PARAMETERS*****************************
// Arduino Due, PICO2: COM6, MYRES: COM31
// sensor parameters // sensor parameters
long measurements = 10; //How many times should the sensor be read for a single readout? old 100 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 const int averaging = 5; // Number of times for the running average old 50
...@@ -357,10 +356,13 @@ void loop() { ...@@ -357,10 +356,13 @@ void loop() {
break; break;
} }
case 'v': // Send Version case 'v': // Send Version
{ Serial.print(__FILE__); // file name at compilation
Serial.println(version); Serial.print(" ");
Serial.print(version);
Serial.print(" ");
Serial.print(__DATE__); // compilation date
Serial.println();
break; break;
}
}; };
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* Read sensor values and send them over serial connection. * 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***************************** // *****************************OPERATIONAL PARAMETERS*****************************
...@@ -114,7 +114,12 @@ void loop(){ ...@@ -114,7 +114,12 @@ void loop(){
Serial.println("debug"); Serial.println("debug");
break; break;
case 'v': // Send Version 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; break;
}; };
} }
......
...@@ -8,7 +8,7 @@ You can send a command to set the shutter (o0, o1) or to switch to manual mode ( ...@@ -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) "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 // Servo setup
#include <Servo.h> #include <Servo.h>
...@@ -48,7 +48,12 @@ void loop() ...@@ -48,7 +48,12 @@ void loop()
Serial.println("pong"); Serial.println("pong");
break; break;
case 'v': // Send Version 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; break;
case 'l': // return last state case 'l': // return last state
Serial.print(state); Serial.print(state);
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
* Control the shutters of the Quanta-Ray * Control the shutters of the Quanta-Ray
**************************************** ****************************************
*/ */
//** PARAMETERS **//
const String version = "Shutterschalter 0.1"; // Version of this script const String version = "1.0.0"; // Version of this script
#include <LiquidCrystal.h> //Library for LCD #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 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; ...@@ -21,6 +21,7 @@ const int shgLedPin = 8;
// Constants // Constants
const int HighLimit = 850; const int HighLimit = 850;
//** VARIABLES **//
// State variables // State variables
int fundamentalShutter = LOW; int fundamentalShutter = LOW;
int shgShutter = LOW; int shgShutter = LOW;
...@@ -83,7 +84,12 @@ void loop(){ ...@@ -83,7 +84,12 @@ void loop(){
Serial.println("Debug"); Serial.println("Debug");
break; break;
case 'v': // Send Version 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; break;
} }
} }
......
...@@ -2,13 +2,21 @@ ...@@ -2,13 +2,21 @@
/* /*
*************************** ***************************
* Read sensor values and calculate a PID value from sensor 0. * 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 **// //** CONSTANTS PARAMETERS change for your application **//
const int cyclePeriod = 1000; //program cycle period in ms 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 // I/O-pins
const int sensor0Pin = 0; //Channel to which the sensor 0 is connected 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 ...@@ -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 sensor4Pin = 4; //Channel to which the sensor 2 is connected
const int sensor5Pin = 5; //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 control is enabled
const int indicatorPin = 3; //Pin sending a control signal, indicating if the relay is closed const int blueLedPin = 2; //Pin being set high, if the temperature is too low
const int tooColdPin = 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 tooHotPin = 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 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> #include <EEPROM.h>
const int floatsize = sizeof(float); const int floatsize = sizeof(float);
...@@ -32,13 +46,6 @@ long measurements = 100; //How many times should the sensor be read for a single ...@@ -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 const int averaging = 50; // Number of times for the running average
int precision = 5; // Number of digits to print. 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. // Sensor values.
float sensor0Value = 0; //Define sensor value variable float sensor0Value = 0; //Define sensor value variable
float sensor1Value = 0; //Define sensor value variable float sensor1Value = 0; //Define sensor value variable
...@@ -57,8 +64,8 @@ RunningAverage average4(averaging); //generate running average variable ...@@ -57,8 +64,8 @@ RunningAverage average4(averaging); //generate running average variable
RunningAverage average5(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. // 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 nextCycle = 3000; //set starting values for an initial delay of some seconds to prevent racing conditions on start
unsigned long nextOff = 0; // When to switch off the heater the next time unsigned long nextOff = 0; // When to disable the control for the next time
//******************************PID-CONTROL***************************************** //******************************PID-CONTROL*****************************************
// Werte werden aus EEPROM gelesen // Werte werden aus EEPROM gelesen
...@@ -68,9 +75,8 @@ float PIDKi; //Wert für die PID Regelung (Integral-Teil) ...@@ -68,9 +75,8 @@ float PIDKi; //Wert für die PID Regelung (Integral-Teil)
float integral; //Wert für die PID Integralteil 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() { ...@@ -78,10 +84,10 @@ void setup() {
//Setup the serial connection and the pins //Setup the serial connection and the pins
Serial.begin(9600); Serial.begin(9600);
pinMode(tooColdPin,OUTPUT); pinMode(blueLedPin,OUTPUT);
pinMode(tooHotPin,OUTPUT); pinMode(redLedPin,OUTPUT);
pinMode(indicatorPin, OUTPUT); pinMode(indicatorPin, OUTPUT);
pinMode(relayPin, OUTPUT); pinMode(controlPin, OUTPUT);
pinMode(powerPin, OUTPUT); pinMode(powerPin, OUTPUT);
digitalWrite(powerPin, HIGH); digitalWrite(powerPin, HIGH);
...@@ -137,10 +143,14 @@ void loop(){ ...@@ -137,10 +143,14 @@ void loop(){
average4.addValue(sensor4Value); average4.addValue(sensor4Value);
average5.addValue(sensor5Value); average5.addValue(sensor5Value);
//Serial communication int emergencyValue = analogRead(emergencyPin);
if(autoData){ if (emergencyPin >= 0 and emergencyValue > emergencyThreshold){
sendSensorDataLast(); controller = 0;
enable_control(false);
digitalWrite(redLedPin, HIGH);
} }
//Serial communication
if ( Serial.available() ){ if ( Serial.available() ){
char input[20]; char input[20];
int len; int len;
...@@ -162,16 +172,6 @@ void loop(){ ...@@ -162,16 +172,6 @@ void loop(){
case 'f': //Set the precision case 'f': //Set the precision
precision = atoi(&input[1]); precision = atoi(&input[1]);
break; 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 case 'c': // Configure the controller
controller = input[1]-'0'; controller = input[1]-'0';
Serial.println("ACK-controller"); Serial.println("ACK-controller");
...@@ -194,14 +194,14 @@ void loop(){ ...@@ -194,14 +194,14 @@ void loop(){
Serial.println("ACK-setpoint"); Serial.println("ACK-setpoint");
} }
break; break;
case 'o': // control the relay itself case 'o': // direct output control
if ( input[1] <= '0' ){ if ( input[1] <= '0' ){
controlRelay( false ); enable_control( false );
Serial.println("ACK openRelay"); Serial.println("ACK disable control");
} }
else { else {
controlRelay( true ); enable_control( true );
Serial.println("ACK closeRelay"); Serial.println("ACK enable control");
} }
break; break;
case 'd': // debug case 'd': // debug
...@@ -215,7 +215,12 @@ void loop(){ ...@@ -215,7 +215,12 @@ void loop(){
Serial.println(); Serial.println();
break; break;
case 'v': // Send Version 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; break;
case 'e': // Store values in EEPROM case 'e': // Store values in EEPROM
EEPROM.put(0 * floatsize, setpoint); EEPROM.put(0 * floatsize, setpoint);
...@@ -224,24 +229,38 @@ void loop(){ ...@@ -224,24 +229,38 @@ void loop(){
EEPROM.put(3 * floatsize, integral); EEPROM.put(3 * floatsize, integral);
Serial.println("ACK EEPROM saved."); Serial.println("ACK EEPROM saved.");
break; 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 // Control temperature, if desired
if ( millis() >= nextCycle ){ if ( millis() >= nextCycle ){
if (controller & 2){ if (controller > 0){
calculatePID(); calculatePID();
} }
nextCycle += cyclePeriod; nextCycle += cyclePeriod * 1000;
} }
if ( millis() >= nextOff && nextOff > 0){ if ( millis() >= nextOff && nextOff > 0){
controlRelay(false); enable_control(false);
} }
//if we are within 2300ms of rollover of millis counter suspend normal operation //if we are within 2300ms of rollover of millis counter suspend normal operation
if (millis() > rolloverThreshold){ 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 while (millis() > rolloverThreshold){}; //wait for rollover to happen
}; };
} }
...@@ -287,8 +306,8 @@ void calculatePID(){ ...@@ -287,8 +306,8 @@ void calculatePID(){
// Calculate the PID signal // Calculate the PID signal
float error = setpoint - average0.getAverage(); float error = setpoint - average0.getAverage();
integral += PIDKi * error; integral += PIDKi * error;
if (integral > cyclePeriod){ if (integral > 1000){
integral = cyclePeriod; integral = 1000;
} }
else if (integral < 0){ else if (integral < 0){
integral = 0; integral = 0;
...@@ -296,21 +315,22 @@ void calculatePID(){ ...@@ -296,21 +315,22 @@ void calculatePID(){
float pid = PIDKp * error + integral; float pid = PIDKp * error + integral;
if (pid < 0){ if (pid < 0){
nextOff = 0; nextOff = 0;
enable_control( false );
} }
else { else {
nextOff = millis() + pid; nextOff = millis() + cyclePeriod * pid;
controlRelay( true ); enable_control( true );
} }
} }
void controlRelay(const boolean close){ void enable_control(const boolean enabled){
//Open or close the relay //Enable or disable the control (relay, valve...)
if ( close ){ if ( enabled ){
digitalWrite(relayPin, HIGH); digitalWrite(controlPin, HIGH);
digitalWrite(indicatorPin, HIGH); digitalWrite(indicatorPin, HIGH);
} }
else { else {
digitalWrite(relayPin, LOW); digitalWrite(controlPin, LOW);
digitalWrite(indicatorPin, LOW); digitalWrite(indicatorPin, LOW);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment