//FreeRTOS library:
#include <Arduino_FreeRTOS.h>
//Variables for 74HC595 shift register:
int latchPin = 5; //Latch pin of 74HC595 is connected to Digital pin 5
int clockPin = 6; //Clock pin of 74HC595 is connected to Digital pin 6
int dataPin = 4; //Data pin of 74HC595 is connected to Digital pin 4
byte leds = 0; //Variable to hold the pattern of which LEDs are currently turned on or off
void setup(){
//Set all the pins of 74HC595 as OUTPUT:
pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT);
//Task initalization:
xTaskCreate(LEDs, "74HC595", 100, NULL, 0, NULL);
}
static void LEDs(void* pvParameters){
//Initially turns all the LEDs off, by giving the variable 'leds' the value 0
leds = 0; updateShiftRegister(); vTaskDelay(500 / portTICK_PERIOD_MS);
for (int i = 0; i < 2; i++){
bitSet(leds, i); //Set the bit that controls that LED in the variable 'leds'
updateShiftRegister();
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
/* „updateShiftRegister()” - This function sets the latchPin to low,
* then calls the Arduino function 'shiftOut' to
* shift out contents of variable 'leds' in the shift register
* before putting the 'latchPin' high again. */
void updateShiftRegister(){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
//No need for 'loop' function when FreeRTOS is used!
void loop(){}