I am working on a project in IDE that should use a float switch to sense water level and use stepper motor to close a valve when the float switch reaches a certain level. The water will be slowly draining from the container, and when the water level goes down and float switch reaches the bottom of the sensor, the stepper motor needs to reverse and re-open the valve. I've got the code ready, but it is not working. I asked my TA and the computer lab folks for help, but they are all new to IDE and have not been able to help much. Hoping to find a little feedback here. Below is the code that I have:
#include <Stepper.h>;
// sets constant intervals. sPR sets how far a revolution is for the stepper
const int stepsPerRevolution = 2052 ;
// sets the pin that checks the float switch
const int analogInPin = 0;
// resets clockwise rotation and counterclockwise. Set during later loops.
int rotationCW = 0;
int rotationCCW = 0;
// variable that corresponds to how far the switch has moved.
int floatSwitch = 0;
int location = 1;
// sets variable for stepper
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
// setup for more stepper motor variables
void setup() {
myStepper.setSpeed(15);
Serial.begin(9600);
}
// loop to read the digital pin that the floatswitch is connected to
void loop() {
floatSwitch == digitalRead(6);
Serial.print("floatswitch = ");
Serial.println(floatSwitch);
delay(1000);
// if the floatswitch is on, this turns the gear to reduce water flow
if (floatSwitch == 1 && location == 1) {
rotationCW = 0.5 * stepsPerRevolution;
Serial.println("clockwise");
myStepper.step(rotationCW);
location == (2);
delay(500);
Serial.println("At Location 1");
}
// if the floatswitch is turned off and the gear has been moved, this moves the gear back to
// return it to its regular flow rate.
else if (floatSwitch == 0 && location == 2); {
rotationCCW = -0.5 * stepsPerRevolution;
Serial.println("counterclockwise");
myStepper.step(rotationCCW);
location == (1);
delay(500);
Serial.println("At Location 2");
}
}
I would probably use a geared motor with limit switches or a servo rather than a stepper which may struggle with torque and also needs something ( limit switch or stop ) to be sure if it’s position .
It is a simply white plastic donut ring that slides up and down a little column with a sensor inside to register 'on' or 'off' when it reaches the top or slides down to the bottom.
I removed the double = sign, and this has fixed the program not registering the float switch, but the stepper motor is not behaving as expected. It just goes back and forth between location 1 and 2 no matter the value for the float switch.
#include <Stepper.h>;
// sets constant intervals. sPR sets how far a revolution is for the stepper
const int stepsPerRevolution = 2052 ;
// sets the pin that checks the float switch
const int analogInPin = 0;
// resets clockwise rotation and counterclockwise. Set during later loops.
int rotationCW = 0;
int rotationCCW = 0;
// variable that corresponds to how far the switch has moved.
int floatSwitch = 0;
int location = 1;
// sets variable for stepper
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
// setup for more stepper motor variables
void setup() {
myStepper.setSpeed(15);
Serial.begin(9600);
}
// loop to read the digital pin that the floatswitch is connected to
void loop() {
floatSwitch = digitalRead(6);
Serial.print("floatswitch = ");
Serial.println(floatSwitch);
delay(1000);
// if the floatswitch is on, this turns the gear to reduce water flow
if (floatSwitch == 1 && location == 1) {
rotationCW = 0.5 * stepsPerRevolution;
Serial.println("clockwise");
myStepper.step(rotationCW);
location = 2;
Serial.print("location = ");
delay(500);
Serial.println("At Location 1");
}
// if the floatswitch is turned off and the gear has been moved, this moves the gear back to
// return it to its regular flow rate.
else if (floatSwitch == 0 && location == 2); {
rotationCCW = -0.5 * stepsPerRevolution;
Serial.println("counterclockwise");
myStepper.step(rotationCCW);
location = 1;
Serial.print("location = ");
delay(500);
Serial.println("At Location 2");
}
else if (floatSwitch == 1 && location == 2 {
delay(1000)
rotationCCW = 0
myStepper.step(rotationCCW);
}
}
}
How have you got your float switch wired, between pin 6 and gnd or pin 6 and 5V?
If between pin 6 and 5V, have you got a 10K resistor between pin 6 and gnd to pull the pin LOW when the switch is open?
OR
If between pin 6 and gnd, have you got a 10K resistor between pin 6 and 5Vto pull the pin HIGH when the switch is open?
Can I suggest you give pin 6 a name such as floatPin , it will help with readability of your code.
Are you only using one float switch?
Please post a link to specs/data of the switch.
Can you please post a circuit diagram of your complete project so we can see your component layout.
Include your power supply, just a hand drawn schematic will be fine unless you have a CAD.