Troubleshooting code to detect fluctuating capacitive sensor output

Hi!

I am using Arduion Uno Rev 3 and adafruit Motorshield Rev 2.3. I am reading a capacitive sensor output and driving a solenoid valve based on the output. For my current application Capacitive sensor output fluctuates between logic 1 and 0 as my sensor trigger is not consistent, there is break in flow.

My algorithm is if sensor sees a trigger then it waits for logic 0 (absence of trigger). If it does not see any trigger for a certain time (let’s say 3 sec consecutively) it drives the solenoid valve. I am sharing my code here. The solenoid valve is not getting any actuation from the Arduino board. Can you please help me understand if my code development is right?

Thanks for your help.
Nil

capacitive sensor trigger detection.ino (2.17 KB)

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?

Hi PaulS,

Thank you so so much for your reply. I did not use Serial.print() function before but when I did it helped me a lot to debug the issue. I have rephrased the question with more details and clarity and have posted it. Here is the link:

I have checked “state change detection example”. I still need help with the code development.

I will appreciate if you take a look and help.

Thanks a lot again for your time.

Kind regards,
Niloy