Camera slider auto reverse, with limit switches / kill switch

But I still have a problem with this because it looks to me like there would have to be a case GOING_CCW: { which would have duplicate code.

Yes, and a STOPPED case also. I am doing a step by step help here. The 3 lines controling the motor could be placed in a function, each state calling it with different arguments... And I have not yet talked about debouncing.

jbellavance:
Yes, and a STOPPED case also. I am doing a step by step help here.

Fair enough. I admire your perseverance.

When I am learning something as an adult I much prefer to be shown (or told about) the end goal up front so that I can understand the purpose of the journey. YMMV

...R

Yes, That is why I presented the complete switch (case) construct. Maybe I was not clear enough of my intentions though :wink:

Thanks a lot!

I have finished my code, and appreciate all the comments.
Now I have a working code with variable speed control. I needed to map the potmeter to prevent stalling the gearmotor. Now i can start over again, trying to make a state machine :slight_smile:

const int motor = 12;                 // motor channel A on pin 12
const int brake = 9;                  // brake channel A on pin 9
const int limitBEGIN = 4;             // limit switch BEGIN on pin 4
const int limitEND = 5;               // limit switch END on pin 5
const int killSwitch = 6;             // killSwitch on pin 6
const int motorSpeed = A5;

int currentSensing = A0;              // read current on motor
int limitBeginState = 0;              // variable to hold status
int limitEndState = 0;                // variable to hold status
int killSwitchState = 0;              // variable to hold status
int motorSpeedValue = 0;

void setup() {
  pinMode(motor, OUTPUT);             // initiates motor channel A
  pinMode(brake, OUTPUT);             // initiates brake channel A
  pinMode(limitBEGIN, INPUT);         // limit switch BEGIN
  pinMode(limitEND, INPUT);           // limit switch END
  pinMode(killSwitch, INPUT);         // Brake switch channel A
  pinMode(motorSpeed, INPUT);
  Serial.begin(9600);
  Serial.println("Camera slider with limit and kill switch, variable speed:\n");
}
void loop() {
  // read the state of switch:
  limitBeginState = digitalRead(limitBEGIN);
  limitEndState = digitalRead(limitEND);
  killSwitchState = digitalRead(killSwitch);
  {
    motorSpeedValue = analogRead(motorSpeed);
    motorSpeedValue = map(motorSpeedValue, 0, 1023, 130, 255);
    analogWrite(3, motorSpeed);
  }

  if (killSwitchState == HIGH) {                                    // brake out of while loop and brake the motor
    do {
      digitalWrite(brake, HIGH);
      killSwitchState = digitalRead(killSwitch);                    // check switches
    } while (killSwitchState == HIGH);
    digitalWrite(brake, LOW);                                       // brake off
  }
  if (limitBeginState == LOW && limitEndState == LOW) {             // limit switches LOW and LOW
    do {
      digitalWrite(motor, LOW);                                     // turn motor on clockwise:
      digitalWrite(brake, LOW);                                     // brake off
      analogWrite(3, motorSpeedValue);                              // full speed
      limitBeginState = digitalRead(limitBEGIN);                    // check switches
      limitEndState = digitalRead(limitEND);                        // check switches
      killSwitchState = digitalRead(killSwitch);                    // check switches
      {
        motorSpeedValue = analogRead(motorSpeed);                   // check and map motor speed
        motorSpeedValue = map(motorSpeedValue, 0, 1023, 130, 255);
        analogWrite(3, motorSpeed);
      }
    } while (limitEndState == LOW && killSwitchState == LOW);       // do this until limitEndState changes or killSwitch is triggered
    limitBeginState = digitalRead(limitBEGIN);                      // check switches
    limitEndState = digitalRead(limitEND);                          // check switches
    killSwitchState = digitalRead(killSwitch);                      // check switches
    {
      motorSpeedValue = analogRead(motorSpeed);                     // check and map motor speed
      motorSpeedValue = map(motorSpeedValue, 0, 1023, 130, 255);
      analogWrite(3, motorSpeed);
    }
  }
  if (limitBeginState == LOW && limitEndState == HIGH) {            // limit switches LOW and HIGH
    do {
      digitalWrite(motor, LOW);                                     // turn motor on clockwise:
      digitalWrite(brake, LOW);                                     // brake off
      analogWrite(3, motorSpeedValue);                              // full speed
      limitBeginState = digitalRead(limitBEGIN);                    // check switches
      limitEndState = digitalRead(limitEND);                        // check switches
      killSwitchState = digitalRead(killSwitch);                    // check switches
      {
        motorSpeedValue = analogRead(motorSpeed);                   // check and map motor speed
        motorSpeedValue = map(motorSpeedValue, 0, 1023, 130, 255);
        analogWrite(3, motorSpeed);
      }
    } while (limitBeginState == LOW && killSwitchState == LOW);     // do this until limitBeginState changes or killSwitch is triggered
    limitBeginState = digitalRead(limitBEGIN);                      // check switches
    limitEndState = digitalRead(limitEND);                          // check switches
    killSwitchState = digitalRead(killSwitch);                      // check switches
    {
      motorSpeedValue = analogRead(motorSpeed);                     // check and map motor speed
      motorSpeedValue = map(motorSpeedValue, 0, 1023, 130, 255);
      analogWrite(3, motorSpeed);
    }
  }
  if (limitBeginState == HIGH && limitEndState == LOW) {            // limit switches HIGH and LOW
    do {
      digitalWrite(motor, HIGH);                                    // turn motor on counter clockwise
      digitalWrite(brake, LOW);                                     // brake off
      analogWrite(3, motorSpeedValue);                              // full speed
      limitBeginState = digitalRead(limitBEGIN);                    // check switches
      limitEndState = digitalRead(limitEND);                        // check switches
      killSwitchState = digitalRead(killSwitch);                    // check switches
      {
        motorSpeedValue = analogRead(motorSpeed);                   // check and map motor speed
        motorSpeedValue = map(motorSpeedValue, 0, 1023, 130, 255);
        analogWrite(3, motorSpeed);
      }
    } while (limitEndState == LOW && killSwitchState == LOW);       // do this until limitEndState changes or killSwitch is triggered
    limitBeginState = digitalRead(limitBEGIN);                      // check switches
    limitEndState = digitalRead(limitEND);                          // check switches
    killSwitchState = digitalRead(killSwitch);                      // check switches
    {
      motorSpeedValue = analogRead(motorSpeed);                     // check and map motor speed
      motorSpeedValue = map(motorSpeedValue, 0, 1023, 130, 255);
      analogWrite(3, motorSpeed);
    }
  }
}

Good show,

You will find that the number of lines needed to do the tasks will be cut in half using a state machine.

Jacques