Arduino PROJECT HELP

I am making an arduino project for Control Systems.
My project is a home defense system which can be armed using a command via the serial monitor.
The coding is giving me trouble.
int message = 0; // This will hold one byte of the serial message
int redLEDPin = 11; // What pin is the red LED connected to?
int redLED = 0; // The value/brightness of the LED, can be 0-255
int laserPin = 5;
int laser = 0;
int irpPin = 3;
int irp = 0;
int gledPin = 4;
int gled = 0;
int buzzPin = 7;
int buzz = 0;

void setup() {
Serial.begin(9600); //set serial to 9600 baud rate
}

void loop(){
int sensorvalue = analogRead(A0);

if (Serial.available() > 0) { // Check if there is a new message
message = Serial.read(); // Put the serial input into the message

if (message == ‘R’){ // If a capitol R is received…
redLED = 255;
laser = 255;
irp = 255;
gled = 0;
while(true)
{if (sensorvalue < 400)
{while(true)
{digitalWrite(buzzPin,HIGH);}}
else
digitalWrite(buzzPin,LOW);}

}

if (message == ‘r’){ // If a lowercase r is received…
redLED = 0;
laser = 0;
irp = 0;
gled = 255;
// Set redLED to 0 (off)
}

}
analogWrite(redLEDPin, redLED);
analogWrite(laserPin, laser);
analogWrite(irpPin, irp);
analogWrite(gledPin, gled);
analogWrite(buzzPin, buzz);
}
This code does not operate properly when the serial monitor command R is typed in only the buzzer comes on. However, I want the command R to power up the IR Sensor, red led, and the laser, and if the ir sensor is triggered it should then turn on the buzzer.
The code below does everything i need it to do but does not activate the buzzer when the ir sensor is tripped.
int message = 0; // This will hold one byte of the serial message
int redLEDPin = 11; // What pin is the red LED connected to?
int redLED = 0; // The value/brightness of the LED, can be 0-255
int laserPin = 5;
int laser = 0;
int irpPin = 3;
int irp = 0;
int gledPin = 4;
int gled = 0;

void setup() {
Serial.begin(9600); //set serial to 9600 baud rate
}

void loop(){
if (Serial.available() > 0) { // Check if there is a new message
message = Serial.read(); // Put the serial input into the message

if (message == ‘R’){ // If a capitol R is received…
redLED = 255;
laser = 255;
irp = 255;
gled = 0;
}
if (message == ‘r’){ // If a lowercase r is received…
redLED = 0;
laser = 0;
irp = 0;
gled = 255;
// Set redLED to 0 (off)
}

}
analogWrite(redLEDPin, redLED);
analogWrite(laserPin, laser);
analogWrite(irpPin, irp);
analogWrite(gledPin, gled);
}
The bit of code regarding the analog read of the sensor works separately because I have used it to read the ir sensor value and activate the alarm but when the two are brought together only the buzzer sounds upon the R command and nothing else works
What can I do?

In the first code when you get the ‘R’ you put it into an infinite loop buzzing the buzzer. How do you expect anything else to ever happen? You’ve trapped yourself in the infinite while(true) loop.

It should be no surprise on the second code as there isn’t a line anywhere in there to read or react to an IR sensor.

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)

int message = 0;     //  This will hold one byte of the serial message
int redLEDPin = 11;   //  What pin is the red LED connected to?
int redLED = 0;          //  The value/brightness of the LED, can be 0-255
int laserPin = 5;
int laser = 0;
int irpPin = 3;
int irp = 0;
int gledPin = 4;
int gled = 0;
int buzzPin = 7;
int buzz = 0;

void setup() {  
  Serial.begin(9600);  //set serial to 9600 baud rate
}

void loop(){
int  sensorvalue = analogRead(A0);
 
    if (Serial.available() > 0) { //  Check if there is a new message
      message = Serial.read();    //  Put the serial input into the message

   if (message == 'R'){  //  If a capitol R is received...
     redLED = 255;
     laser = 255;
     irp = 255;
     gled = 0;
     while(true)
     {if (sensorvalue < 400)
     {while(true)
     {digitalWrite(buzzPin,HIGH);}}
     else
     digitalWrite(buzzPin,LOW);}
  
   }
   
   if (message == 'r'){  //  If a lowercase r is received...
     redLED = 0;
     laser = 0;
     irp = 0;
     gled = 255;
     //  Set redLED to 0 (off)
   }

 }   
analogWrite(redLEDPin, redLED);
analogWrite(laserPin, laser);
analogWrite(irpPin, irp);
analogWrite(gledPin, gled);
analogWrite(buzzPin, buzz);
}

