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?