#define RED 12
#define BLUE 11
#define GREEN 9
#define SW 7
#define POT A0
#include <TaskScheduler.h>
// Scheduler
Scheduler ts;
void task1();
void task2();
void task3();
// set LED states
int REDS = LOW;
int brightness = 0;
// previous time for the tasks depending upon time.
unsigned long prevTime = millis();
unsigned long prevTimeSPPot = millis();
long timeIntervalASP = 3000;
// time intervals for the tasks
long timeRED = 1000;
long timeGREEN = 3000;
void setup()
{
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(SW, INPUT_PULLUP);
Serial.print("PROGRAM START");
}
void loop()
{
unsigned long curTime = millis();
task1();
task2();
task3();
}
void task1(){
// Task 1 : Blink LED1 (T1)
unsigned long curTime = millis();
if(curTime - prevTime > timeRED) {
prevTime = curTime;
REDS=!REDS;
digitalWrite(RED, REDS);
}
}
void task2(){
// Task 2 : Glow LED2 when BTN is pressed
if(digitalRead(SW) ){
digitalWrite(BLUE, LOW);
}else{
digitalWrite(BLUE, HIGH);
}
}
void task3(){
unsigned long curTime = millis();
// Task 3 : Read input from serial monitor (0-255) and then write to LED3
int potValue = analogRead(POT);
if (curTime - prevTimeSPPot > timeIntervalASP) {
prevTimeSPPot = curTime;
Serial.print("Value : ");
Serial.println(potValue);
brightness = potValue;
analogWrite(GREEN, brightness);
}
}