//define outputs for motor control
int P_FET_Q1 = 5;
int N_FET_Q4 = 9;
int P_FET_Q3 = 6;
int N_FET_Q2 = 10;
//define inputs for motor control
int MovPos0m = 13; //button to drive close to person
int MovPos5m = 14; //button to drive to 5 meter distance
int MovPos7m = 15; //button to drive to 7 meter distance
int MovPos10m = 16; //button to drive to 10 meter distance
int PreSw0m = 19; //preswitch to slow down velocity bevor the carriage arived at person
int PreSw5m = 20; //preswitch to slow down velocity bevor the carriage arived at 5m
int PreSw7m = 21; //preswitch to slow down velocity bevor the carriage arived at 7m
int PreSw10m = 22; //preswitch to slow down velocity bevor the carriage arived at 10m
int LiSw0m = 23; //limitwitch to stop the carriage arived at person
int LiSw5m = 24; //limitwitch to stop the carriage arived at 5m
int LiSw7m = 25; //limitwitch to stop the carriage arived at 7m
int LiSw10m = 26; //limitwitch to stop the carriage arived at 10m
//variables for controlling
int PWM = 0; //variable for pwm signal
int actPos = 0; //actual position of the carriage
int setPos = 0; //position to which the carriage should move
void setup() {
//MosFet Transistor switch off
digitalWrite(N_FET_Q2, LOW);
digitalWrite(P_FET_Q3, LOW);
digitalWrite(N_FET_Q4, LOW);
digitalWrite(P_FET_Q1, LOW);
//define pinMode for outputs signals
pinMode(P_FET_Q1, OUTPUT);
pinMode(N_FET_Q2, OUTPUT);
pinMode(P_FET_Q3, OUTPUT);
pinMode(N_FET_Q4, OUTPUT);
//define pinMode for input signals
//buttons for different positions
pinMode(MovPos0m, INPUT);
pinMode(MovPos5m, INPUT);
pinMode(MovPos7m, INPUT);
pinMode(MovPos10m, INPUT);
//preswitches to slow down the velocity
pinMode(PreSw0m, INPUT);
pinMode(PreSw5m, INPUT);
pinMode(PreSw7m, INPUT);
pinMode(PreSw10m, INPUT);
//limitswitches to stop at defined distance
pinMode(LiSw0m, INPUT);
pinMode(LiSw5m, INPUT);
pinMode(LiSw7m, INPUT);
pinMode(LiSw10m, INPUT);
}
void driveForward() {
digitalWrite(P_FET_Q1, HIGH);
delay(10);
//acceleration
//Drive Forward (0% -> 99%)
while (PWM < 255) {
analogWrite(N_FET_Q4, PWM);
PWM++;
delay(10);
}
PWM = 0;
delay(10);
}
void driveBackward(){
digitalWrite(P_FET_Q3, HIGH);
delay(10);
//acceleration
//Drive Backward (0% -> 99%)
while (PWM < 255) {
analogWrite(N_FET_Q2, PWM);
PWM++;
delay(10);
}
PWM = 0;
delay(10);
}
void decForward() {
//declaration
//Drive Backward (99% -> 0%)
while (PWM > 0) {
analogWrite(N_FET_Q2, PWM);
PWM--;
delay(10);
}
}
void decBackward () {
//declaration
//Drive Forward (99% -> 0%)
while (PWM > 0) {
analogWrite(N_FET_Q4, PWM);
PWM--;
delay(10);
}
}
void stopMotor () {
delay(10);
digitalWrite(P_FET_Q1, LOW);
delay(10);
digitalWrite(N_FET_Q2, LOW);
delay(10);
digitalWrite(P_FET_Q3, LOW);
delay(10);
digitalWrite(N_FET_Q4, LOW);
delay(1000);
}
void loop() {
// Taster für 0m wurde gedrückt
if (digitalRead(MovPos0m==LOW)) {
setPos = 0;
if (actPos < setPos) {
while (PreSw0m==LOW) {
driveForward();
}
while (LiSw0m==LOW) {
decForward();
}
}
else {
while (PreSw0m==LOW) {
driveBackward();
}
while (LiSw0m==LOW) {
decForward();
}
}
stopMotor();
actPos = 0;
}
//Taster für 5m wurde gedrückt
if (digitalRead(MovPos5m==LOW)) {
setPos = 5;
if (actPos < setPos) {
while (PreSw5m==LOW) {
driveForward();
}
while (LiSw5m==LOW) {
decForward();
}
}
else {
while (PreSw5m==LOW) {
driveBackward();
}
stopMotor();
delay(10);
while (LiSw5m==LOW) {
decForward();
}
}
stopMotor();
actPos = 5;
}
// Taster für 7m wurde gedrückt
if (digitalRead(MovPos7m==LOW)) {
setPos = 7;
if (actPos < setPos) {
while (PreSw7m==LOW) {
driveForward();
}
while (LiSw7m==LOW) {
decForward();
}
}
else {
while (PreSw7m==LOW) {
driveBackward();
}
while (LiSw7m==LOW) {
decForward();
}
}
stopMotor();
actPos = 7;
}
// Taster für 10m wurde gedrückt
if (digitalRead(MovPos10m==LOW)) {
setPos = 10;
if (actPos < setPos) {
while (PreSw10m==LOW) {
driveForward();
}
while (LiSw10m==LOW) {
decForward();
}
}
else {
while (PreSw10m==LOW) {
driveBackward();
}
while (LiSw10m==LOW) {
decForward();
}
}
stopMotor();
actPos = 10;
}
}