Back to Parent

int ledPin = D0;
int switchPin = D1;
int distPin = A1;
int switchState = 0;
int prevState = 0;
int alarmState = 2;
int prevAlarmState = 0;
int dist = 0;

void setup() {
  Particle.function("led", ledControl);

  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(distPin, INPUT);
	Serial.begin(9600);


  digitalWrite(ledPin, LOW);
}

void loop()
{

  switchState = digitalRead(switchPin);
  dist = analogRead(distPin);


  if(switchState == 0 && prevState == 1){
    alarmState = 0;
  }

  else if(switchState == 1 && prevState == 0){
    alarmState = 0;
  }

  if(dist > 1600 && alarmState == 1){
    alarmState = 2;
  }
  else if(dist < 1600 && alarmState == 2){
    alarmState = 1;
  }

  alarm(alarmState);

  prevState = switchState;

}

void alarm(int alarmState){
  if (alarmState == 0){
      digitalWrite(ledPin, LOW);
  }

  else if(alarmState == 1){
      for (int i = 0; i<255; i++){
        analogWrite(ledPin, i);
        delay(2);
      }
      for (int i = 255; i>0; i--){
        analogWrite(ledPin, i);
        delay(2);
      }
  }

  else if(alarmState == 2){
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
        digitalWrite(ledPin, HIGH);
        delay(300);
    }
}

int ledControl(String cmd){


//find state of led
  if(cmd == "Reminder"){
      alarmState = 1;
  }
  else if (cmd == "Off"){
      alarmState = 0;
  }
  else if (cmd == "Low"){
      alarmState = 2;
  }

  else{
    return -1;
  }

  return alarmState;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0