of 1

Arduino Codes

Published on January 2017 | Categories: Documents | Downloads: 3 | Comments: 0
56 views

Comments

Content

PULL UP AND PULL DOWN
const int btnPin1 = 1;
const int btnPin2 = 2;
const int ledPin1 = 9;
const int ledPin2 = 10;
void setup()
{
pinMode(btnPin1,INPUT);
pinMode(btnPin2,INPUT);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
}
void loop()
{
//pull down
int btnPin1State=digitalRead(btnPin1);
digitalWrite(ledPin1,btnPin1State);
//pull up
int btnPin2State = digitalRead(btnPin2);
digitalWrite(ledPin2,btnPin2State);
if(btnPin2State==HIGH)
{
digitalWrite(ledPin2,LOW);
}
else
{
digitalWrite(ledPin2,HIGH);
}

}
JINGLE BELLS SOUND
#include <pitches.h>
const int pinSpeaker = 9;
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.125
#define SIXTEENTH 0.0625

int tune[] =
{ NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4,
NOTE_E4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4,
NOTE_G4,NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_E4,
NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4,
NOTE_G4, NOTE_F4, NOTE_D4, NOTE_C4};
float duration[] =
{
QUARTER, QUARTER, HALF, QUARTER, QUARTER, HALF, QUARTER,
QUARTER, QUARTER+EIGHTH, EIGHTH,
WHOLE, QUARTER, QUARTER, QUARTER+EIGHTH, EIGHTH,
QUARTER, QUARTER, QUARTER, EIGHTH, EIGHTH,
QUARTER, QUARTER, QUARTER, QUARTER, HALF, HALF, QUARTER,
QUARTER, HALF, QUARTER, QUARTER, HALF,
QUARTER, QUARTER, QUARTER+EIGHTH, EIGHTH, WHOLE,
QUARTER, QUARTER, QUARTER, QUARTER,
QUARTER, QUARTER, QUARTER, EIGHTH, EIGHTH, QUARTER,
QUARTER, QUARTER, QUARTER, WHOLE
};

int length;
void setup()
{
pinMode(pinSpeaker,OUTPUT);
length = sizeof(tune)/sizeof(tune[0]);
}
void loop()
{
for(int i=0; i<length; i++)
{
tone(pinSpeaker,tune[i],800);
delay(800*duration[i]);
noTone(pinSpeaker);
}
delay(2000);
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close