Limit Switch Problem

Hi, can anyone help with my limit switch problem,I am running a uno and a TB6612 Driver with RC Transmitter to run a DC Motor. The motor runs fine Fwd/Brake/Bwd, and will not run if limit switch is activated, the problem is once the motor is running it will not stop with limit switch, I am missing something and just can’t think straight any more.

//---------Board-UNO----Driver–TB6612------RC-Transmitter--------DC-Motor----------
const int DIR_A = 5;
const int DIR_B = 4;
const int PWM = 6;
const int Pin = 9; // Limit-Switch
const int Pin2 = 10;// Receiver Pin

void setup()
{ Serial.begin(9600);
pinMode(DIR_A, OUTPUT);
pinMode(DIR_B, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(Pin, INPUT_PULLUP);
pinMode(Pin2, INPUT_PULLUP);
}
void loop() {
unsigned long duration = pulseIn(Pin2, HIGH);
digitalRead(Pin);
digitalRead(Pin2);

if (Pin2, (duration > 1500) && (!digitalRead(Pin))) {
Fwd();
}

if (Pin2, (duration > 1301 && (duration < 1499))) {
Brake();
}
if (Pin2, (duration < 1300) && (!digitalRead(Pin))) {
Bwd();
}
}
void Fwd() {
digitalWrite(DIR_A, HIGH);
digitalWrite(DIR_B, LOW);
analogWrite(PWM, 250);
}
void Brake() {
digitalWrite(DIR_A, LOW);
digitalWrite(DIR_B, LOW);
analogWrite(PWM, 0);
}
void Bwd() {
digitalWrite(DIR_A, LOW);
digitalWrite(DIR_B, HIGH);
analogWrite(PWM, 250);
}

DC-Motor.txt (1.02 KB)

Hi.

//---------Board-UNO----Driver--TB6612------RC-Transmitter--------DC-Motor----------
const int DIR_A = 5;
const int DIR_B = 4;
const int PWM = 6;
const int Pin = 9;  // Limit-Switch
const int Pin2 = 10;// Receiver Pin

void setup()
{ Serial.begin(9600);
  pinMode(DIR_A, OUTPUT);
  pinMode(DIR_B, OUTPUT);
  pinMode(PWM, OUTPUT);
  pinMode(Pin, INPUT_PULLUP);
  pinMode(Pin2, INPUT_PULLUP);
}
void loop() {
  unsigned long duration = pulseIn(Pin2, HIGH);
  digitalRead(Pin2);
  digitalRead(Pin2);

  if (Pin2, (duration > 1500) && (!digitalRead(Pin))) {
    Fwd();
  }

  if (Pin2, (duration > 1301 && (duration < 1499))) {
    Brake();
  }
  if (Pin2, (duration < 1300) && (!digitalRead(Pin))) {
    Bwd();
  }
}
void Fwd() {
  digitalWrite(DIR_A, HIGH);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM, 250);
}
void Brake() {
  digitalWrite(DIR_A, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM, 0);
}
void Bwd() {
  digitalWrite(DIR_A, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM, 250);
}

Perhaps you should create some // comments per line, so you can read back what you’re building, or are trying to build.

