- Joined
- Dec 3, 2013
- Messages
- 1,844
Not sue if this is the correct forum but here goes. I'm hoping there are some Arduino guru's here.
I trying to setup a feed motor that uses the state of two IR sensors to control a relay to cycle a motor on and off. The intent is to give some head room in the feed system to keep the motor from turning on/off constantly. The sensors will be spaced apart, high and low. When the space between the two sensors is full (both beams broken) the motor is of. As the system feeds the the top beam will become unbroken but the motor stays off until the lower beam is unbroken. When both beams are unbroken then the motor turns on. As the feed tube fills the lower beam will become broken but the motor stays on until the feed tube is full (both beams broken) then the motor turns off.
I have a single sensor working but I'm struggling on how to incorporate the second sensor.
Code so far.
/*
Feeder
*/
#define LEDPIN 12 //Motor staus LED is on pin 12
#define RELAY2PIN 10 //Feeder relay is on pin 10
#define SENSOR2PIN 2 //Feeder IR Sensor2 is on pin 2
#define SENSOR3PIN 3 //Feeder IR Sensor3 is on pin 3
// variables will change:
int sensor2State = 0, lastState=0; // variable for reading the sensor2 status
int sensor3State = 0, lastState=0; // variable for reading the sensor3 status
void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the RELAY2PIN pin as an output:
pinMode(RELAY2PIN, OUTPUT);
// initialize the sensor2 pin as an input:
pinMode(SENSOR2PIN, INPUT);
// initialize the sensor3 pin as an input:
pinMode(SENSOR3PIN, INPUT);
digitalWrite(SENSOR2PIN, HIGH); // turn on the pullup
digitalWrite(SENSOR3PIN, HIGH); // turn on the pullup
}
void loop(){
// read the state of the sensor2 value:
sensorBF2State = digitalRead(SENSOR2PIN);
// read the state of the sensor3 value:
sensorBF3State = digitalRead(SENSOR3PIN);
// check if the sensor3 beam is broken
// if it is, the sensor3State is LOW:
if (sensor3State == LOW) {
// turn RELAY2PIN off:
digitalWrite(RELAY2PIN, LOW);
}
else {
// turn RELAY2PIN on:
digitalWrite(RELAY2PIN, HIGH);
}
I trying to setup a feed motor that uses the state of two IR sensors to control a relay to cycle a motor on and off. The intent is to give some head room in the feed system to keep the motor from turning on/off constantly. The sensors will be spaced apart, high and low. When the space between the two sensors is full (both beams broken) the motor is of. As the system feeds the the top beam will become unbroken but the motor stays off until the lower beam is unbroken. When both beams are unbroken then the motor turns on. As the feed tube fills the lower beam will become broken but the motor stays on until the feed tube is full (both beams broken) then the motor turns off.
I have a single sensor working but I'm struggling on how to incorporate the second sensor.
Code so far.
/*
Feeder
*/
#define LEDPIN 12 //Motor staus LED is on pin 12
#define RELAY2PIN 10 //Feeder relay is on pin 10
#define SENSOR2PIN 2 //Feeder IR Sensor2 is on pin 2
#define SENSOR3PIN 3 //Feeder IR Sensor3 is on pin 3
// variables will change:
int sensor2State = 0, lastState=0; // variable for reading the sensor2 status
int sensor3State = 0, lastState=0; // variable for reading the sensor3 status
void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the RELAY2PIN pin as an output:
pinMode(RELAY2PIN, OUTPUT);
// initialize the sensor2 pin as an input:
pinMode(SENSOR2PIN, INPUT);
// initialize the sensor3 pin as an input:
pinMode(SENSOR3PIN, INPUT);
digitalWrite(SENSOR2PIN, HIGH); // turn on the pullup
digitalWrite(SENSOR3PIN, HIGH); // turn on the pullup
}
void loop(){
// read the state of the sensor2 value:
sensorBF2State = digitalRead(SENSOR2PIN);
// read the state of the sensor3 value:
sensorBF3State = digitalRead(SENSOR3PIN);
// check if the sensor3 beam is broken
// if it is, the sensor3State is LOW:
if (sensor3State == LOW) {
// turn RELAY2PIN off:
digitalWrite(RELAY2PIN, LOW);
}
else {
// turn RELAY2PIN on:
digitalWrite(RELAY2PIN, HIGH);
}