@keyframes fade-in{0%{opacity:0}100%{opacity:1}}@keyframes fade{10%{transform:scale(1, 1)}35%{transform:scale(1, 1.7)}40%{transform:scale(1, 1.7)}50%{opacity:1}60%{transform:scale(1, 1)}100%{transform:scale(1, 1);opacity:0}}[data-language] code,[class^="lang"] code,pre [data-language],pre [class^="lang"]{opacity:0;-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";animation:fade-in 50ms ease-in-out 2s forwards}[data-language] code.rainbow,[class^="lang"] code.rainbow,pre [data-language].rainbow,pre [class^="lang"].rainbow{animation:none;transition:opacity 50ms ease-in-out}[data-language] code.loading,[class^="lang"] code.loading,pre [data-language].loading,pre [class^="lang"].loading{animation:none}[data-language] code.rainbow-show,[class^="lang"] code.rainbow-show,pre [data-language].rainbow-show,pre [class^="lang"].rainbow-show{opacity:1}pre{position:relative}pre.loading .preloader div{animation-play-state:running}pre.loading .preloader div:nth-of-type(1){background:#0081f5;animation:fade 1.5s 300ms linear infinite}pre.loading .preloader div:nth-of-type(2){background:#5000f5;animation:fade 1.5s 438ms linear infinite}pre.loading .preloader div:nth-of-type(3){background:#9000f5;animation:fade 1.5s 577ms linear infinite}pre.loading .preloader div:nth-of-type(4){background:#f50419;animation:fade 1.5s 715ms linear infinite}pre.loading .preloader div:nth-of-type(5){background:#f57900;animation:fade 1.5s 853ms linear infinite}pre.loading .preloader div:nth-of-type(6){background:#f5e600;animation:fade 1.5s 992ms linear infinite}pre.loading .preloader div:nth-of-type(7){background:#00f50c;animation:fade 1.5s 1130ms linear infinite}pre .preloader{position:absolute;top:12px;left:10px}pre .preloader div{width:12px;height:12px;border-radius:4px;display:inline-block;margin-right:4px;opacity:0;animation-play-state:paused;animation-fill-mode:forwards}pre{background-color:#000;word-wrap:break-word;margin:0px;padding:10px;color:#fff;font-size:14px;margin-bottom:20px}pre,code{font-family:'Monaco', 'Menlo', courier, monospace}code span{transition:color 0.6s ease-in}code .red{color:#f50419}code .orange{color:#f57900}code .yellow{color:#f5e600}code .green{color:#00f50c}code .blue{color:#0081f5}code .indigo{color:#5000f5}code .violet{color:#9000f5}code .animate{color:#fff !important}pre{background:#010104}pre .comment{color:#747474}pre .keyword,pre .selector,pre .operator{color:#bc5cff}pre .storage,pre .support{color:#fb2639}pre .string{color:#fff55c}pre .constant{color:#ffad5c}pre .entity{color:#43a6ff}pre .inherited-class,pre .storage{font-style:italic}[data-language="custom"] .comment,.lang-custom .comment,.language-custom .comment{color:#314CAD}[data-language="custom"] .integer,.lang-custom .integer,.language-custom .integer{color:#f5e600}[data-language="custom"] .keyword,.lang-custom .keyword,.language-custom .keyword{color:#f57900}[data-language="html"] .attribute,.lang-html .attribute,.language-html .attribute{color:#fc4959} Easy Mades: Arduino Tutorial - Buttons

Arduino Tutorial - Buttons

This is a very important tutorial for beginners as you can do major projects using this theory. Only thing is that you have to think beyond the normal.

I will explain one by one. first of all shall we collect the necessary parts for this project. You may need following parts.

Let's do the wiring.

Lets start coding.

I have assiged here int variable for 13 pin of arduino (on board led) and I call that pin as led.Then i make the variable

int led = 13;

another variable for the "push button" . I assiged again int variable to Pin number 8 and i call it as button.

int button = 8;

now I want to light the LED therefore I set the pin number 13 (led) as a output.


and also we get the signal from Button to arduino, we set that pin 8 as input.


As i have connected GROUND wire to input pin 8 (button) its current always 0. If is push the button , 5v pin connect with the pin 8, then the current of pin 8 become 1. ( 1 = 5v). In arduino it 5v may be called as "1" or "HIGH" and Ground volt may be called as "0" or "LOW".

Now i want to get the value of pin 8. so i use the following code.

digitalRead(button); if the button is pressed, this will be "1" else "0"

Now you can understand what shall we do. we use "if" command.

if (digitalRead(button)==HIGH) {
digitalWrite(led,HIGH); // we use digitalWrite command to get out put pin voltage 5v or 0.
delay(300);  }// delay(1000) = 1 second. here the programme will stop a while.
digitalWrite(led,LOW);} // off the led

Full code is like this

You can see now if you press the button, LED ON and release the button LED off.
If you press and hold the button LED continuously on and off 

another issue is there. IF you press the button LED on and wait a while due to the delay function. Until the delay time is over the programme do not run the codes. If we have added another code after this button code we can not run both codes simultaneously. 

Due to those matters we have to do some changes to the code.


 * www.easymades.com
 * www.facebooc.com/easymadescom
 * Arduino Button

int led = 13;
int button = 8;
int Bstatus=LOW;
int current;
int newtime;
int x = LOW;
int i =  0;

void setup() {
  // put your setup code here, to run once:

void loop() {
  // put your main code here, to run repeatedly:

Bstatus = digitalRead(button);

//for this purpose we can use millis command

current = millis();

if (digitalRead(button)==HIGH){ // or you can use as 1
//Serial.println("Button pressed");
//delay(1000); // 
if ((current - newtime) > 200){ // change 200 then you can control the speed.
  newtime = current ;
i = i+1;

if (i > 1){i=0;}

if (i == 0) {
    x = LOW;
  else{ x = HIGH;}

//newtime = current ; //if you want to on/off when press and hold the button then 

//Serial.println("Button Button released");

Watch the video for more details

********Your comments are highly appreciated************


Copyright © 2013 Easy Mades
var rainbow = require('rainbow-code'); var highlighted = rainbow.colorSync('var foo = true;', 'javascript');