15LLP108_Demo4_LedBlinking.pdf 1. Introduction In Demo3, we have learned how to read sensor values of light, temperature and humidity of a node and output these values to the console. In this demonstration, we will use the code from Demo3 and learn how to turn on/off the LEDs and make them blinking regularly on the sensor node XM1000, meanwhile to count how many times the LED has blinked and output the count to the console. 2. Timer In order to make the blue LED on the XM1000 sensor node to blink in every half second (i.e. On 0.5S and Off 0.5S), we also need a timer. Follow the instructions in Demo3 for configure and reset a timer. We aslo need to create an infinite while() loop so that it runs our functions repeatedly, such as counting the times the LED has blinked, output the counter’s value and actually turn on or off the LEDs to make it blinking. Please follow timer and while() loop structure in Demo3. 3. LED Blinking To get access to the LED functionalities in Contiki, we need to include the LED header file in the source code: #include "leds.h" // file is in directory /home/user/contiki/core/dev After the process begin, we have to initialise the LEDs on the sensor node by calling the following function: leds_init(); // Initialise the LEDs And finally we can turn on, off, or blink the LEDs by the following functions: void leds_on(unsigned char leds); void leds_off(unsigned char leds); void leds_toggle(unsigned char leds); void leds_invert(unsigned char leds); For example, if you want to blink the Blue LED, yon need to call the toggle function as: void leds_toggle(LEDS_BLUE); // Toggle the blue LED 4. Exercise Modify the program from Demo3 with periodic timer to make the BLUE led blinking in every half second, also to count the blinking times and output the counted number to the console. Can your change the code so that the BLUE LED is lighted for 1 second and off for 0.5 second periodically? 15LLP108 – Internet of Things and Applications Lab Session 2: Demo 4 – LED Blinking Prepared by Xiyu Shi 5. Source code Here is the source code for reference #include "contiki.h" #include "leds.h" #include <stdio.h> /* for printf() */ static struct etimer timer; /*____________________________________________________*/ PROCESS(led_blinking_process, "LED Blinking Process"); PROCESS(LED_process, "LED process"); AUTOSTART_PROCESSES(&LED_process); /*____________________________________________________*/ PROCESS_THREAD(LED_process, ev, data) { static int count = 0; PROCESS_BEGIN(); etimer_set(&timer, CLOCK_CONF_SECOND/2); // 0.5S timer leds_init(); // intialise the LEDs while(1) { PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER); // wait for timer event count++; // count the blinking times process_start(&led_blinking_process, NULL); // to blink the BLUE Led printf("Count: %d\n", count); // output the counte ...