here is the code
#include <math.h>
const int Led_Green = 9;
const int Led_Yellow = 10;
const int Gate1 = 5;
const int Gate2 = 6;
const int Joystick_X = A0;
const int Joystick_Y = A1;
const int pushbutton = 3;
int JoystickValue_X = 0;
int JoystickValue_Y = 0;
const int middle_X = 512;
const int middle_Y = 512;
int pushbuttonState;
int JoystickValue_X_Y;
int tempsensor1 = A2;
int tempsensor2 = A3;
int tempsensor3 = A4;
int readVal1 = 0;
int readVal2 = 0;
int readVal3 = 0;
double Temp1 = 0;
double Temp2 = 0;
double Temp3 = 0;
int TempLimit;
double offset_degree;
double offset_degree1 = -2.2; // RT=26.0°C, difference
double offset_degree2 = 0.7;
double offset_degree3 = -2.47;
unsigned long deltaTime;
bool running = true;
//Define experiment (0 = manual, 1 = vollgas, 2 = , 3 = )
int experiment = 0;
//Define functions--------------------------------------------------------------------
void manual_function() {
JoystickValue_X = analogRead (Joystick_X); // [0, 1023]
JoystickValue_X = constrain(JoystickValue_X, middle_X, 1023); // [512, 1023]
JoystickValue_X = map(JoystickValue_X, middle_X, 1023, 0, 1023); // [0, 100]
JoystickValue_Y = analogRead (Joystick_Y); // [0, 1023]
JoystickValue_Y = constrain(JoystickValue_Y, middle_Y, 1023); // [512, 1023]
JoystickValue_Y = map(JoystickValue_Y, middle_Y, 1023, 0, 1023); // [0, 100]
Serial.print(Temp1);
Serial.print(";");
Serial.print(Temp2);
Serial.print(";");
Serial.print(Temp3);
Serial.print(";");
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.println(JoystickValue_Y);
}
//Funktion Temperatur offset und Umrechung-----------------------------------------------------
double Thermistor(int RawADC, int sensor_number)
{
double Temp;
double offset_degree;
Temp = log(10000.0 * ((1024.0 / RawADC - 1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
Temp = Temp - 273.15; // Konvertierung von Kelvin in Celsius
switch (sensor_number) {
case 1:
offset_degree = offset_degree1;
break;
case 2:
offset_degree = offset_degree2;
break;
case 3:
offset_degree = offset_degree3;
break;
default:
offset_degree = 0;
break;
}
return Temp+offset_degree;
}
//-----------------------------------------------------------------------------------------------
void case_function( int JoystickInput_X, int JoystickInput_Y, int TempLimit, int TempValue) {
if(deltaTime < 45000L) {
JoystickValue_X = JoystickInput_X;
JoystickValue_Y = JoystickInput_Y;
Serial.print(Temp1);
Serial.print(";");
Serial.print(Temp2);
Serial.print(";");
Serial.print(Temp3);
Serial.print(";");
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.println(JoystickValue_Y);
}
else if ((deltaTime >= 45000L) && (TempValue > TempLimit)) {
JoystickValue_X = 0;
JoystickValue_Y = 0;
Serial.print(Temp1);
Serial.print(";");
Serial.print(Temp2);
Serial.print(";");
Serial.print(Temp3);
Serial.print(";");
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.println(JoystickValue_Y);
}
else if ((deltaTime > 45000L) && (TempValue < TempLimit)) {
Serial.print("Experiment done.");
running = false;
}
}
//---------------------------------------------------------------------------------
void setup() {
pinMode(Joystick_X, INPUT); // X-axis
pinMode(Joystick_Y, INPUT); // Y-axis
pinMode(pushbutton,INPUT); // press button
digitalWrite(pushbutton, INPUT_PULLUP);
pushbuttonState = digitalRead(pushbutton);
Serial.begin(9600);
while (digitalRead(pushbutton) == HIGH);
}
void loop() {
if(running == true) {
//currentTime = millis();
//unsigned long deltaTime = CalculateDeltaTime();
static unsigned long TimeZero = millis();
deltaTime = millis() - TimeZero;
Serial.print("start");
Serial.print(";");
Serial.print(deltaTime);
Serial.print(";");
readVal1 = analogRead(tempsensor1);
Temp1 = Thermistor(readVal1,1);
readVal2 = analogRead(tempsensor2);
Temp2 = Thermistor(readVal2,2);
readVal3 = analogRead(tempsensor3);
Temp3 = Thermistor(readVal3,3);
switch(experiment) {
case 0:
manual_function();
break;
case 1:
case_function(1023, 0, 18, Temp1);
break;
case 2:
case_function(0, 1023, 23, Temp2);
break;
case 3:
case_function(1023, 1023, 23, Temp3);
break;
case 4:
case_function(1023, 0, 36, Temp1);
break;
case 5:
case_function(0, 1023, 36, Temp2);
break;
case 6:
case_function(1023, 1023, 36, Temp3);
break;
}
if (JoystickValue_Y <= 0) {
analogWrite(Led_Green, 0);
analogWrite(Gate1, 0);
}
if (JoystickValue_Y > 0) {
analogWrite(Led_Green, JoystickValue_Y/4);
analogWrite(Gate1, JoystickValue_Y/4);
}
if (JoystickValue_X <= 0) {
analogWrite(Led_Yellow, 0);
analogWrite(Gate2, 0);
}
if (JoystickValue_X > 0) {
analogWrite(Led_Yellow, JoystickValue_X/4);
analogWrite(Gate2, JoystickValue_X/4);
}
delay(1000);
}
}