Modify Existing Code to Switch Relay


newbie here,

how can modify existing code switch relay coil. single click should control 2 different pins, when relaypin1 high relaypin2 should low , visa versa when switch clicked.

can me modify code? thanks

code: [select]
/* 4-way button:  click, double-click, press+hold, , press+long-hold test sketch

by jeff saltzman
oct. 13, 2009

to keep physical interface simple possible, sketch demonstrates generating 4 output events single push-button.
1) click:  rapid press , release
2) double-click:  2 clicks in quick succession
3) press , hold:  holding button down
4) long press , hold:  holding button long time
*/

#define buttonpin 5        // analog input pin use digital input
#define ledpin1 3          // digital output pin led 1
#define ledpin2 4          // digital output pin led 2
#define ledpin3 0          // digital output pin led 3
#define ledpin4 13          // digital output pin led 4

// led variables
boolean ledval1 = false;    // state of led 1
boolean ledval2 = false;    // state of led 2
boolean ledval3 = false;    // state of led 3
boolean ledval4 = false;    // state of led 4

//=================================================

void setup() {
   // set button input pin
   pinmode(buttonpin, input);
   digitalwrite(buttonpin, high );
   // set led output pins
   pinmode(ledpin1, output);
   digitalwrite(ledpin1, ledval1);
   pinmode(ledpin2, output);
   digitalwrite(ledpin2, ledval2);
   pinmode(ledpin3, output);   
   digitalwrite(ledpin3, ledval3);
   pinmode(ledpin4, output);   
   digitalwrite(ledpin4, ledval4);
}

void loop() {
   // button event , act accordingly
   int b = checkbutton();
   if (b == 1) clickevent();
   if (b == 2) doubleclickevent();
   if (b == 3) holdevent();
   if (b == 4) longholdevent();
}

//=================================================
// events trigger

void clickevent() {
   ledval1 = !ledval1;
   digitalwrite(ledpin1, ledval1);
}
void doubleclickevent() {
   ledval2 = !ledval2;
   digitalwrite(ledpin2, ledval2);
}
void holdevent() {
   ledval3 = !ledval3;
   digitalwrite(ledpin3, ledval3);
}
void longholdevent() {
   ledval4 = !ledval4;
   digitalwrite(ledpin4, ledval4);
}
//=================================================
//  multi-click:  1 button, multiple events

// button timing variables
int debounce = 20;          // ms debounce period prevent flickering when pressing or releasing button
int dcgap = 250;            // max ms between clicks double click event
int holdtime = 1000;        // ms hold period: how long wait press+hold event
int longholdtime = 3000;    // ms long hold period: how long wait press+hold event

// button variables
boolean buttonval = high;   // value read button
boolean buttonlast = high;  // buffered value of button's previous state
boolean dcwaiting = false;  // whether we're waiting double click (down)
boolean dconup = false;     // whether register double click on next release, or whether wait , click
boolean singleok = true;    // whether it's ok single click
long downtime = -1;         // time button pressed down
long uptime = -1;           // time button released
boolean ignoreup = false;   // whether ignore button release because click+hold triggered
boolean waitforup = false;        // when held, whether wait event
boolean holdeventpast = false;    // whether or not hold event happened already
boolean longholdeventpast = false;// whether or not long hold event happened already

int checkbutton() {   
   int event = 0;
   buttonval = digitalread(buttonpin);
   // button pressed down
   if (buttonval == low && buttonlast == high && (millis() - uptime) > debounce)
   {
       downtime = millis();
       ignoreup = false;
       waitforup = false;
       singleok = true;
       holdeventpast = false;
       longholdeventpast = false;
       if ((millis()-uptime) < dcgap && dconup == false && dcwaiting == true)  dconup = true;
       else  dconup = false;
       dcwaiting = false;
   }
   // button released
   else if (buttonval == high && buttonlast == low && (millis() - downtime) > debounce)
   {       
       if (not ignoreup)
       {
           uptime = millis();
           if (dconup == false) dcwaiting = true;
           else
           {
               event = 2;
               dconup = false;
               dcwaiting = false;
               singleok = false;
           }
       }
   }
   // test normal click event: dcgap expired
   if ( buttonval == high && (millis()-uptime) >= dcgap && dcwaiting == true && dconup == false && singleok == true && event != 2)
   {
       event = 1;
       dcwaiting = false;
   }
   // test hold
   if (buttonval == low && (millis() - downtime) >= holdtime) {
       // trigger "normal" hold
       if (not holdeventpast)
       {
           event = 3;
           waitforup = true;
           ignoreup = true;
           dconup = false;
           dcwaiting = false;
           //downtime = millis();
           holdeventpast = true;
       }
       // trigger "long" hold
       if ((millis() - downtime) >= longholdtime)
       {
           if (not longholdeventpast)
           {
               event = 4;
               longholdeventpast = true;
           }
       }
   }
   buttonlast = buttonval;
   return event;
}

you know relays in hardware? relays have 3 output pins. can send power common pin 1 or other of output pins (but not both).


Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > Modify Existing Code to Switch Relay


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code