12v sennse and a button

Ok so I have 2 questions id like your INPUT;

1st is I need the Arduino sense when another circuit has activated but its a 12v circuit how could I safely connect the 2?

2nd question is I need a momentary switch to go HGH and stay high no matter how many times its pressed any ideas?

here is my code its not tested yet because i'm still waiting for parts.

[quote//#include <LiquidCrystal.h>
//LiquidCrystal lcd(13,12,11,10,A5,A4)

// Input pins
int pirpin = 8;
int doorpin = 7;
int setpin = 6;
int disarmpin = 5;
//Output pins
int sirenpin = 4;
int siren2pin = 3;
int strobepin = 9;
//var

void setup()
{
pinMode(pirpin, INPUT);
pinMode(doorpin, INPUT);
pinMode(setpin, INPUT);
pinMode(disarmpin, INPUT);
pinMode(sirenpin, OUTPUT);
pinMode(siren2pin, OUTPUT);
pinMode(strobepin, OUTPUT);
}

void loop()
{
if (digitalRead(setpin) == HIGH && digitalRead(pirpin) == HIGH)
{
digitalWrite(sirenpin, HIGH); digitalWrite(strobepin, HIGH);
//delay(900000);
//digitalWrite(sirenpin, LOW);
}
else
{
digitalWrite(sirenpin, LOW);
}

if (digitalRead(setpin) == HIGH && digitalRead(doorpin) == HIGH)
{
digitalWrite(sirenpin, HIGH); digitalWrite(strobepin, HIGH);
}
else
{
digitalWrite(sirenpin, LOW);
}

if (digitalRead(setpin) == HIGH && digitalRead(doorpin) == HIGH && digitalRead(pirpin) == HIGH)
{
digitalWrite(sirenpin, HIGH); digitalWrite(strobepin, HIGH);
}
else
{
digitalWrite(sirenpin, LOW);
}

if (digitalRead(disarmpin) == HIGH)
{
digitalWrite(sirenpin, LOW); digitalWrite(strobepin, LOW);
}

}
[/quote]

Would this work?

if digitalRead(setpin, HIGH);
digitalWrite(setpin, HIGH);

else digitalWrite(sepin, HIGH);

Apart from the spelling mistake (and you cannot afford spelling mistakes in a computer program - the compiler may detect some, but if it does not, you are in real trouble!), you are mis-construing what "digitalRead()" does - for a start, it only takes one argument.

You meant:

if (digitalRead(setpin) ==  HIGH);

though I suspect

if (digitalRead(setpin));

should also do the same job.

And of course, if "setpin" is an input, then the only thing that writing it HIGH will do (and it will do this) is to enable the internal pullup.

Now I notice in your second post here, you do know what the "code" tags mean, so if you go back and mark the code in your first post the same way, we (myself and others) may take a look at it and make further suggestions.

In general, all you do is to read the pin and set a variable in order to "latch" the value.

Hi and thanks for your feedback, I appreciate it! I find it difficult to edit using a iphone so I have added below the code. Thanks for your time.

//#include <LiquidCrystal.h>
//LiquidCrystal lcd(13,12,11,10,A5,A4)


// Input pins
 int pirpin = 8;
int doorpin = 7;
int setpin = 6;
int disarmpin = 5;
//Output pins
int sirenpin = 4;
int siren2pin = 3;
int strobepin = 9;
//var


void setup()
{
  pinMode(pirpin, INPUT);
  pinMode(doorpin, INPUT);
  pinMode(setpin, INPUT);
  pinMode(disarmpin, INPUT);
  pinMode(sirenpin, OUTPUT);
  pinMode(siren2pin, OUTPUT);
  pinMode(strobepin, OUTPUT);
}

void loop()
{
  if (digitalRead(setpin) == HIGH && digitalRead(pirpin) == HIGH)
  {
    digitalWrite(sirenpin, HIGH); digitalWrite(strobepin, HIGH);
    //delay(900000);
    //digitalWrite(sirenpin, LOW);
  }
  else
  {
    digitalWrite(sirenpin, LOW);
  }
 
  if (digitalRead(setpin) == HIGH && digitalRead(doorpin) == HIGH)
  {
    digitalWrite(sirenpin, HIGH); digitalWrite(strobepin, HIGH);
  }
  else
  {
    digitalWrite(sirenpin, LOW);
  }
 
  if (digitalRead(setpin) == HIGH && digitalRead(doorpin) == HIGH && digitalRead(pirpin) == HIGH)
  {
    digitalWrite(sirenpin, HIGH); digitalWrite(strobepin, HIGH);
  }
  else
  {
    digitalWrite(sirenpin, LOW);
  }
 
  if (digitalRead(disarmpin) == HIGH)
  {
  digitalWrite(sirenpin, LOW); digitalWrite(strobepin, LOW);
  }

}

Minor things:
You are assuming inputs will be pulled high when active. This may be a result of reading the unfortunate "tutorials" on the Arduino site. In practice, buttons (and most sensors) should be wired to ground and have pull-up resistors to 5V so that they are active LOW. This minimises the risk should wires to the buttons (etc.) be shorted to ground. Also, by setting a pin to an INPUT but writing it HIGH (as I mentioned before) you activate the internal pull-up in the MCU and probably do not need any other. I have not changed this for the present.

Major things:
You have used a delay() function for 900 seconds - this will do just what it says - absolutely nothing will happen for about fifteen minutes except for the siren continuing to sound even if you press the "disarm" button!

I have therefore substituted a piece of code which allows all other things to happen but checks every time through the loop to see if fifteen minutes has passed.

Now, you want "memory" of two things - whether the system is "armed" or not, and whether it is "alarmed" or not. I have defined these a "booleans" because they are either true or false; one or the other. Actually, I have not really used the "alarmed" and "strobed" flags, but they will be useful for enhancements.

Such as? Well, there are all sorts of things to consider. So far, the code does not distinguish between your "door" and your "PIR". Should it?

What about if either of these is still tripped when the fifteen minute timeout happens? Should the siren continue? (It should not.) You can use the "alarmed" and "strobed" flags to take care of such complications.

Note that I have coded for "disarm" taking priority over "set".

Hope you get your hardware soon. (Have you not an Arduino and some LEDs to try it out on already?)

//#include <LiquidCrystal.h>
//LiquidCrystal lcd(13,12,11,10,A5,A4)

// Input pins
int pirpin = 8;
int doorpin = 7;
int setpin = 6;
int disarmpin = 5;

// Output pins
int sirenpin = 4;
int siren2pin = 3;
int strobepin = 9;

// var - the missing pieces!
boolean armed;
boolean alarmed;
boolean strobed;

unsigned long count1 = 0;   // will store initial time

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check 
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

void setup()
{
  pinMode(pirpin, INPUT);
  pinMode(doorpin, INPUT);
  pinMode(setpin, INPUT);
  pinMode(disarmpin, INPUT);

  pinMode(sirenpin, OUTPUT);
  pinMode(siren2pin, OUTPUT);
  pinMode(strobepin, OUTPUT);

  armed = false;
  alarmed = false;
  strobed = false;
}

void loop()
{
  if (digitalRead(setpin) == HIGH) { // Can be armed
    if (digitalRead(disarmpin) == LOW) // unless disarm is also held
      armed = true;
  }

  if (armed && digitalRead(pirpin) == HIGH)
  {
    alarmed = true;
    digitalWrite(sirenpin, HIGH); 
    strobed = true;
    digitalWrite(strobepin, HIGH);
    count1 = millis(); // set the countdown
    //delay(900000);
    //digitalWrite(sirenpin, LOW);
  }

  if (armed && digitalRead(doorpin) == HIGH)
  {
    alarmed = true;
    digitalWrite(sirenpin, HIGH); 
    strobed = true;
    digitalWrite(strobepin, HIGH);
    count1 = millis(); // set the countdown
  }

  if (digitalRead(disarmpin) == HIGH)
  {
    armed = false;
    alarmed = false;
    digitalWrite(sirenpin, LOW); 
    strobed = false;
    digitalWrite(strobepin, LOW);
  }

  if (timeout(&count1, 900000UL )) { // Enough siren?
    alarmed = false;
    digitalWrite(sirenpin, LOW); 
  }
}

Your 12V source question:

1st is I need the Arduino sense when another circuit has activated but its a 12v circuit how could I safely connect the 2?

Need to use 2 resistors and divide down the 12V to 5V so that the Max Voltage of VCC + 0.5V is not exceeded.
Connect 2 resistors in series from 12V to Gnd:
12V - resistor R1 - resistor R2 - Gnd
Arduino is connected to the R1/R2 junction, and will see:
Vout = 12V * R2/(R1 + R2)
With R2 = 5K, and R1 = 8.2K:
Vout = 12V * 5000/(5000 + 8200) = 4.5V

CrossRoads:
Your 12V source question:

1st is I need the Arduino sense when another circuit has activated but its a 12v circuit how could I safely connect the 2?

Need to use 2 resistors and divide down the 12V to 5V so that the Max Voltage of VCC + 0.5V is not exceeded.
Connect 2 resistors in series from 12V to Gnd:
12V - resistor R1 - resistor R2 - Gnd
Arduino is connected to the R1/R2 junction, and will see:
Vout = 12V * R2/(R1 + R2)
With R2 = 5K, and R1 = 8.2K:
Vout = 12V * 5000/(5000 + 8200) = 4.5V

Thanks I will try this Day off work tomorrow so will be off to get some arts for the project. +Karma

Paul__B:
Minor things:
You are assuming inputs will be pulled high when active. This may be a result of reading the unfortunate "tutorials" on the Arduino site. In practice, buttons (and most sensors) should be wired to ground and have pull-up resistors to 5V so that they are active LOW. This minimises the risk should wires to the buttons (etc.) be shorted to ground. Also, by setting a pin to an INPUT but writing it HIGH (as I mentioned before) you activate the internal pull-up in the MCU and probably do not need any other. I have not changed this for the present.

Major things:
You have used a delay() function for 900 seconds - this will do just what it says - absolutely nothing will happen for about fifteen minutes except for the siren continuing to sound even if you press the "disarm" button!

I have therefore substituted a piece of code which allows all other things to happen but checks every time through the loop to see if fifteen minutes has passed.

Now, you want "memory" of two things - whether the system is "armed" or not, and whether it is "alarmed" or not. I have defined these a "booleans" because they are either true or false; one or the other. Actually, I have not really used the "alarmed" and "strobed" flags, but they will be useful for enhancements.

Such as? Well, there are all sorts of things to consider. So far, the code does not distinguish between your "door" and your "PIR". Should it?

What about if either of these is still tripped when the fifteen minute timeout happens? Should the siren continue? (It should not.) You can use the "alarmed" and "strobed" flags to take care of such complications.

Note that I have coded for "disarm" taking priority over "set".

Hope you get your hardware soon. (Have you not an Arduino and some LEDs to try it out on already?)

I BIG thank you! I was not expecting so much thank you. I Will go through your revised code and lean from what you have done. to answer some of your questions.

I was assuming that the switch is HIGH when on. my "button" is not really a button on the disarming part its actually a 12v pulse from a door keypad that is telling the door lock to open. its a total separate circuit I plan on pulling the voltage down to 4.5- 5v

delay() yes I suspected it could cause everything else to stop for 15 minutes but I hadn't Googled yet to confirm. I was going to remove if I couldn't find a solution. I wanted the siren to stop after 15 minutes but the strobe to flash until I hit disarm.

The door and PIR are 2 separate sensors obviously a PIR sensor and the door would probably be a magnetic read switch.

So what I have is 2 separate systems
system 1: 12v keypad and 12v door lock. (keypad is a standalone system) (keypad is on the outside of the building so no delay needed when opening door.

system 2: Arduino that will sense the button press to arm the system. once armed if PIR or door are triggered then Siren and strobe activate, The disarm will be another pin sensing a pulse from the keypad to the lock. The arm momentary ARM button needs to arm only and either ignore other presses or just arm the system again.

Once again thanks foe your help I will be getting my teeth into this tomorrow and yes I do have an UNO and the sired (which can be run from the UNO surprisingly even tough its 12V

it it is an entirely different system electrically, wouldn't an optocoupler be a good way to monitor it?

Peter_I:
it it is an entirely different system electrically, wouldn't an optocoupler be a good way to monitor it?

I did not know these existed until I just Googled it. I think your right this will be better than resistors.

I have tested the code on a LED's and a buzzer all works great. thanks for the help everyone I'm learning (slowly)