//PHOEBE DEGROOT - 03102022
//lab1_ledcontrol v1
//Web control of two led state and blink via web
//written for Particle Argon
#define numLEDs 2
int blinkDelay = 800; //blink delay in millis
int set = 1;
//struct containting LED data
typedef struct {
uint8_t pin;
uint8_t state;
uint8_t blinkCt;
unsigned long timer;
}LED;
LED LED01 = {D2, LOW, 0, 0};
LED LED02 = {D3, LOW, 0, 0};
LED *pLED; //pointer to the current LED
LED myLEDs[] = {LED01, LED02};
void init(){
pinMode(LED01.pin, OUTPUT);
pinMode(LED02.pin, OUTPUT);
digitalWrite(LED01.pin, LOW);
digitalWrite(LED02.pin, LOW);
}
void setup() {
init();
Particle.function("led",ledControl);
}
void ledBlink(LED *tLED){
//fn to blink led given pointer to LED object
if (millis()-tLED->timer>blinkDelay){
if (tLED->state == HIGH){
tLED->state = LOW;
//decr blinkCt every low switch
if (tLED->blinkCt>0) {tLED->blinkCt--;}
}
else if (tLED->state == LOW){
tLED->state = HIGH;
}
digitalWrite(tLED->pin, tLED->state);
tLED->timer = millis();
}
}
void loop() {
pLED = &LED01;
if (pLED->blinkCt != 0){
ledBlink(pLED);
}
pLED = &LED02;
if (pLED->blinkCt != 0){
ledBlink(pLED);
}
}
int ledControl(String command){
command.toUpperCase();
String subcmd="";
if (set == 1) pLED = &LED01;
else if (set == 2) pLED = &LED02;
if (command == "HIGH"){
pLED->state = HIGH;
pLED->blinkCt = 0;
digitalWrite(pLED->pin, HIGH);
}
else if (command == "LOW"){
pLED->state = LOW;
pLED->blinkCt = 0;
digitalWrite(pLED->pin, LOW);
}
else if (command == "BLINK"){
pLED->state = LOW;
pLED->blinkCt = -1;
}
else if (command == "BLINK3"){
pLED->state = LOW;
pLED->blinkCt = 3;
}
else if (command.charAt(5) == ':'){
//Use "BLINK:N" to blink led N times
subcmd = command.substring(6);
if (subcmd.toInt() >= 0){
pLED->blinkCt = subcmd.toInt();
pLED->state = LOW;
}
}
else if (command.charAt(2) == 'D'){
//Use "LEDN" to send commands to LEDN
subcmd = command.substring(3);
if (subcmd.toInt() >0 and subcmd.toInt()<= numLEDs){
set = subcmd.toInt();
}
}
else{return -1;}
return 1;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .