Arduino Code Help

jbolt

Active User
H-M Supporter Gold Member
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 sounds like you could simply wire the two sensors in series, both would have to close to complete the circuit. You might even be able to hook them directly to a relay and do away with the Arduino, unless its needed for other functions.
 
Define a state variable for the motor status. If the motor is off, then only turn it on if the low beam is high; then, when the motor is on, only turn it off if the high beam is low.

if (stateMotor == LOW) { // motor is not running
if (stateSensorLow == HIGH) {
digitalWrite(RELAY2PIN, HIGH); // only turn motor on when level drops below SensorLow
stateMotor = HIGH; // track motor state
}
}
else { // motor is running
if (stateSensorHigh == LOW) {
digitalWrite(RELAY2PIN, LOW); // only stop motor when level rises above SensorHigh
stateMotor = LOW; // track motor state
}

It's possible to compact:

if (stateMotor == LOW && stateSensorLow == HIGH) {
digitalWrite(RELAY2PIN, HIGH);
stateMotor = HIGH;
}
else if (stateMotor == HIGH && stateSensorHigh == LOW) {
digitalWrite(RELAY2PIN, LOW); // only stop motor when level rises above SensorHigh
stateMotor = LOW;
}
 
Jay,

jwmelvin's code is right on target. He responded as I was creating essentially the same code. What he created is a state machine with 2 states: MOTOR_ON and MOTOR_OFF. While in the MOTOR_OFF state you just want to check the lower beam. While in the MOTOR_ON state you just want to check the upper beam.

By the way the method to enable the pullup resistors is:
pinMode(SENSOR2PIN, INPUT_PULLUP);
pinMode(SENSOR3PIN, INPUT_PULLUP);

Glen
 
Thanks guys. It didn't occur to me to see it from the motor state. I will noodle this over tonight.

Old dog trying to learn just wish I had more time and brain cells to learn it better. I really love what you can do with these types of platforms but it is light years away from what I have spent my career doing.
 
My IR sensors did not arrive today so I will have to wait until tomorrow to try. I fiddled with the code and it compiles but I'm not sure the variable definitions are correct.

/*
Feeder
*/

#define RELAY2 10 //Feeder relay is on pin 10
#define SENSORLOW 2 //Feeder Low IR Sensor is on pin 2
#define SENSORHIGH 3 //Feeder High IR Sensor is on pin 3

// variables will change:
int stateMotor = 0; // variable for reading the motor status
int stateSensorLow = 0; // variable for reading the sensorLow status
int stateSensorHigh = 0; // variable for reading the sensorHigh status

void setup() {

pinMode(RELAY2, OUTPUT); // initialize the RELAY2 pin as an output:
pinMode(SENSORLOW, INPUT_PULLUP); // initialize the sensorLow pin as an input:
pinMode(SENSORHIGH, INPUT_PULLUP); // initialize the sensorHigh pin as an input:

digitalWrite(SENSORLOW, HIGH); // turn on the pullup
digitalWrite(SENSORHIGH, HIGH); // turn on the pullup
}

void loop(){
if (stateMotor == LOW && stateSensorLow == HIGH) {
digitalWrite(RELAY2, HIGH); // only turn motor on when level drops below SensorLow
stateMotor = HIGH;
}
else if (stateMotor == HIGH && stateSensorHigh == LOW) {
digitalWrite(RELAY2, LOW); // only stop motor when level rises above SensorHigh
stateMotor = LOW;
}
}
 
Good morning Jay,

You're pretty close. Your code doesn't need the stateSensorLow and stateSensorHigh variables. That operation is handled directly with digitalRead(SENSORLOW) and digitalRead(SENSORHIGH) function calls. You could actually eliminate the stateMotor variable as the motor's ON/OFF state is actually stored in the pin. However, I think this code is more clear. Usually a more complex state machine has its state variable in memory.

The code below should work assuming the sensor logic levels are correct.

There are coding style conventions regarding indented code that I've included. Not all coders agree, but this is typical. It's all about readability.

I'm glad to see members of this site getting involved with software. While it looks difficult, it's really not any more complicated than planning the process to make a part on a machine; it's just a different mind-set.

Glen

/*
Feeder
*/

#define RELAY2 10 //Feeder relay is on pin 10
#define SENSORLOW 2 //Feeder Low IR Sensor is on pin 2
#define SENSORHIGH 3 //Feeder High IR Sensor is on pin 3

// state variables
int stateMotor = 0;

void setup() {
pinMode(RELAY2, OUTPUT); // initialize the RELAY2 pin as an output:
pinMode(SENSORLOW, INPUT_PULLUP); // sensor Low with pullup enabled
pinMode(SENSORHIGH, INPUT_PULLUP); // sensor High with pullup enabled
}

void loop() {
if(stateMotor == LOW) {
if(digitalRead(SENSORLOW) == HIGH) { // turn on if Low sensor shows empty
digitalWrite(RELAY2, HIGH);
stateMotor = HIGH;
}
}
else {
if(digitalRead(SENSORHIGH == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY2, LOW);
stateMotor = LOW;
}
}
}
 
Update: the indenting doesn't show up with this commenting system.
 
Good morning Jay,

You're pretty close. Your code doesn't need the stateSensorLow and stateSensorHigh variables. That operation is handled directly with digitalRead(SENSORLOW) and digitalRead(SENSORHIGH) function calls. You could actually eliminate the stateMotor variable as the motor's ON/OFF state is actually stored in the pin. However, I think this code is more clear. Usually a more complex state machine has its state variable in memory.

The code below should work assuming the sensor logic levels are correct.

There are coding style conventions regarding indented code that I've included. Not all coders agree, but this is typical. It's all about readability.

I'm glad to see members of this site getting involved with software. While it looks difficult, it's really not any more complicated than planning the process to make a part on a machine; it's just a different mind-set.

Glen

Thanks Glen. I'd love to know this stuff better. I find it fascinating but its learning another language. I do better if I can immerse myself.

I follow most of the code you have. I was getting definition errors without the sensor variables so that's why they were there. I'm still a little confused as to how the stateMotor variable is tied to the relay?
 
I'm still a little confused as to how the stateMotor variable is tied to the relay?

Manually, in that when you change the relay state you also change the stateMotor variable. I believe it is also possible to do a read operation on the relay output pin but I wasn’t sure so didn’t suggest that.
 
Back
Top