Search

Blog Archive

Tuesday, 12 March 2019

Programmes

//1. Blinking Two LED’s using Arduino
int LED1 = 10;
int LED2 = 11;
void setup()
{
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
}
void loop()
{
digitalWrite(LED1,HIGH);
delay(1000);
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
delay(1000);
digitalWrite(LED2,LOW);
}

//2 Control led with pushbutton
const int LED = 10;
const int SW = 7;
int val=0;
void setup()
{
pinMode(LED,OUTPUT);
pinMode(SW, INPUT);
}
void loop()
{
val=digitalRead(SW);
if(val==HIGH)
{
digitalWrite(LED,HIGH);
}
else
{
digitalWrite(LED,LOW);
}
}

//3.Toggle LED using Pushbutton
int LED=10;
int SW=7;
boolean state = true; //declare variable state as boolean
int val=0;
void setup()
{
 pinMode(LED,OUTPUT);
 pinMode(SW,INPUT);
 }
void loop()
{
 val=digitalRead(SW);
 delay(120); // Software debouncing using 120ms delay
if(val==HIGH)
{state=!state; // Compliment variable state using !
 digitalWrite(LED,state);
}
}


//4. Toggle 2 LED using Pushbutton
int LED1=10;
int LED2=11;
int SW=7;
boolean state = true; // declare variable state as boolean
int val=0;
void setup()
{
 pinMode(LED1,OUTPUT);
 pinMode(LED2,OUTPUT);
 pinMode(SW,INPUT);
}
void loop()
{
 val=digitalRead(SW);
 delay(100); // Software debouncing
if(val==HIGH)
{state=!state; // Complimenting the status of LED
 digitalWrite(LED1,state);
 digitalWrite(LED2,!state);
}
}


//5. Fading LED

int led = 10;          
int brightness = 0;   
int fadeAmount = 5;  

void setup() {
  pinMode(led, OUTPUT);
}
void loop() {
 
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}

No comments:

Post a Comment