And can you explain me what you think this snippet does ?

 unsigned long duration = pulseIn(Pin2, HIGH);
  digitalRead(Pin2);
  digitalRead(Pin2);

  if (Pin2, (duration > 1500) && (!digitalRead(Pin))) {
    Fwd();

The description of your problem makes me think this is the source of that problem,and you’re doing something similar three times.

You know that pulseIn is blocking code, don’t you ?
(Look up blocking code in the (hard to see) search bar on the top right of each page of this forum)

Thanks for your reply. digitalRead(Pin2); twice was an error but not the problem should have been just (Pin) but that is not the problem either. I know pulseIn slows it all down have to read up on it. if I take all the transmitter code out it works fine but i need the transmitter.

Hi.

So.

You're waiting for a HIGH on Pin2, and are measuring the duration of that HIGH state. In order for you to measure that duration, Pin2 needs to go LOW. The next thing you do, is measure Pin2 again. It was LOW about 0.0000000625 seconds ago, what would the value be now ? I'll forget that you re-read Pin2 for the 3rd time, because that was a mistake.

When you do this:

  if (Pin2, (duration > 1500) && (!digitalRead(Pin)))

What do you expect to test ?

Make yourself a truth table, and fill out every possible value you are testing plus the result. An if can only result in TRUE or FALSE. Show that truth table, and someone will tell you what's going wrong here, perhaps me after i've had my well earned night rest.

I know this is completely wrong but I have had a go at the truth table and is as follows=
/*

  • AND && OR|| NOT!
  • 0 0 0 0 0 0 0 1
  • 0 1 0 0 1 1 1 0
  • 1 0 0 1 0 1
  • 1 1 1 1 1 1
    */
    /*if (Pin2, (duration > 1500) && (!digitalRead(Pin))) { when duration Condition = True && Pin ! = True / Fwd = True
    Fwd(); when duration Condition = True && Pin ! = False / Fwd = False
    }else{ when duration Condition = False && Pin ! = True / Fwd = False
    when duration Condition = False && Pin ! = False / Fwd = False
    if (Pin2, (duration > 1301 && (duration < 1499))) {
    Brake(); when Fwd(); Condition = True && Pin ! = True / Fwd = True
    }else{ when Fwd(); Condition = True && Pin ! = False / Fwd = False
    if (Pin2, (duration < 1300) && (!digitalRead(Pin))) {
    Bwd();

Ok, i failed with the request for the truth table because i didn't tell you how you can show a table (or something more similar than what you ended up with with this forum software. That involves a bit of code similar to HTML. So i can't make much of your truth table.

May i ask why you put the Pin2 part is this:

if (Pin2, (duration > 1500) && (!digitalRead(Pin)))

?

By the description in your last post, you seem to know you are testing the value of Pin2, but you are omitting the result of that test immediately after that test. That's what the comma does in that if.. You're testing it, but you aren't using it for the outcome of that if.. So why bothering with that ?

This would do the same:

if ((duration > 1500) && (!digitalRead(Pin))) { // If (duration is TRUE) AND (NOT Pin is TRUE), do the next

Also, did you study pulseIn (click !) ?

If so, did you see this section:

Please also note that if the pin is already high when the function is called, it will wait for the pin to go LOW and then HIGH before it starts counting.

.

Why are you using pulseIn for this anyway ?

It seems I was reading the pin twice The code now works Thanks Mas3 for making me work at it. I guess there will be a better way but this is the code so far:

//---------Board-UNO----Driver–TB6612------RC-Transmitter--------DC-Motor----------
const int DIR_A = 5;
const int DIR_B = 4;
const int PWM = 6;
const int Pin = 9; // Limit-Switch
const int Pin2 = 10;// Receiver Pin

void setup()
{ Serial.begin(9600);
pinMode(DIR_A, OUTPUT);
pinMode(DIR_B, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(Pin, INPUT_PULLUP);
pinMode(Pin2, INPUT_PULLUP);
}
void loop() {
unsigned long duration = pulseIn(Pin2, HIGH);// Measure pulsein length

if (Pin2, (duration > 1500) && (!digitalRead(Pin))) {
digitalWrite(DIR_A, HIGH);
digitalWrite(DIR_B, LOW);
analogWrite(PWM, 250);
}
else {
Brake();
}
if (Pin2, (duration < 1300) && (!digitalRead(Pin))) {
digitalWrite(DIR_A, LOW);
digitalWrite(DIR_B, HIGH);
analogWrite(PWM, 250);
}
}
void Brake() {
digitalWrite(DIR_A, LOW);
digitalWrite(DIR_B, LOW);
analogWrite(PWM, 0);
}

  if (Pin2, (duration < 1300) && (!digitalRead(Pin))) {

That’s a bit obscure.
Is it working for you?

Please remember to use code tags when posting code.

 if (Pin2, (duration > 1500) && (!digitalRead(Pin)))

I guess it works because the comma is throwing away the Pin2 part.

OP: Just to keep yourself from looking stupid any longer, these lines are just plain wrong. The "Pin2, " part has got to go. It is not doing anything other than making you look bad.

Pin2 gone and program still works. Thanks

That can't work correctly.

Besides that you have been told 3 times now that the Pin2 part in your if.. doesn't do anything than consuming time and you'd be better off without it, the new code does this:

Pin2 isn't read any more so trying to use it doesn't make any sense.

Test if you should go forward, in which case you do so. If you do not need to go forward, brake. Test if you should go back, in which case you do so.

So what would happen if you need to go back for a while ?

Let me guess: Are you using a RF remote control, reading its servo output with your pulseIn thingy ?

Hi, Did you build this code in stages? Did you get the limit switches and motor working before adding the remote control?

  • Might I suggest you strip the IR remote out, or begin again.
  • Start with just getting your motor to brake, forward, reverse.
  • Then add the limit switch and get that working.
  • Then add the IR remote.

  • Nameyour pins limitPin and receivPin, not pin and pin2.

  • Is your limit at one end, ie does it stop you going FWD, if so why are you checking it when you go BWD?

  • How have you got your limit switch wired?

  • Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

  • Can you please post a picture of your project or a diagram of how your motor and limit are setup.

  • Have you got a DMM?

Thanks.. Tom... :)

Ok first this is still just a bench project,I have taken a photo of it. I will have a limit switch at either end but for now just have one wired, it is an opto switch. The motor i am using for the test is a hacked old servo, I cut out PCB and chucked it and wired the dc motor direct to the TB6612 leaving the capacitors on the motor; I will not be using this motor but a worm geared dc motor so should have some holding torch. Looking at the code now I don’t think the brake is working but when I put the aileron stick to left motor goes one way for as long as I want bring the stick to centre and motor stops to the right goes other way (the pulsein thingy). Motor will not go if limit is triggered and stops when triggered. From what you all are saying the code is wrong, but it is working so I am pleased with that. Yes I will still work on code and try to get it right. I have also put the code to the stepper motor code and that works great too.

Code:
//---------Board-UNO----Driver–TB6612------RC-Transmitter--------DC-Motor----------
const int DIR_A = 5;
const int DIR_B = 4;
const int PWM = 6;
const int Pin = 9; // Limit-Switch
const int Pin2 = 10;// Receiver Pin

void setup()
{ Serial.begin(9600);
pinMode(DIR_A, OUTPUT);
pinMode(DIR_B, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(Pin, INPUT_PULLUP);
pinMode(Pin2, INPUT_PULLUP);
}
void loop() {
unsigned long duration = pulseIn(Pin2, HIGH);// Measure pulsein length

if ((duration > 1500) && (!digitalRead(Pin))) {
digitalWrite(DIR_A, HIGH);
digitalWrite(DIR_B, LOW);
analogWrite(PWM, 250);
}
else {
Brake();
}
if ((duration < 1300) && (!digitalRead(Pin))) {
digitalWrite(DIR_A, LOW);
digitalWrite(DIR_B, HIGH);
analogWrite(PWM, 250);
}
}
void Brake() {
digitalWrite(DIR_A, LOW);
digitalWrite(DIR_B, LOW);
analogWrite(PWM, 0);
}

The link to upload photo seems to have disappeared

Hi, Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

If you use REPLY rather than QUICK REPLY you will find an attachment facility, use that to attach your files, going off forum to image sites is a pain as we have to put up with all the junk and advertising that goes with them, as I don't need a new wife, mistress, insurance, car, etc.

Thanks.. Tom.. :)