Hopefully this posts it properly!
So again this is the code I would like to run as Delta G stated there is something wrong with my while true loop.
I know the second code doesnt read the IR sensor. I only posted it to show that it does all my desired functions but when I try to read the ir sensor it no longer does any of the desired functions.

How can I rewrite the first code to avoid the infinite true loop. Thanks for the help Delta G and Tom

TR94: How can I rewrite the first code to avoid the infinite true loop. Thanks for the help Delta G and Tom

Just use an if statement and rely on the fact that loop already loops. I don't really understand the purpose of your infinite while loop so I really can't help code around it. If it were my alarm is just set up a flag and set it true when the buzzer should buzz and put the buzz code in an if block testing that flag.

Where did you come up with this while(true) idea?
This:

while(true)
     {digitalWrite(buzzPin,HIGH);}

does exactly what it says… writes the buzzer pin forever. What else did you expect?

    while(true)
     {if (sensorvalue < 400)
     {while(true)
     {digitalWrite(buzzPin,HIGH);}}
     else
     digitalWrite(buzzPin,LOW);}

true is a keyword that is always going to be true. In a while() loop the part in the parentheses is where the test to see if the loop should go again is done. You are not performing such a test. You are keeping it true forever. if that part is true (non-zero) the loop will execute. Only if it is false (zero) will a while() loop break.

You need to provide a test and very likely reread some sensor or pin as a basis for the test. If you wish to break a while() loop based on something like a pin reading you need to do that reading in the while() loop.

Here’s a example using a variable

byte x = 100;
while (x) 
{
    Serial.println(x);
    x--;
}

That while() loop will end when x == 0. Personally I try to avoid while() loops on Arduino as one without exit conditions can cause grief.

the second while(true) is in there so that once the sensor is tripped the alarm continues to sound until the system is disarmed. even if I write the sensor reading section of code as a separate if statement, upon typing R into the serial monitor the buzzer goes off while none of the sensing components light up and try to sense. The R command immediately trips the buzzer.

Show us that code.

