would this work?

I’m trying to build a alarm, would this code work?
What should I set threshold too? Im using Inpin1 and 2 are pushbuttons.

const int inPin1 = 7;
const int inPin2 = 8;
const int relayPin = 12;
const int sensorPin = A0;
const int threshold = 400;

void setup() {
  // buttonpins as input
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
  // relay as output
  pinMode(relayPin, OUTPUT);
  //sensor as input
  pinMode(sensorPin, INPUT);
  // serial communications:
  Serial.begin(9600);
}

void loop() {
  // value of sensor
  int analogValue = analogRead(sensorPin);
  int digitalValueA = digitalRead(inPin1);
  int digitalValueB = digitalRead(inPin2);

  // if sensor is turned on turn on alarm
  if (analogValue > 400)
  {
    delay(15000);
    digitalWrite(relayPin, HIGH);
  }
  else if (digitalValueA > 400 && digitalValueB > 400)
  {
    digitalWrite(relayPin, LOW);
  }
  else
  {
    digitalWrite(relayPin, LOW);
  }

  //print analog value
  Serial.println(analogValue);
  delay(1); //delay between reads
}

You will have to test to determine what threshold to use.

else if (digitalValueA > 400 && digitalValueB > 400) digital pins are only HIGH or LOW

 if (analogValue > 400)
  {
    delay(15000);
    digitalWrite(relayPin, HIGH);
  }

delay stops everything for the duration. here are some suggestions on how to avoid using it. blink without delay Demonstration code for several things at the same time multitasking the arduino

I want It to wait 15 seconds before the alarm starts beeping.

Yes those links will show you how to do that.

the Blink without delay example shows how to make a repeating timer.

Here the code is rearranged to make a one shot timer.

unsigned long startTime = 0;
const long interval = 1000;  // 1 second = 1000 millis
bool timeFlag = false;

void setup()
{
}

void loop()
{
  if (something you want to Time)
  {
    timeFlag = true;
    startTime = millis(); // Begin timing sequence
    // do stuff here when timing sequence starts
  }

  unsigned long currentTime = millis();
  if (timeFlag == true && currentTime - startTime >= interval) // End timing sequence
  {
    timeFlag = false;
    // do stuff here when time is up
  }
}

the one shot will work for your alarm delay.

the repeating timer works well for beeping or flashing an output.

read the links they will teach you a lot

should I still write const int inpin1 = 7 etc? and do I need to use else if?

new code:

unsigned long startTime = 0; const long interval = 1000; // 1 second = 1000 millis bool timeFlag = false; const int inPin1 = 7; const int inPin2 = 8; const int relayPin = 12; const int sensorPin = A0; const int threshold 400;

void setup() { pinMode(inPin1, INPUT); pinMode(inPin2, INPUT); pinMode(relayPin, OUTPUT); pinMode(sensorPin, INPUT); Serial.begin(9600); }

void loop() { int analogValue = analogRead(sensorPin); int digitalValueA = digitalRead(inPin1); int digitalValueB = digitalRead(inPin2);

if (analogValue > 400) { timeFlag = true; startTime = millis(15000); // Begin timing sequence // do stuff here when timing sequence starts digitalWrite(relayPin, HIGH); } else if (digitalValueA, HIGH && digitalValueB, HIGH) { digitalWrite(relayPin, LOW); } else { digitalWrite(relayPin, LOW); }

Serial.println(analogValue); delay(1);

unsigned long currentTime = millis(); if (timeFlag == true && currentTime - startTime >= interval) // End timing sequence { timeFlag = false; // do stuff here when time is up } }

now I'm getting error messages....

Please use [ code ] tags. The forum software eats some of your code if you don't.

Well, I would think that changing the 1-second delay to 15 seconds should be fairly easy. Just change the value of interval where it is defined at the top.

Please don’t use F**ing. A pencil sketch badly photographed with a phone is better.

po2us:
why? fritz’ng is awesome

It is not “awesom” it is crap. The reason is that in order to get the circuit into a form that can be understood you have to convert it into a schematic in your head. This is something you can not do and it is not something anyone can do once the circuit becomes non trivial. You only think it is great because you know very little.

po2us:
I uploaded the code to the Arduino and right after plugging it in the alarm starts beeping. I cant turn it off by pressing inPin1 and inPin2. What am I doing wrong?

You haven’t wired up anything to the other side of those push buttons. They don’t connect to anything.
Read this for how to wire up switches and buttons. Inputs

  if (analogValue > 400)
  {
    timeFlag = true;
    startTime = millis(15000); // Begin timing sequence
    // do stuff here when timing sequence starts
    digitalWrite(relayPin, HIGH);
  }

As was mentioned the interval variable is where you put the 15000 not in millis(). Since you want the alarm to go off after the 15 seconds is up, turn the relay on after the timing sequence ends.

else if (digitalValueA, HIGH && digitalValueB, HIGH)
  {
    digitalWrite(relayPin, LOW);
  }
  else
  {
    digitalWrite(relayPin, LOW);
  }

you don't need any else's here just say if the buttons are pushed turn the relay off.