Help me debounce a relay contact


hi

this first project , works fine need have run signal on relay contact,

now realise (after spurious results) problems experiencing caused bounce on relay contacts need eliminate.

i have gone through debounce tutorial , spent hours looking round net not getting gist of coding, sketch being first ever  attempt @ sort of coding.

the project opens , closes flap driving motor , has 2 position switches tell if open or closed , direction move based on switch , run signal logic.
a run signal tells way move.

so in code below need debounce run signal don't want set initial states unless there no other way.

any appreciated

code: [select]
// named constants switch , motor pins
const int flapclosed = 2; // number of pin flap closed switch
const int flapopen = 4; // number of pin flap open switch
const int run = 5; // number of pin open or close external signal
const int motor = 12; // number of motor output pin


  //setup channel a
 
 void setup() {
  // initialize motor pin output:
  pinmode(motor, output);
  // initialize flap switch pins input:
  pinmode(flapclosed, input_pullup);
  pinmode(flapopen, input_pullup);
  pinmode(run, input_pullup);
  //brake motor
  pinmode(9, output);
}
 
void loop(){
//get signal status:
int closed = digitalread(flapclosed);
int open = digitalread(flapopen);
int run = digitalread(run);

      //flap closed , run contact closed
  if (closed == high && open == low && run == low) {
     // open flap:
     digitalwrite(motor, high); //establishes forward direction of channel a
     digitalwrite(9, low);   //disengage brake channel a
     analogwrite(3, 255);   //spins motor on channel @ full speed
     
  }
     //flap open , run switch open
  else if(closed == low && open == high && run == high) {
     // close flap
     digitalwrite(motor, low); //establishes backward direction of channel a
     digitalwrite(9, low);   //disengage brake channel a
     analogwrite(3, 255);   //spins motor on channel @ full speed
  }
 
      //stop motor if flap closed
    else if (closed == high && open == low && run == high) {
     // stop motor
     digitalwrite(9, high);
    }
    //stop motor if flap open
    else if (closed == low && open == high && run == low) {
     // stop motor
     digitalwrite(9, high);
    }

}

hi,

you handle input signal coming relay if coming button pressed.

you have 2 major choices deal debouncing:
1. coding of "relay read states" , small delays (look @ this site.)
2. of debouncing library (e.g. bounce library)


Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > Help me debounce a relay contact


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'