0

Hello i am trying to reverse a dc motor direction upon hitting either limit switch. but noting is working can some one point me in right direction. all example i find use stepper motors and step count.

const int sw2 = 2;          //Up Limit switch
const int sw3 = 3;          //Down Limit switch
const int ic1_2 = 8;        //TA8409S IN2
const int ic1_1 = 9;        //TA8409S IN1
const int enable = 10;      //TA8409S Enable (motor)
int sw2State = 0;
int sw3State = 0;

void setup() {
  pinMode(sw2, INPUT_PULLUP);
  pinMode(sw3, INPUT_PULLUP);
  pinMode(ic1_1, OUTPUT);
  pinMode(ic1_2, OUTPUT);
  pinMode(enable, OUTPUT);
  digitalWrite(enable, 1);
  Serial.begin(9600);
}

void loop() {
  tilt_up();
}

void tilt_up()
{
Serial.print("up Started\n");
  digitalWrite(ic1_1, LOW);
  digitalWrite(ic1_2, HIGH);
  sw2State = digitalRead(sw2);
  if (digitalRead(sw2State == LOW)) {
    tilt_down();
    Serial.print("sw2 pressde\n");
  }
}

void tilt_down()
{
  Serial.print("down Started\n");
  digitalWrite(ic1_1, HIGH);
  digitalWrite(ic1_2, LOW);
  sw3State = digitalRead(sw3);
  if (digitalRead(sw3State == LOW)) {
    tilt_up();
    Serial.print("sw3 Pressed\n");
  }
 }
1

If you want to reverse the movement you sould remember where you are going. This, along with the brackets correction suggested by @MikaelPatel and @NickGammon, leads to this code:

boolean moving_up = true;

void loop() {
    if (moving_up)
        tilt_up();
    else
        tilt_down();
}

void tilt_up()
{
    digitalWrite(ic1_1, LOW);
    digitalWrite(ic1_2, HIGH);
    if (digitalRead(sw2State) == LOW) {
        moving_up = false;
        Serial.println("sw2 Pressed");
        Serial.println("down Started");
    }
}

void tilt_down()
{
    digitalWrite(ic1_1, HIGH);
    digitalWrite(ic1_2, LOW);
    sw3State = digitalRead(sw3);
    if (digitalRead(sw3State) == LOW) {
        moving_up = true;
        Serial.println("sw3 Pressed");
        Serial.println("up Started");
    }
}
  • This also fixes recursion, which would make the processor run out memory eventually. – Gerben Jan 23 '16 at 15:51
  • @Gerben as I also wrote on the other answer, even with the original code there SHOULD not be any recursion problem, since (hopefully) sw2 and sw3 will never be engaged together (and so calling tiltDown - which is only done when sw2 is engaged - should not call tiltUp. Anyway the original code does not have the behavior the poster wanted – frarugi87 Jan 25 '16 at 8:49
1
void loop() {
  tilt_up();
}

void tilt_up()
{
...
  if (digitalRead(sw2State == LOW)) {
    tilt_down();
...
  }
}

...

void tilt_down()
{
...
  if (digitalRead(sw3State == LOW)) {
    tilt_up();
...
  }
 }

You are recursing here. loop calls tilt_up which eventually calls tilt_down which calls tilt_up and so on until you - rather quickly - run out of RAM.

Rewrite so that you are not doing that.


Also see what @MikaelPatel told you in your other question. You have the brackets in the wrong place.

  if (digitalRead(sw2State == LOW)) {

should be:

  if (digitalRead(sw2State) == LOW) {
  • No, he is not recursing too much because (hopefully) sw2 and sw3 will never be engaged together. The problem here is that the motor will move towards sw2, then hit it, reverse for one cycle and then go "up" again. See my answer to get what (I guess) is the behavior the OP wanted – frarugi87 Jan 22 '16 at 22:46
  • @frarugi87 : Your code certainly looks much better, as the main loop now controls which direction it is going. – Nick Gammon Jan 22 '16 at 22:53
0

I got it to work!!! do not know if it is the right way ... but it works. thank you all for your input/advice. here is the new working code:

/*

 */

// set pin numbers:
const int sw2 = 2;           // Down limit
const int sw3 = 3;           // Up Limit
const int ledPin =  13;      // Pwr L.E.D
const int ic1_2 = 8;         // TA8409S IN2
const int ic1_1 = 9;         // TA8409S IN1
const int enable = 10;       // TA8409S Enable (motor)

int sw2State = 0;            // Down Limit State
int sw3State = 0;            // Up   Limit State

void setup() {
  // Setting L.E.D and Motor In, Enable pins as an output:
  pinMode(ic1_1, OUTPUT);
  pinMode(ic1_2, OUTPUT);
  pinMode(enable, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // Setting limit switches pins as inputs
  pinMode(sw2, INPUT_PULLUP);
  pinMode(sw3, INPUT_PULLUP);
  // Enables motor
  digitalWrite(enable, 1);
  //Starts program down
  down();
}

void loop() {
  // reads the state of the limit switches
  sw2State = digitalRead(sw2);
  sw3State = digitalRead(sw3);

  if (sw3State == LOW) {
    down();
  }
  if (sw2State == LOW) {
    up();
  }
}

void up() {
  digitalWrite(ic1_1, LOW);
  digitalWrite(ic1_2, HIGH);
}

void down() {
  digitalWrite(ic1_1, HIGH);
  digitalWrite(ic1_2, LOW);
}

void err() {
  digitalWrite(enable, LOW);
  digitalWrite(ic1_1, LOW);
  digitalWrite(ic1_2, LOW);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.