0% found this document useful (0 votes)
11 views

Push Button Hands-On Document

This document is more important to research

Uploaded by

harinikadevi9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Push Button Hands-On Document

This document is more important to research

Uploaded by

harinikadevi9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Push Button

int ledpin =13; // Define pin 13 for LED

int inpin= 5; // Define pin 5 for Push Button

int val; // Define the variable val

void setup() {

pinMode(ledpin, OUTPUT); // Define LED as Output

pinMode(inpin,INPUT); // Define Button as Input

void loop() {

val = digitalRead(inpin); // Read digital pin 5 level value assigned to


val

if(val == HIGH) // Test button is pressed

digitalWrite(ledpin, HIGH);

else {

digitalWrite(ledpin,LOW);

}
Push button Incremental
// set pin numbers
const byte led1 = 13; // first led is pin 13
const byte led2 = 12; // second led is pin 12
const byte led3 = 11; // third led is pin 11
const byte pushbutton = 8; // pushbutton is pin 8
// variables
unsigned long ledstate = 0; // current state of output pin
byte buttonstate;
byte lastbuttonstate = LOW;
int reading;
void setup()
{
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pushbutton, INPUT);

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
void loop()
{
reading = digitalRead(pushbutton);
Serial.println(ledstate);

if (reading != lastbuttonstate)
{
buttonstate = reading;

if (buttonstate == HIGH)
{
ledstate ++;
if (ledstate == 4){
ledstate = 0;
}
}

if (ledstate == 1){
digitalWrite(led1, HIGH);
}
else digitalWrite(led1, LOW);

if (ledstate == 2){
digitalWrite(led2, HIGH);
}
else digitalWrite(led2, LOW);

if (ledstate == 3){
digitalWrite(led3, HIGH);
}
else digitalWrite(led3, LOW);

lastbuttonstate = reading;
}
}
Push Button for Multiple Device access
#include "OneButton.h" //we need the
OneButton library

OneButton button(A0, true); //attach a button


on pin A0 to the library

#define L1 2
int RLY1 = LOW;

#define L2 3
int RLY2 = LOW;

#define L3 4
int RLY3 = LOW;

void setup() {

pinMode(L1, OUTPUT); // sets the digital


pin as output
pinMode(L2, OUTPUT); // sets the digital
pin as output
pinMode(L3, OUTPUT); // sets the digital
pin as output

button.attachDoubleClick(doubleclick); // link the


function to be called on a doubleclick event.
button.attachClick(singleclick); // link the
function to be called on a singleclick event.
button.attachLongPressStop(longclick); // link the
function to be called on a longpress event.
}

void loop() {

button.tick(); // check the status


of the button

delay(10); // a short wait


between checking the button
} // loop
void doubleclick() { // what happens
when button is double-clicked
RLY1 = ~ RLY1;
digitalWrite(L1,RLY1);
delay(250);
}

void singleclick(){ // what happens


when the button is clicked
RLY2 = ~ RLY2;
digitalWrite(L2,RLY2);
delay(250);
}

void longclick(){ // what happens


when buton is long-pressed
RLY3 = ~ RLY3;
digitalWrite(L3,RLY3);
delay(250);

// turn off the blue Relay


}

You might also like