Jay,
The variable stateMotor keeps track of whether the motor is On or Off. It doesn't directly control the relay. That's done with the digitalWrite(RELAY2, x) statement.
To make things a bit more clear, I'll rename stateMotor to be motorOn. motorOn is a boolean variable with the values true or false.
// state variable declaration
bool motorOn = false;
When the program comes out of reset, motorOn is false. At the start of each pass through the loop the program looks at the motorOn variable. If motorOn is false (motor is Off), it looks at the Lower Sensor. If it needs to turn on, the program sets motorOn to true and turns the relay On with digitalWrite(RELAY2, HIGH). If the motor doesn't need to turn on, the program skips over the "else" statement to the bottom then jumps back to the top of the loop. To get into the "else" statement, motorOn must be true (motor is On) where it looks at the Upper Sensor. If the motor needs to turn Off, motorOn is set false and the relay is turned off with digitalWrite(RELAY2, LOW). Then the program jumps back o the start of the loop to repeat the process.
Since the comment system doesn't allow tabs, in the code below, I used 4 spaces for each tab indentation
#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 variable
bool motorOn = false;
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
// this statement was missing on the original programs
digitalWrite(RELAY2, LOW); // make sure the relay is Off after reset
}
void loop() {
if(motorOn == false) {
if(digitalRead(SENSORLOW) == HIGH) { // turn On if Low sensor shows empty
digitalWrite(RELAY2, HIGH);
motorOn = true;
}
}
else {
if(digitalRead(SENSORHIGH == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY2, LOW);
motorOn = false;
}
}
}
I've been using Arduino Nanos. They are powerful and cheap (about $4 each). So far I've built a stepper motor controller to replace the gear train on my G4000 lathe for feeding. One of these days I'll finish the version that will be able to do to threading in metric and US. I also just finished a DRO for the cross slide. It uses a $10 Harbor Freight caliper. It calculates diameters directly simplifying my work. It's working great on my current project which involves tight tolerances for ball bearings.
Glen
The variable stateMotor keeps track of whether the motor is On or Off. It doesn't directly control the relay. That's done with the digitalWrite(RELAY2, x) statement.
To make things a bit more clear, I'll rename stateMotor to be motorOn. motorOn is a boolean variable with the values true or false.
// state variable declaration
bool motorOn = false;
When the program comes out of reset, motorOn is false. At the start of each pass through the loop the program looks at the motorOn variable. If motorOn is false (motor is Off), it looks at the Lower Sensor. If it needs to turn on, the program sets motorOn to true and turns the relay On with digitalWrite(RELAY2, HIGH). If the motor doesn't need to turn on, the program skips over the "else" statement to the bottom then jumps back to the top of the loop. To get into the "else" statement, motorOn must be true (motor is On) where it looks at the Upper Sensor. If the motor needs to turn Off, motorOn is set false and the relay is turned off with digitalWrite(RELAY2, LOW). Then the program jumps back o the start of the loop to repeat the process.
Since the comment system doesn't allow tabs, in the code below, I used 4 spaces for each tab indentation
#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 variable
bool motorOn = false;
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
// this statement was missing on the original programs
digitalWrite(RELAY2, LOW); // make sure the relay is Off after reset
}
void loop() {
if(motorOn == false) {
if(digitalRead(SENSORLOW) == HIGH) { // turn On if Low sensor shows empty
digitalWrite(RELAY2, HIGH);
motorOn = true;
}
}
else {
if(digitalRead(SENSORHIGH == LOW) { // turn off if High sensor shows full
digitalWrite(RELAY2, LOW);
motorOn = false;
}
}
}
I've been using Arduino Nanos. They are powerful and cheap (about $4 each). So far I've built a stepper motor controller to replace the gear train on my G4000 lathe for feeding. One of these days I'll finish the version that will be able to do to threading in metric and US. I also just finished a DRO for the cross slide. It uses a $10 Harbor Freight caliper. It calculates diameters directly simplifying my work. It's working great on my current project which involves tight tolerances for ball bearings.
Glen