Basic coding assistance needed

Looking for someone to assist me with this code? I’m new and learning. I don’t want this to go into alarm Unless the AlarmPin is High for 5 seconds. What addition Code would i add and how? Any Detailed Help to add it to this code? Thanks

// We will be using D1 to control our LED
const int ledPin = D1;  //red led that lights up when the system is in alarm
const int led = D7;     //green led that flashes off and on when the system is Normal
const int lasttime = 0;

// Alarm Trigger pin that reads either high for alarm or low signals for no alarm is wired to D0
const int AlarmPin = D0;


void setup()
{


  pinMode( AlarmPin , INPUT_PULLUP); // sets pin as input
  pinMode( led , OUTPUT );           // green led
  pinMode( ledPin , OUTPUT );       //  red led
  Particle.subscribe("Task_Complete", anything, "200022001247343438323536");
}

void loop(){

    // find out if the ALARM is HIGH
   // or not by reading from it.
   int AlarmState = digitalRead( AlarmPin );


  if(AlarmState==1)
  {
    // turn the LED On
    digitalWrite( ledPin, HIGH);
    ))
    digitalWrite( led, LOW);
  }
  else{
    // otherwise
    // turn the RED ledPin Off and rotates the GREEN led off and on with 2 sec delay
    digitalWrite( ledPin, LOW);
    ))
   digitalWrite(led, HIGH);
    delay(2000);
    digitalWrite(led, LOW);
    delay(2000);

  }



    if ((lasttime == 0) && (AlarmState == 1))
  {
        // Send a publish...
        int success = Particle.publish("Alarm_Status","ACTIVE",PRIVATE);
        delay(2000);


      }else{
          // Otherwise, this isn't a new status, and we don't have to do anything.


      }
       if ((lasttime == 1) && (AlarmState == 0))
  {

        // Send a publish...
        Particle.publish("Alarm_Status","NORMAL",PRIVATE);
        delay(2000);


      }else{
          // Otherwise, this isn't a new status, and we don't have to do anything.


      }
      lasttime = AlarmState;
}

void anything(const char *event, const char *data)
{

}

Without going into coding details, how would you logically implement this?

  • Check if pin is high…
  • start counting…
  • etc…

hello, thanks for replying. I’m using a High sensitive microphone Sound detection sensor. When my alarm system goes into alarm, the sound detection picks up the alarm and sends a High output to the AlarmPin input pin of the particle board and post it to the particle and IFTTT website. Which in returns sends me a text and email that my alarm system is sounding at home. Everything is working okay now but, I want to add to this Code to not post an Alarm unless the Sound Detection sensor is High for 10 seconds, I don’t want a posted alarm. I’m trying to leave enough lead way for the family when then set the alarm off by accident. Hope this was the info you needed. Again im learning and new at coding.

I understand the setup now.
Try braking it up into little pieces, and figure out what needs to happen: pseudo code if you will

Read the alarm pin
If high, start counting
If X time passed, read alarm pin
If high, publish to ifttt
Else ‘reset’

Mind you, there are various ways to do this, so try to think of one, and we’ll go from there?

oh okay thanks, that leads me in the right direction. I sure appreciate it…

You have multiple options:

When you detect that the alarm pin is high, you can compute when to trigger the alarm.

time_t alertTime = Time.now() + 5;

or

int alertMillis = millis() + 5000;

You can then then use a check in loop() to detect and act with logic like:

if ((alertTime > 0) && (Time.now() >= alertTime)) {
     // trigger alert
}

or

If ((alertMillis > 0) && (Millis() >= alertMillis())) {
     // trigger alert
}

Cancelling the alert would be done by simply setting AlertMillis or AlertTime to zero.

Another option would be to write a function that triggers the alert, and create a software timer to call that function when the timer expires. You can even have the action performed multiple times if you like. And, of course, you can cancel the timer at any time. software timer details

--------------------- Clarification:

I bashed two things together for the sake of brevity, and I shouldn’t have.

If you use the alertMillis or alertTime scenarios, you will want to declare the variables and set them to zero. You declare them as a global variable … (before setup()) … or as a static variable within loop().

When you detect the alert pin is high, you will simply want to set the variable to the alert state value (Time.now() + 5), or (Millis() + 5000).

You will NOT want to declare the variable and set it to the alert state value in one step. I hope this did not cause any confusion.

1 Like

Oh Wow, thanks. This is what I needed. I really appreciate it. I’m going to give that a try now and plug it in.

1 Like

yes that helped out a lot. Thanks.