Time Functions
a. millis()
- This function returns the number of milliseconds passed since the Particle Photon board started running the current program.
- This number overflows (rolls back to zero) after approximately 50 days.
- Value returned by millis is an unsigned long int.
Example
unsigned long time;
time = millis();
b. micros()
- This function returns the number of microseconds passed since the Particle Photon board started running the current program.
- This number overflows (rolls back to zero) after exactly 35,791,394 microseconds (0 .. 35,791,394) on the Photon.
- Value returned by micros is an unsigned long int.
- Example
unsigned long time;
time = micros();
c. delay(value)
- This function pauses the program for the number of milliseconds specified.
value
: the number of milliseconds to pause the program.
d. delayMicroseconds(value)
- This function pauses the program for the number of microseconds specified.
value
: the number of microseconds to pause the program.
e. Time.hour()
- Retrieve the hour for the current or given time. Integer is returned without a leading zero.
- Returns: Integer 0-23
- Example:
Serial.print(Time.hour());
f. Time.minute()
- Retrieve the minute for the current or given time. Integer is returned without a leading zero.
- Returns: Integer 0-59
- Example:
Serial.print(Time.minute());
g. Time.second()
- Retrieve the seconds for the current or given time. Integer is returned without a leading zero.
- Returns: Integer 0-59
- Example:
Serial.print(Time.second());
h. Time.day()
- Retrieve the day for the current or given time. Integer is returned without a leading zero.
- Returns: Integer 1-31
- Example:
Serial.print(Time.day());
i. Time.weekday()
- Retrieve the weekday for the current or given time.
1 = Sunday
2 = Monday
3 = Tuesday
4 = Wednesday
5 = Thursday
6 = Friday
7 = Saturday
- Returns: Integer 1-7
- Example:
Serial.print(Time.weekday());
j. Time.month()
- Retrieve the month for the current or given time. Integer is returned without a leading zero.
- Returns: Integer 1-12
- Example:
Serial.print(Time.month());
k. Time.year()
- Retrieve the 4-digit year for the current or given time.
- Returns: Integer
- Example:
Serial.print(Time.year());
l. Time.timeStr()
- Return string representation for the given time.
- Returns: Srting
- Example:
Serial.print(Time.timeStr());
Example:
In the given example, we transmit time to serial monitor using millis
and micros
functions. These functions return the number of milliseconds and microseconds values passed since the Particle Photon board started running the current program.
Program
unsigned long time_value;
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
pinMode(D7, OUTPUT); /* Pin 13 is defined as Output */
Serial.begin(9600); /* opens serial port, sets data rate to 9600 bps */
}
/* Loop runs over and over after the startup function */
void loop()
{
digitalWrite(D7, HIGH); /* Make pin 13 HIGH */
delay(2000); /* Wait for 2 seconds */
digitalWrite(D7, LOW);
delayMicroseconds(100); /* Wait for 100 microseconds */
Serial.print("Time passed since power on in milliseconds:");
time_value = millis(); /* Read value of number of microseconds since program started executing */
Serial.println(time_value);
delay(2000);
Serial.print("Time passed since power on in microseconds:");
time_value = micros(); /* Read value of number of milliseconds since program started executing */
Serial.println(time_value);
delay(1000);
}
Interrupt Functions
a) interrupts()
- This function re-enables interrupts after they have been disabled by noInterrupts().
b) noInterrupts()
- This function disables interrupts.
- Interrupts can be re-enabled using interrupts().
c) attachInterrupt(pin, function, mode)
- All pins with the exception of D0 and A5 (since at present Mode Button external interrupt(EXTI) line is shared with D0, A5). Also please note following are the pins for which EXTI lines are shared so only one can work at a time:
- D1, A4
- D2, A0, A3
- D3, DAC
- D4, A1
Parameters:
pin
: is the pin number to which external interrupt functionality is to be enabled.
For Particle Photon, only pins 2 and 3 can be used for external interrupts.
function
: The function to call when interrupt event occurs. This function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine (ISR).mode
: Decides which event should cause an interrupt. 4 options are available to select from.
i) CHANGE
- Interrupt generated whenever pin changes value.
ii) FALLING
- Interrupt generated whenever pin changes from HIGH to LOW.
iii) RISING
- Interrupt generated whenever pin changes from LOW to HIGH.
iv) priority (optional):
- The priority of this interrupt. Default priority is 13. Lower values increase the priority of the interrupt.
v) subpriority (optional):
- The subpriority of this interrupt. Default subpriority is 0.
d) detachInterrupt(pin)
- Turns off the interrupt specified by the (pin) number.
Example:
/* Interrupt Functions */
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
}
/* Loop runs over and over after the startup function */
void loop()
{
noInterrupts();
/* critical, time-sensitive code here */
interrupts();
/* other code here */
}
Program
Let’s turn ON-OFF LED when interrupt is generated using switch on Particle Photon.
Program
/* Interrupt Functions */
int LED = D7;
int Button = D1;
volatile bool led_state = LOW;
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
pinMode(LED, OUTPUT); /* Pin 13 is defined as Output */
pinMode(Button, INPUT_PULLUP); /* Interrupt pin */
attachInterrupt(Button, blink, FALLING);
}
/* Loop runs over and over after the startup function */
void loop()
{
digitalWrite(LED, led_state); /* Write state value to pin 13 which has on-board LED */
}
/* ISR */
void blink() {
led_state = !led_state; /* Toggle state value of LED */
}
Components Used |
||
---|---|---|
Particle Photon PHNTRAYH |
X 1 |