Elektromotor with VMA03 and arduino Mega

Hi all,

for a project I want to control an electric motor with my arduino mega. For this I use a VMA03 from velleman.
The motor should turn both left and right and the end stops are indicated by 2 pushbuttons. The engine is started by a touch sensor (TTP223B). I can do the running with my code but I have to operate the touch for 4 seconds before giving this signal. The full motor movement from the left ==> right and vice versa also takes ± 4 seconds.

Can I change my code so that I only have to operate the touch for a second and then start the engine?
Another problem is that the engine has no brake. The engine therefore pushes the push button for some time before it is stationary. Can I programmatically stop the engine?

code:

#define ctsPin 7 // Pin for capactive touch sensor
#define eindstop1 8
#define eindstop2 9

int pwm_a = 3;  //PWM control for motor outputs 1 and 2
int dir_a = 2;  //direction control for motor outputs 1 and 2

void setup() {
 pinMode(ctsPin, INPUT);
 pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
 pinMode(dir_a, OUTPUT);
 pinMode(eindstop1, INPUT);
 pinMode(eindstop2, INPUT);
}

void loop() {
 int ctsValue = digitalRead(ctsPin);
 int toetsWaarde1 = digitalRead(eindstop1);
 int toetsWaarde2 = digitalRead(eindstop2);

 if (toetsWaarde1 == LOW && toetsWaarde2 == LOW) {
 digitalWrite (dir_a, HIGH);
 analogWrite(pwm_a, 150);
 } else {
 analogWrite(pwm_a, 0);
 delay(4000);
 }

 if (toetsWaarde1 == LOW && toetsWaarde2 == HIGH && ctsValue == HIGH) {
   digitalWrite (dir_a, LOW);
   analogWrite(pwm_a, 100);
   delay(4000);
 }

 if (toetsWaarde1 == HIGH && toetsWaarde2 == LOW && ctsValue == HIGH) {
   digitalWrite (dir_a, HIGH);
   analogWrite(pwm_a, 100);
   delay(4000);
 }
}

Moderator: Code tags added

The reason everything (!) takes four seconds is that everytime you do anything you have a delay(4000) at the end of it.

So, simply put, could you Change it to 1 second? Sure, Change all delay(4000) to delay(1000).

But, in order to do it right, you Need to Change the entire program.

You could start by reviewing the code in this Forum for blink without delay. You are not blinking, but the technique to be learned in that sample can be used for your Project as well.

You should really also think about moving the Motor WHILE CONTINUALLY TESTING THE END STOPS instead of moving it for x seconds and then testing the end stop.