second tcrt5000 reflective sensor will not trigger reverse relay.

Hello all,

Project explanation
Using an Rboard (atmega328) I am trying to use a reflective sensor that turns on reverse motor relay and second reflective sensor would turn on forward relay.

project progress
I have sucessfully written a program that turned on the reverse relay when I ran my finger in front of tcrt1sensor. Then modified this program to add the second sensor and relay that does not work.
The reverse sensor still works as it did but the other relay will not come on. I have tested the sensor, if I swap the two sensors it will operate the rev. relay. So the mechanical aspect has been proofed. I have included code below Thanks for your 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); 
 }
}
}

Ouch! Won't this have the potential to be a dead short if both sensors detect a reflective media at the same time thus energizing both the forward and reverse relays at the same time? I think some code is needed to prevent that scenario. Also, relays are mechanical and are not instantaneous. One relay will be slower than the other to operate; therefore one may still have it's contacts closed (although the coil is no longer energized) when the other relay closes it's contacts , i.e., same same dead short scenario. Some delay is needed to ensure the previously energized relay has opened it's contacts before energizing the other relay.

  • Scotty

No this circuit actully will not short circuit. It is a circuit that I have come up with using two relays to have three modes forward/brake/and/reverse.
if both relays came on at the same time you would have the same 12 volt + going to each side of the motor. and when both relays are off it has - 12volts on each side. When I get more time I will make a little diagram and post. In the mean time can anyone help me with my code. it may be simple for some I am new to the arduino IDE.