I need to shorten my Code. (Level: for very beginners)

Hi all!

My first time in here so bare with me plz.

I'm writing a code for my alarm (in connection with my Assignment) which i want to be a little shorter if possible (without using fancy shortcuts).

My code is about an Alarm system that has 3 Inputs (movement sensor, Microphone & Water Sensor). when any of those are "activated" my alarm will react with the 2 Outputs that i have (Led light & Piezo "speaker").

I just want to shorten my code and combine all my 3 "if"s toghether so i dont have 3 parts in my loop basically meaning the same.

Here's a picture of my code and i hope you kinda get what im trying to do. Imgur: The magic of the Internet

Any help would be much appreciated, Thx ! :slight_smile:

My first time in here so bare with me plz.

You can take your clothes off if you want. I'll keep mine on.

Here's a picture of my code

Forget it. I don't look at pictures of text. Post the text here.

For best results here, read the two posts by Nick Gammon at the top of this Forum for guidelines on posting here. Make sure you state your questions clearly, tell us what you expected to see and what you got instead, any inputs that produced the bogus results, and a complete listing of your code. These steps help us help you.

PaulS:
Forget it. I don't look at pictures of text. Post the text here.

umm, well ok then here is my code:

int watersensor = 13;
int microphone = 12;
int window = 11;
int speaker = 3;
int light = 2;

void setup() {
// put your setup code here, to run once:
pinMode (watersensor, INPUT);
pinMode (microphone, INPUT);
pinMode (window, INPUT_PULLUP);
pinMode (speaker, OUTPUT);
pinMode (light, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

int mic = digitalRead (microphone);
int wat = digitalRead (watersensor);
int win = digitalRead(window);

if (mic == HIGH)

{
for (int k = 0; k <= 2000; k++) {

digitalWrite(speaker, HIGH); // her
digitalWrite(light, HIGH);
delay(1);
digitalWrite(speaker, LOW);
digitalWrite(light, LOW);
delay(1);
}
}
else {
digitalWrite(speaker, LOW);
digitalWrite(light, LOW);

}

if (wat == HIGH)
{
for (int k = 0; k <= 2000; k++) {

digitalWrite(speaker, HIGH);
digitalWrite(light, HIGH);
delay(1);
digitalWrite(speaker, LOW);
digitalWrite(light, LOW);
delay(1);
}
}
else {
digitalWrite(speaker, LOW);
digitalWrite(light, LOW);
}

if (win == LOW)
{
for (int k = 0; k <= 2000; k++) {

digitalWrite(speaker, HIGH);
digitalWrite(light, HIGH);
delay(1);
digitalWrite(speaker, LOW);
digitalWrite(light, LOW);
delay(1);
}
}
else {
digitalWrite(speaker, LOW);
digitalWrite(light, LOW);
}
}


I want the "if (mic == HIGH)", "if (win == LOW)" & "if (wat == HIGH)" toghether like "if (mic + win + war == HIGH)... but because the "Win" is on Low im running out i ideas to do it.

that way im assuming that i can get the code to end after the first

" else {
digitalWrite(speaker, LOW);
digitalWrite(light, LOW); "

Read the two posts!!!!!!!!!!!!!!!!!!

int mic = digitalRead (microphone);

What kind of microphone are you using? I've never heard of one connected to a digital pin.

 int win = digitalRead(window);

The windows in my house are not sensors.

Read this:

http://www.cplusplus.com/doc/tutorial/operators/

In particular, read the section on "logical operators".

JHR23:
I want the "if (mic == HIGH)", "if (win == LOW)" & "if (wat == HIGH)" toghether like "if (mic + win + war == HIGH)... but because the "Win" is on Low im running out i ideas to do it.

Perhaps something like:

int watersensor = 13;
int microphone = 12;
int window = 11;
int speaker = 3;
int light = 2;



void setup() {
 // put your setup code here, to run once:
 pinMode (watersensor, INPUT);
 pinMode (microphone, INPUT);
 pinMode (window, INPUT_PULLUP);
 pinMode (speaker, OUTPUT);
 pinMode (light, OUTPUT);
}

void loop() {
 int mic = digitalRead (microphone);
 int wat = digitalRead (watersensor);
 int win = digitalRead(window);

 if (mic == HIGH || wat == HIGH || win == LOW) // evaluate alert conditions
 {
   digitalWrite(light, HIGH);
   tone(speaker,500, 500); // background tone 500 Hz for 500 milliseconds
   delay(1000); // wait until tone() is finished after 500ms and another 500ms pause
   digitalWrite(light, LOW);
 }
 else 
 {
   digitalWrite(speaker, LOW);
   digitalWrite(light, LOW);
 }
}

Delta_G:
&& - Arduino Reference

Sounds like you need to become familiar with how to say AND and OR in the programming world.

Which in C++, can be "and" || "or". :wink:

Better still

const byte watersensor = 13;
const byte microphone = 12;
const byte window = 11;
const byte speaker = 3;
const byte light = 2;