First Post - Help with delay

This is my first post. I have been playing with this project and can not figure out how to make my motor not delay but to continue.
At the end of the if statement "if (digitalRead(StopSensor) == LOW) {" the motor will not move as needed. I need the motor to move without a time limit, to move on infinitely until a button or switch is pushed.

[code]

int StartButton = 13;
const int forwards = 7;           // Actuator
const int backwards = 6;          // Actuator
int ForwardSwitch = 5;
int ReverseSwitch = 4;
int StopSensor = 2;               // Proximity Sensor

#include <SoftwareSerial.h>           // Wheel Chair Motor
#include <SyRenSimplified.h>          // Wheel Chair Motor
SyRenSimplified SR;                   // Wheel Chair Motor

void setup() {
  pinMode(StartButton, INPUT);
  pinMode(StopSensor, INPUT_PULLUP);            // Proximity Sensor
  pinMode(ForwardSwitch, INPUT);
  pinMode(ReverseSwitch, INPUT);
  pinMode(forwards, OUTPUT);           // Actuator set relay as an output
  pinMode(backwards, OUTPUT);        // Actuator set relay as an output
  SyRenTXPinSerial.begin(9600);         // Wheel Chair Motor
}

void loop() {
  if (digitalRead(ForwardSwitch) == HIGH) {           
    SR.motor(0);                                   // Stop Wheel Chair Motor
    delay(20);
    SR.motor(40);                                  // -127 full reverse. 0 os Stop.  +127 if full forward
    delay(250);
    SR.motor(0);                                   // Stop Wheel Chair Motor
    delay(20);
  }

  if (digitalRead(StartButton) == HIGH) {          
    SR.motor(0);                                   // Stop Wheel Chair Motor
    delay(20);
    SR.motor(40);                                  // -127 full reverse. 0 os Stop.  +127 if full forward
    delay(20);
    SR.motor(0);                                   // Stop Wheel Chair Motor
    delay(20);
  }

  if (digitalRead(StopSensor) == LOW) {      // Proximity Sensor  
    SR.motor(0);                             // Stop Wheel Chair Motor
    delay(20);
    digitalWrite(forwards, LOW);             // Actuator-Activate the relay one direction, they must be different to move the motor
    digitalWrite(backwards, HIGH);           // Actuator
    delay(1000);                             // Actuator
    digitalWrite(forwards, HIGH);            // Actuator-Deactivate both relays to brake the motor
    digitalWrite(backwards, HIGH);           // Actuator-Deactivate both relays to brake the motor
    delay(2000);                             // Actuator
    digitalWrite(forwards, HIGH);            // Actuator
    digitalWrite(backwards, LOW);            // Actuator-Activate the relay the other direction, they must be different to move the motor
    delay(1000);                             // Actuator
    digitalWrite(forwards, HIGH);            // Actuator-Deactivate both relays to brake the motor
    digitalWrite(backwards, HIGH);           // Actuator
    delay(2000);                             // Actuator
//Here is the Problem
    SR.motor(40);                          // -127 full reverse. 0 os Stop.  +127 if full forward
    delay(20);
  }

  if (digitalRead(ReverseSwitch) == HIGH) {      
    SR.motor(0);                                       // Stop Wheel Chair Motor
    delay(20);
    SR.motor(-40);                                     // -127 full reverse. 0 os Stop.  +127 if full forward
    delay(1000);
    SR.motor(0);                                       // Stop Wheel Chair Motor
    delay(20);
  }
}

[/code]

Consider delay() as the F-word.
An educated person would only use that word after hitting his/her thumb with a hammer.
The delay() call stops the processor dead in it's tracks.
Learn to code without using the delay() call.
The "BlinkWithoutDelay" example in the IDE will teach you.
Examples > 02.Digital > BlinkWithoutDelay.
Leo..

1 Like

Welcome.

In addition to blink without delay, or BWOD as it is sometimes called, you should look into

Arduino two things at once

and

Arduino finite state machine

and

Arduino traffic lights

and

Arduino finite state machine traffic lights

for general information and learning about how you can restructure your sketch so that it can be walking and chewing gum and playing the tambourine all apparently at once.

google those terms and spend a few minutes at a few websites to see if anything captures your imagination, is at an appropriate level and if there is a presenter, that s/he is at least tolerable.

If you could provide an overview of the goals for the project, that might help you as much as it would us… by the time you are writing actual code, there shouldn't be too much to think about if your deign is full nailed down and comprehensive.

Not saying you did, but skipping right to pounding out code is way too often seen.

a7

Wawa,
Thank you. I spent the weekend playing with this and it fixed it.

SonOfLeod

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.