So I working on a project that turns a pump on and off depending on a sensor state however I am running into a problem with the sensor bouncing between on and off quickly. The first thing I tried was putting a capacitor on the signal line to try and filter out some of the noise, this certainly slowed down the flickering of my indicator led however it did not remove it to an acceptable level, now one option would be to put a bigger cap on it but is there a way to do this in the program with out using delay()?
Here is the code I have so far with out any debounce written in, it is based off the zoetrope code from the starter set.
const int SensorPin = 13; // the number of the sensor pin
const int MotorPin = 9; // the number of the motor pin
int SensorState = 0; // variable for reading the sensor's status
void setup() {
// initialize the motor pin as an output:
pinMode(MotorPin, OUTPUT);
// initialize the sensor pin as an input:
pinMode(SensorPin, INPUT);
}
void loop(){
// read the state of the switch value:
SensorState = digitalRead(SensorPin);
// check if the switch is pressed.
if (SensorState == HIGH) {
// turn motor on:
digitalWrite(MotorPin, HIGH);
}
else {
// turn motor off:
digitalWrite(MotorPin, LOW);
}
}
Last edited by adafruit_support_bill on Tue Jul 28, 2015 6:00 pm, edited 1 time in total.
Reason:please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.
Track the last state change with millis() and ignore any state transitions within some time window. For something like a pump motor, you can probably use a fairly long time window.
adafruit_support_bill wrote:Track the last state change with millis() and ignore any state transitions within some time window. For something like a pump motor, you can probably use a fairly long time window.
const int SensorPin = 13; // the number of the switch pin
const int MotorPin = 9; // the number of the motor pin
int SensorState = 0; // variable for reading the switch's status
int MotorState = 0;
int LastStateChange = 0;
void setup() {
// initialize the motor pin as an output:
pinMode(MotorPin, OUTPUT);
// initialize the switch pin as an input:
pinMode(SensorPin, INPUT);
}
void loop(){
// read the state of the switch value:
SensorState = digitalRead(SensorPin);
if ((SensorState != MotorState) && (millis() - LastStateChange > 5000))
{
MotorState = SensorState;
digitalWrite(MotorPin, MotorState);
LastStateChange = millis();
}
}
so that code certainly decreased the amount of flickering i got out of my indicator led ( it is hooked up on the same output as the line that goes to the pump) however I was under the impression that the above code was only supposed to allow a state change if the input read the same state after an interval set in the code. i have the interval set for 10 seconds but I a state change on the output quicker than that.
code was only supposed to allow a state change if the input read the same state after an interval set in the code.
It only does a motor state change if the sensor state is different than the motor state and 5 seconds has elapsed.
yes sorry I miss stated what it does, however that does not change the fact that I was getting a state change faster than the programmed interval. I believe the problem might be in the fact that I did not use the long command when I initialized the LastStateChange variable. I noticed in the arduino debounce example that they use long whenever they are going to be counting with millis().
const int SensorPin = 13; // the number of the switch pin
const int MotorPin = 9; // the number of the motor pin
int SensorState = 0; // variable for reading the switch's status
int MotorState = 0;
long LastStateChange = 0;
void setup() {
// initialize the motor pin as an output:
pinMode(MotorPin, OUTPUT);
// initialize the switch pin as an input:
pinMode(SensorPin, INPUT);
}
void loop(){
// read the state of the switch value:
SensorState = digitalRead(SensorPin);
if ((SensorState != MotorState) && (millis() - LastStateChange > 2500))
{
MotorState = SensorState;
digitalWrite(MotorPin, MotorState);
LastStateChange = millis();
}
}
with the LastStateChange initialized with the command long
const int SensorPin = 13; // the number of the switch pin
const int MotorPin = 9; // the number of the motor pin
const int LedPin = 8;
int SensorState = 0; // variable for reading the switch's status
int MotorState = 0;
long LastStateChange = 0;
void setup() {
// initialize the motor pin as an output:
pinMode(MotorPin, OUTPUT);
// initialize the switch pin as an input:
pinMode(LedPin, OUTPUT);
pinMode(SensorPin, INPUT);
}
void loop(){
// read the state of the switch value:
SensorState = digitalRead(SensorPin);
if ((SensorState != MotorState) && (millis() - LastStateChange > 5000))
{
MotorState = SensorState;
digitalWrite(MotorPin, MotorState);
digitalWrite(LedPin, MotorState);
LastStateChange = millis();
}
}
I am running into a bug where the state gets stuck and won't change. I have observed this in both directions (pump not turning on and pump not turning off). Does anyone have any thoughts on what might be causing this particular bug?
On a completely different non programming note, does anyone have any idea why my pump struggles to run through a mosfet but not when hooked directly to power and ground when the fet is hooked to the same source?
I don't see any obvious faults in the code. You might try adding in a Serial.println statement to display the sensor value - just to make sure it is giving you the output you are expecting from it.