I am reading a capacitive sensor
Which one?
For my current application Capacitive sensor output fluctuates between logic 1 and 0
That's not the way the capacative sensor library expects capacative sensors to work. Why do you expect 0 or 1?
There is no reason to attach code that can be posted inline.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *solenoid = AFMS.getMotor(1);
const int button = 3;
int buttonstate = LOW; // microswitch state
const int duration = 3000; // duration 3 sec
unsigned int countstart = 0;
unsigned int startFlag = 0;
unsigned long startTime;
unsigned long endTime;
unsigned long elapsedTime = 0;
void setup() {
pinMode(button, INPUT);
AFMS.begin(); // create with the default frequency 1.6KHz
solenoid->setSpeed(225); // Set cleanmotor speed to start, from 0 (off) to 255 (max speed)
solenoid->run(FORWARD); // setup: variable for cleanmotor to run in forward direction
solenoid->run(BACKWARD);
solenoid->run(RELEASE);
}
void loop() {
buttonstate = digitalRead(button); // read the state of the microswitch value: HIGH "on" or LOW "off"
if( buttonstate == HIGH && countstart == 0 ){
countstart = 1;
}
while(countstart == 1) {
buttonstate = digitalRead(button); // read the state of the microswitch value: HIGH "on" or LOW "off"
if ( ( buttonstate == LOW ) && ( startFlag == 0 ) ) { //start new time sequence
startFlag = 1;
startTime = millis();
}
if ( ( buttonstate == LOW ) && ( startFlag == 1 ) ){
endTime = millis();
elapsedTime = endTime - startTime;
}
if ( ( buttonstate == HIGH ) && ( startFlag == 1 ) ){
startFlag = 0;
elapsedTime = 0;
}
if ( elapsedTime > 5000 ) {
delay(2000); //2 sec delay
solenoid->run(BACKWARD);
delay(40);
solenoid->run(RELEASE);
delay(500); //flush duration 0.5 sec
solenoid->run(FORWARD);
delay(40);
solenoid->run(RELEASE);
countstart = 0;
startFlag == 0;
elapsedTime = 0;
buttonstate = LOW;
delay(5000); //wait for 5 sec
break;
}
else {
}
}
}
unsigned int countstart = 0;
I can't even begin to guess what this is supposed to hold. A count? An initial count? Of what?
unsigned long startTime;
unsigned long endTime;
Start of what? End of what?
if( buttonstate == HIGH && countstart == 0 ){
countstart = 1;
}
You REALLY need to look at the state change detection example, and do something when the switch BECOMES pressed, not when the switch IS pressed.
if ( elapsedTime > 5000 ) {
delay(2000); //2 sec delay
Your comment should explain WHY you are stuffing your head in the sand for two seconds. It is OBVIOUS what the code does.
else {
}
You look stupid when you include useless code. That isn't what you want to do, is it?
delay(5000); //wait for 5 sec
Why?
break;
The solenoid valve is not getting any actuation from the Arduino board.
Why not? What do your Serial.print() statements tell you is happening?
Why not? Debugging by guesswork sucks. Debugging with facts is a whole lot better, and easier, too.
Why? What would happen if you left this statement out?