Day 6- Millis concept & Denounce Button
Task
1) Blink without Delay
2) Blink multiple LED with different interval
3) Debounce button
4) Control Blinking LED with debounce Button
5) 4 LED with 4 Debounce buttons
6) 4ch Home Automation With IR remote and 4 push button debounce
Millis()
This function is used to return the number of milliseconds at the time, the Arduino board begins running the current program. This number overflows i.e. goes back to zero after approximately 50 days.
millis() function Syntax
millis () ;
This function returns milliseconds from the start of the program.
Example
unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Time:"); time = millis();
Serial.println(time); //prints time since program started
delay(1000); // wait a second so as not to send massive amounts of data
}
Arduino provides four different time manipulation functions. They are −
S.No. Function & Description
1 delay () function
The way the delay() function works is pretty simple. It accepts a single integer (or number) argument. This number represents the time (measured in milliseconds).
2 delayMicroseconds () function
The delayMicroseconds() function accepts a single integer (or number) argument. There are a thousand microseconds in a millisecond, and a million microseconds in a second.
3 millis () function
This function is used to return the number of milliseconds at the time, the Arduino board begins running the current program.
4 micros () function
The micros() function returns the number of microseconds from the time, the Arduino board begins running the current program. This number overflows i.e. goes back to zero after approximately 70 minutes.
-------------------------------------------------------------
Concept
---------------------------------------------------------------
Blink Without Delay
Sometimes you need to do two things at once. For example you might want to blink an LED while reading a button press. In this case, you can't use delay(), because Arduino pauses your program during the delay(). If the button is pressed while Arduino is paused waiting for the delay() to pass, your program will miss the button press.
This sketch demonstrates how to blink an LED without using delay(). It turns the LED on and then makes note of the time. Then, each time through loop(), it checks to see if the desired blink time has passed. If it has, it toggles the LED on or off and makes note of the new time. In this way the LED blinks continuously while the sketch execution never lags on a single instruction.
An analogy would be warming up a pizza in your microwave, and also waiting some important email. You put the pizza in the microwave and set it for 10 minutes. The analogy to using delay() would be to sit in front of the microwave watching the timer count down from 10 minutes until the timer reaches zero. If the important email arrives during this time you will miss it.
What you would do in real life would be to turn on the pizza, and then check your email, and then maybe do something else (that doesn't take too long!) and every so often you will come back to the microwave to see if the timer has reached zero, indicating that your pizza is done.
In this tutorial you will learn how to set up a similar timer.
Hardware Required
Arduino Board
LED
220 ohm resistor
Circuit
To build the circuit, connect one end of the resistor to pin 13 of the board. Connect the long leg of the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short leg of the LED (the negative leg, called the cathode) to the board GND, as shown in the diagram above and the schematic below.
Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this example with no hardware attached, you should see that LED blink.
After you build the circuit plug your board into your computer, start the Arduino Software (IDE), and enter the code below.
Code
The code below uses the millis() function, a command that returns the number of milliseconds since the board started running its current sketch, to blink an LED.
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // save the last time you blinked the LED
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
-----------------------------------------------------------------------------
Blink 2 LED parallelly with different interval
const int ledPin = 13;
const int ledPin1 = 12;// the number of the LED pin
int ledState = LOW;
int ledState1 = LOW; // ledState used to set the LED
// ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis1 = 0;
const long interval = 1000; // interval at which to blink (milliseconds)
const long interval1 = 500;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1,OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
if (ledState1 == LOW) {
ledState1 = HIGH;
} else {
ledState1 = LOW;
}
digitalWrite(ledPin1, ledState1);
}
}
------------------------------------------------------------------------------
//OR
const int ledPin = 13; // the number of the LED pin
onst int ledPin1 = 12;
int ledState = LOW; // ledState used to set the LED
int ledState1 = LOW;
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis1 = 0;
const long interval = 1000; // interval at which to blink (milliseconds)
const long interval1 = 300;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
if (currentMillis - previousMillis >= interval) { // to blink LED1
// save the last time you blinked the LED
previousMillis = currentMillis;
ledState=!ledState;
digitalWrite(ledPin, ledState);
}
if (currentMillis1 - previousMillis1 >= interval1) { // to blink LED 2
// save the last time you blinked the LED
previousMillis1 = currentMillis1;
ledState1=!ledState1;
digitalWrite(ledPin1, ledState1);
}
}
-------------------------------------------------------------------------------
blink 2 led with different frequency controlled by 2 push buttons
const int ledPin = 13;
const int ledPin1 = 12;// the number of the LED pin
const int btn=2;
const int btn1=3;
byte blink=0;
byte blink1=0;
int ledState = LOW;
int ledState1 = LOW; // ledState used to set the LED
// ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis1 = 0;
const long interval = 1000; // interval at which to blink (milliseconds)
const long interval1 = 500;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1,OUTPUT);
pinMode(btn, INPUT);
pinMode(btn1,INPUT);
digitalWrite(btn,HIGH);
digitalWrite(btn1,HIGH);
}
void loop() {
byte sw=digitalRead(btn);
byte sw1=digitalRead(btn1);
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
if (currentMillis - previousMillis >= interval && blink==1) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
if (currentMillis1 - previousMillis1 >= interval1 && blink1==1) {
previousMillis1 = currentMillis1;
if (ledState1 == LOW) {
ledState1 = HIGH;
} else {
ledState1 = LOW;
}
digitalWrite(ledPin1, ledState1);
}
if (sw==0){blink=!blink; delay(300);}
if (sw1==0){blink1=!blink1; delay(300);}
}
------------------------------------------------------------------------------
Debounce
------------------------------------------------------------------------------
/ constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
-----------------------------------------------------------------------------
4 button 4 led debounce
const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 4;
const int button4Pin = 5;
const int led1Pin = 13;
const int led2Pin = 12;
const int led3Pin = 11;
const int led4Pin = 10;
int led1State = HIGH;
int led2State = HIGH;
int led3State = HIGH;
int led4State = HIGH;
int button1State;
int button2State;
int button3State;
int button4State;
int lastButton1State = LOW;
int lastButton2State = LOW;
int lastButton3State = LOW;
int lastButton4State = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
digitalWrite(led1Pin, led1State);
digitalWrite(led2Pin, led2State);
digitalWrite(led3Pin, led3State);
digitalWrite(led4Pin, led4State);
}
void loop() {
int reading1 = digitalRead(button1Pin);
int reading2 = digitalRead(button2Pin);
int reading3 = digitalRead(button3Pin);
int reading4 = digitalRead(button4Pin);
if (reading1 != lastButton1State) {
lastDebounceTime = millis(); }
if (reading2 != lastButton2State) {
lastDebounceTime = millis(); }
if (reading3 != lastButton3State) {
lastDebounceTime = millis(); }
if (reading4 != lastButton4State) {
lastDebounceTime = millis(); }
//////////////////////////////////////////////////////
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading1 != button1State) {
button1State = reading1;
if (button1State == HIGH) {
led1State = !led1State;
}}}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading2 != button2State) {
button2State = reading2;
if (button2State == HIGH) {
led2State = !led2State;
}}}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading3 != button3State) {
button3State = reading3;
if (button3State == HIGH) {
led3State = !led3State;
}}}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading4 != button4State) {
button4State = reading4;
if (button4State == HIGH) {
led4State = !led4State;
}}}
////////////////////////////////////////////////////////
digitalWrite(led1Pin, led1State);
digitalWrite(led2Pin, led2State);
digitalWrite(led3Pin, led3State);
digitalWrite(led4Pin, led4State);
lastButton1State = reading1;
lastButton2State = reading2;
lastButton3State = reading3;
lastButton4State = reading4;
}
-------------------------------------------------------------------------------
IR remote home automation with pushbutton
#include <IRremote.h>
int RECV_PIN = 14 ;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;
unsigned long previousMillis = 0;
const long interval = 200;
const int Relay1=13;
const int Relay2=12;
const int Relay3=11;
const int Relay4=10;
byte Relay1state=0;
byte Relay2state=0;
byte Relay3state=0;
byte Relay4state=0;
const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 4;
const int button4Pin = 5;
int button1State;
int button2State;
int button3State;
int button4State;
int lastButton1State = LOW;
int lastButton2State = LOW;
int lastButton3State = LOW;
int lastButton4State = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Relay3,OUTPUT);
pinMode(Relay4,OUTPUT);
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,LOW);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
digitalWrite(button1Pin,HIGH);
digitalWrite(button2Pin,HIGH);
digitalWrite(button3Pin,HIGH);
digitalWrite(button4Pin,HIGH);
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
delay(1000);
Serial.println("ready to accepte IR Remote input ");
}
void loop(){
if (irrecv.decode(&results)){
Serial.println(results.value, HEX);
Serial.println(results.value, DEC);
if (results.value == 0XFFFFFFFF)
results.value = key_value;
switch(results.value){
case 0xFFC03F:
Serial.println("Mute");
break;
case 0xFF906F:
Serial.println("PWR");
break;
case 0xFFB04F:
Serial.println("1");
Relay1state=!Relay1state;
break;
case 0xFF9867:
Serial.println("2");
Relay2state=!Relay2state;
break;
case 0xFFD827:
Serial.println("3");
Relay3state=!Relay3state;
break;
case 0xFF807F:
Serial.println("4");
Relay4state=!Relay4state;
break;
case 0xFF8877:
Serial.println("5");
break;
case 0xFFA857:
Serial.println("6");
break;
case 0xFFE817:
Serial.println("7");
break;
case 0xFF609F:
Serial.println("8");
break;
case 0xFF48B7:
Serial.println("9");
break;
case 0xFF6897:
Serial.println("0");
break;
}
key_value = results.value;
delay(100);
irrecv.resume();
}
///////////////////////////////////////////////////////////
int reading1 = digitalRead(button1Pin);
int reading2 = digitalRead(button2Pin);
int reading3 = digitalRead(button3Pin);
int reading4 = digitalRead(button4Pin);
if (reading1 != lastButton1State) {
lastDebounceTime = millis(); }
if (reading2 != lastButton2State) {
lastDebounceTime = millis(); }
if (reading3 != lastButton3State) {
lastDebounceTime = millis(); }
if (reading4 != lastButton4State) {
lastDebounceTime = millis(); }
//////////////////////////////////////////////////////
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading1 != button1State) {
button1State = reading1;
if (button1State == HIGH) {
Relay1state = !Relay1state;
}}}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading2 != button2State) {
button2State = reading2;
if (button2State == HIGH) {
Relay2state = !Relay2state;
}}}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading3 != button3State) {
button3State = reading3;
if (button3State == HIGH) {
Relay3state = !Relay3state;
}}}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading4 != button4State) {
button4State = reading4;
if (button4State == HIGH) {
Relay4state = !Relay4state;
}}}
////////////////////////////////////////////////////////
digitalWrite(Relay1,Relay1state);
digitalWrite(Relay2,Relay2state);
digitalWrite(Relay3,Relay3state);
digitalWrite(Relay4,Relay4state);
lastButton1State = reading1;
lastButton2State = reading2;
lastButton3State = reading3;
lastButton4State = reading4;
}
-------------------------------------------------------------------
No comments:
Post a Comment