Pages: [1]   Go Down
Author Topic: I am having programming difficulties with Controlling motors with limits.  (Read 692 times)
0 Members and 1 Guest are viewing this topic.
Woodland Washington
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Live life to it's fullest
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!


Code:
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
« Last Edit: December 07, 2012, 07:58:27 am by AWOL » Logged

David Smith

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3262
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
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);
}
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

« Last Edit: December 07, 2012, 08:47:13 am by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Woodland Washington
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Live life to it's fullest
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

David Smith

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 144
Posts: 9611
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your wiring anything close to the below?

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Woodland Washington
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Live life to it's fullest
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-mad 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!!
Logged

David Smith

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: