I am having programming difficulties with Controlling motors with limits.

I have a dc motor that I am trying to start and stop with a limit. I am turning the motor on by using two DPST relays. on both relays the N.C. terminal has +12V terminated and the N.O. contact for both relays have -12V connected to them. when both relays are deenergized at the same time it will produce a -12V signal on the center pole in which I have the motor legs connected to. turn relay a on and it gives one leg a positive charge and goes forward. if you energize relay B and release A the polarities will change and the motor will run the other direction. release both and the -12 volts on both legs produce a break. (a diagram would have been easier but I do not have that option right now). I have two limit swithces that I have connected to my board. I got one limit to turn on one relay but the other relay will not come on. I have attached code below.could someone please help!

const int tcrt1Pin = A4; // connected to the TCRT5000 C pin
const int forward = 4; // operates on board relay #4 
const int tcrt2Pin = A5; // connected to the TCRT5000 C pin
const int Reverse = 5; // operates on board relay #3

// variables will change:
int tcrt1State = 0; // variable for reading the TCRT5000 status
int tcrt2State = 1;

void setup() {
// initialize the LED pin as an output:
pinMode(forward, OUTPUT); 
pinMode(Reverse, OUTPUT); 

// initialize the tcrt5000 pin as an input, and turn on the internal pullup resistors:
pinMode(tcrt1Pin, INPUT); 
digitalWrite(tcrt1Pin, HIGH); 
pinMode(tcrt2Pin, INPUT); 
digitalWrite(tcrt2Pin, HIGH);

}
void loop(){
// read the state of the tcrt5000:
tcrt1State = digitalRead(tcrt1Pin);
// check if the tcrt5000 sensor detects something.
// if it is, the tcrtState is high:
if (tcrt1State == LOW) { 
// turn motor off: 
digitalWrite(forward, LOW); 
} 
else {
// turn forward relay on:
digitalWrite(forward, HIGH); 
}
{

 // read the state of the tcrt5000:
 tcrt2State = digitalRead(tcrt2Pin);
 // check if the tcrt5000 sensor detects something.
 // if it is, the tcrtState is high:
 if (tcrt2State == LOW) { 
 // turn motor off: 
 digitalWrite(Reverse, LOW); 
} 
else {
  // turn forward relay on:
  digitalWrite(Reverse, HIGH); 
 }
}
}

Moderator edit: Tags corrected

Even better than a diagram would be an explanation of what is actually not working.

Wouldn't it be better to ensure that you won't activate both relays by deactivating one before activating the other?

if (tcrt1State == LOW) { 
// turn motor off: 
digitalWrite(forward, LOW); 
} 
else {
// turn forward relay on:
digitalWrite(reverse, LOW); //<- ADDED. 
//maybe creating a little delay here too... 
digitalWrite(forward, HIGH); 
}

There are some copy/paste errors in the comments but the code itself looks as if it would turn on each relay according to the corresponding limit switch. You don't say what you want the motor to do, but it seems to me this will probably result in the motor moving away from the limit switch and then stopping as soon as it has cleared the switch. Is that the behaviour you're after?

Are both limit switches actually giving you the values you expect? The easiest way to find out is to initialise the serial port in setup(), and use Serial.println() to output trace messages to the serial monitor telling you what your sketch is doing.

You could halve the amount of code in loop() (and eliminate any possibility of copy/paste bugs) by defining a function that takes the tcrt5000 and relay pin numbers as arguments, and does the reading and outputting. Then you would just call this twice in loop().

Also, it would be better if you were consistent about capitalising the names forward and Reverse.

Guys, thanks for responding I do apologize for not telling you what the problem is. I was in a hurry to get it in and I missed the point. Let's try again.

Only one relay will turn on as the sensor is blocked. as it should. and the second sensor should turn on the other relay but it does not. I did check input data with using serial window. and it does not matter if the relays are on at the same time because it will just insert 12Volts to both sides of the motor windings resulting in a braking action which is what I intended to do. I did try to include some of the call outs in the same line to reduce code and could not get that to work either.

The real difficult is that the English language is a poor substitute for a proper electrical schematic drawing. A schematic drawing is the universal and proper language of electronics. What my appear correct to you might be seen as a simple an obvious mistake, if another set of eyeballs would have a gander at a schematic drawing of what you have built.

Lefty

Your wiring anything close to the below?

zoomkat:
Your wiring anything close to the below?

That looks like a classic circuit I've seen many times. Add DPST contacts to the two limit switches and you can have handy feedback signals to your controller that you have reached a limit of travel and in which direction.

Lefty

The diagram is close, minus the limit switches and diodes. connect the right relay to the motor lead and you have the motor circuit. then add two photo sensors to two different inputs on my Rboard and you have the full circuit. I am 99 percent sure this is not a wiring issue. That is why I posted this in the programming section. I must have a syntax thingy :0 or something wrong.Please tell this rookie what is wrong. I will keep playing with it. This program is a small portion of a larger programming project and I am trying to take baby steps. once I can run the motor in reverse by placing an object in front of sensor 4 and go in reverse and then go forward by interrupting sensor 5 I will expand this program. Thanks!!

weldsmith:
Please tell this rookie what is wrong.

Please let us know whether the switch inputs are giving you the values you expect for both inputs.

If your goal is that the motor will trigger one switch and then reverse until the other switch is triggered, I don't think the current code will achieve that. However, it should operate each relay from the corresponding sensor input. But is it getting the correct inputs?