It's kinda long, but here it is:
#include <Keyboard.h>
//Define variables
const int Led_Green = 9;
const int Led_Yellow = 10;
const int Gate1 = 5;
const int Gate2 = 6;
int JoystickValue_X_Y;
const int Joystick_X = A0;
const int Joystick_Y = A1;
const int pressbutton = 2;
int JoystickValue_X = 0;
int JoystickValue_Y = 0;
int pressbuttonValue = 0;
const int middle_X = 512;
const int middle_Y = 512;
unsigned long startTime;
unsigned long currentTime;
int tempsensor1 = A3;
int tempsensor2 = A4;
int tempsensor3 = A2;
int Temp1;
int Temp2;
int Temp3;
String InBytes;
//Define experiment (0 = manual, 1 = vollgas, 2 = , 3 = )
int experiment = 0;
//Define functions--------------------------------------------------------------------
int 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]
pressbuttonValue = digitalRead(pressbutton);
return JoystickValue_X;
return JoystickValue_Y;
return pressbutton;
}
int case_function( int JoystickInput_X, int JoystickInput_Y) {
if (currentTime < 45000) {
JoystickValue_X = JoystickInput_X;
JoystickValue_Y = JoystickInput_Y;
}
else {
JoystickValue_X = 0;
JoystickValue_Y = 0;
}
return JoystickValue_X;
return JoystickValue_Y;
}
//---------------------------------------------------------------------------------
void setup() {
startTime = millis(); //internal timer
Keyboard.begin();
pinMode(Joystick_X, INPUT); // X-axis
pinMode(Joystick_Y, INPUT); // Y-axis
pinMode(pressbutton,INPUT); // press button
pinMode(Led_Green,OUTPUT);
pinMode(Led_Yellow, OUTPUT);
digitalWrite(pressbutton, HIGH);
Serial.begin(9600);
}
void loop() {
while(Serial.available() > 0) {
//InBytes = Serial.readStringUntil('n');
Temp1 = analogRead(tempsensor1);
Temp2 = analogRead(tempsensor2);
Temp3 = analogRead(tempsensor3);
if (InBytes == "y") {
Serial.write("experiment starts");
switch(experiment) {
case 0:
JoystickValue_X_Y = manual_function();
Temp1 = analogRead(tempsensor1);
Temp2 = analogRead(tempsensor2);
Temp3 = analogRead(tempsensor3);
break;
case 11:
currentTime = millis();
if (Temp1 < 23) {
JoystickValue_X = case_function(1023, 0);
JoystickValue_Y = case_function(1023, 0);
Temp1 = analogRead(tempsensor1);
}
else {
exit(0);
}
break;
case 12:
currentTime = millis();
if(Temp2 < 23) {
JoystickValue_X = case_function(0, 1023);
JoystickValue_Y = case_function(1023, 0);
Temp2 = analogRead(tempsensor2);
}
case 13:
currentTime = millis();
if (Temp1 < 23 && Temp2 < 23) {
JoystickValue_X = case_function(1023, 1023);
JoystickValue_Y = case_function(1023, 1023);
Temp2 = analogRead(tempsensor2);
Temp1 = analogRead(tempsensor1);
}
else {
exit(0);
}
break;
case 21:
currentTime = millis();
if (Temp1 > 36) {
JoystickValue_X = case_function(1023, 0);
JoystickValue_Y = case_function(1023, 0);
Temp1 = analogRead(tempsensor1); }
else {
exit(0);
}
break;
case 22:
currentTime = millis();
if (Temp2 > 36) {
JoystickValue_X = case_function(0, 1023);
JoystickValue_Y = case_function(0, 1023);
Temp2 = analogRead(tempsensor2);;
}
else {
exit(0);
}
break;
case 23:
currentTime = millis();
if (Temp1 > 36 && Temp2 > 36) {
JoystickValue_X = case_function(1023, 1023);
JoystickValue_Y = case_function(1023, 1023);
Temp1 = analogRead(tempsensor1);
Temp2 = analogRead(tempsensor2);
}
else {
exit(0);
}
break;
}}
else {
Serial.write("wrong starting signal");
exit(0);
}}
// else {
// Serial.write("no starting signal");
// }
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.print(JoystickValue_Y);
Serial.print(";");
Serial.print(pressbuttonValue);
Serial.print(";");
Serial.print(Temp1);
Serial.print(";");
Serial.print(Temp2);
Serial.print(";");
Serial.println(Temp3);
delay(100);
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);
}
if(pressbuttonValue = LOW) {
analogWrite(Led_Green, JoystickValue_Y/4);
analogWrite(Led_Yellow, JoystickValue_X/4);
analogWrite(Gate2, JoystickValue_X/4);
analogWrite(Gate1, JoystickValue_Y/4);
}
}