int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int sensorVal = 0;
int ledpin=4; // variable to store the sensor status (value)
void setup()
{
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT);
pinMode(ledpin, OUTPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop()
{
sensorVal = digitalRead(sensor); // read sensor value
if (sensorVal == HIGH) // check if the sensor is HIGH
{
digitalWrite(led, HIGH); // turn LED ON
Serial.println("Motion detected!");
delay(1000);
}
else
{
digitalWrite(led, LOW); // turn LED OFF
Serial.println("Motion stopped!");
delay(1000);
}
if(sensorVal==HIGH)
{
digitalWrite(ledpin,HIGH);
delay(1000);
}
if(sensorVal==LOW)
{
digitalWrite(ledpin, LOW);
delay(1000);
}
}