int sov = analogRead(soPin);
  
    if (Serial.available() > 0) { //  Check if there is a new message
      message = Serial.read();    //  Put the serial input into the message

   if (message == 'R'){  //  If a capitol R is received...
     redLED = 255; //  Set redLED to 255 (on)
     laser = 255;
     gled = 0;
     si = 255;
     {if (sov<500)
     {while(true);
     {digitalWrite(buzz,HIGH);}}
     else
     digitalWrite(buzz,LOW);}}
     
   
   if (message == 'r'){  //  If a lowercase r is received...
     redLED = 0; //  Set redLED to 0 (off)
     laser = 0; 
     gled = 255;
     si = 0;
     buzz = 0;
   }

I removed the while(true) loop, this now allows the R command to arm the system and turn on the IR sensor. However when the system is tripped it still does not turn on the buzzer.

while(true);

With that semicolon there, not only is this loop infinite but it is also empty. It means, "do nothing forever". Since the line to write the buzzer is after that I wouldn't expect the buzzer to buzz at all.

if (message == 'R'){  //  If a capitol R is received...
     redLED = 255;
     laser = 255;
     irp = 255;
     gled = 0;
     if (sensorvalue < 400)
     {digitalWrite(buzzPin,HIGH);}
     else
     digitalWrite(buzzPin,LOW);
  
   }
   
   if (message == 'r'){  //  If a lowercase r is received...
     redLED = 0;
     laser = 0;
     irp = 0;
     gled = 255;

accidentally posted the wrong code section. This is the current code with no while(trues);

When I run the sensor testing if statement separately in a new window it works properly, the sensor is tripped and the alarm then sounds till I reset the arduino.
So the code malfunction occurs when the two codes are combined.
(one code that turns alarm system on and off) and the other code (reads the IR sensor and trips alarm)

You've got the sensor test inside the test for the 'R'. Can you post the whole thing? Can't tell from this snippet if that's why it won't work or not. If it is like the code above where that is all inside a test for serial available it would mean there has to be new data on the serial line waiting in order to read your sensor.

Your use of braces is a bit unorthodox. Use Control-t to autoformat the code and indent the blocks and it may be easier for you to see the flow of the logic you have.

int message = 0;
int redLEDPin = 11;
int redLED = 0;
int laserPin = 5;
int laser = 0;
int irpPin = 3;
int irp = 0;
int gledPin = 4;
int gled = 0;
int buzzPin = 7;
int buzz = 0;

void setup() {
  Serial.begin(9600);

  void loop() {
    int  sensorvalue = analogRead(A0);

    if (Serial.available() > 0) {
      message = Serial.read();

      if (message == 'R') {
        redLED = 255;
        laser = 255;
        irp = 255;
        gled = 0;
        if (sensorvalue < 400)
        {
          digitalWrite(buzzPin, HIGH);
        }
        else
          digitalWrite(buzzPin, LOW);

      }

      if (message == 'r') {
        redLED = 0;
        laser = 0;
        irp = 0;
        gled = 255;

      }

    }
    analogWrite(redLEDPin, redLED);
    analogWrite(laserPin, laser);
    analogWrite(irpPin, irp);
    analogWrite(gledPin, gled);
    analogWrite(buzzPin, buzz);
  }

This is the Full code. This is my first bout with any type of coding so I apologize for all the craziness.

That won't compile. Missing the closing brace on setup. Hopefully a copy paste error. But do you see now how easy that is to spot with it formatted? Notice how the loop function starts out already indented?

And yes you definitely have the problem I thought. There are three things that must all be true for you to get a buzzer. There must be new serial data available, that data must be an 'R', and the sensor must read less than 400. Can you see the problem with that? You'd have to continually bombard the serial line with 'R' in order to ever run the sensor code.

Wow that is great. Moving the sensing code out of the initial if serial.read loop allows the sensor to set off the buzzer. The final step is to make the buzzer go off continually after the sensor is tripped as if it were a real alarm system that would continue to sound even when IR sensor was not reflecting light.

int message = 0;     //  This will hold one byte of the serial message
int redLEDPin = 11;   //  What pin is the red LED connected to?
int redLED = 0;          //  The value/brightness of the LED, can be 0-255
int laserPin = 5;
int laser = 0;
int irpPin = 3;
int irp = 0;
int gledPin = 4;
int gled = 0;
int buzzPin = 7;
int buzz = 0;

void setup() {
  Serial.begin(9600);  //set serial to 9600 baud rate
}

void loop() {
  int  sensorvalue = analogRead(A0);
  if (sensorvalue < 400)


  {
    digitalWrite(buzzPin, HIGH);
  }
  else
    digitalWrite(buzzPin, LOW);

  if (Serial.available() > 0) { //  Check if there is a new message
    message = Serial.read();    //  Put the serial input into the message

    if (message == 'R') { //  If a capitol R is received...
      redLED = 255;
      laser = 255;
      irp = 255;
      gled = 0;


    }

    if (message == 'r') { //  If a lowercase r is received...
      redLED = 0;
      laser = 0;
      irp = 0;
      gled = 255;
      //  Set redLED to 0 (off)
    }

  }
  analogWrite(redLEDPin, redLED);
  analogWrite(laserPin, laser);
  analogWrite(irpPin, irp);
  analogWrite(gledPin, gled);
  analogWrite(buzzPin, buzz);
}

Alright so this is the code as of now and if you can tell me how to make it continue to buzz even after the ir sensor is no long sensing under 400 that would be clutch.

Once you write that buzzer pin HIGH it will stay HIGH forever until you write it LOW or reset the board. You don't have to trap the code anywhere. If you don't want the buzzer to go LOW when the sensor value goes back above 400 then get rid of the else clause that turns it off after the if that turns it on.

I removed the else statement but this had no effect for the buzzer staying on. It still quits buzzing as soon as I remove my finger from the IR beam and the if sensing statement is still in the loop curly brackets.