Thursday 19 April 2018

Millis Function - blinking 2 LEDs

 The is an example of multitasking using the Millis ()
I'm using LEDs conncted to pins 8 & 9 
they each have their cathode connected to Gnd via 220 ohm resistors


The Code:

// &&&&&&&&&&&&&&&&&&&&

unsigned long startms =0;

unsigned long previousMS = 0;
unsigned long interval=1000;
unsigned long previousMS_2 = 0;
unsigned long interval_2=500;

#define LED1 8
#define LED2 9

int LED1_state=0;
int LED2_state=0;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
  startms = millis();

  // code 1 for LED 1
  if (startms - previousMS > interval){
   previousMS = startms;
    if (LED1_state==0) LED1_state=1; else LED1_state=0;
    digitalWrite(LED1,LED1_state);
  }
  // code 2 for LED 2
    if (startms - previousMS_2 > interval_2){
   previousMS_2 = startms;
    if (LED2_state==0) LED2_state=1; else LED2_state=0;
    digitalWrite(LED2,LED2_state);
    
  }
}

// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

No comments:

Post a Comment