Hello!
So I have already had some help from the forum. I am trying to make the led toggle with an PIR sensor (change the state of the led to the opposite when a movement is detected. led is off-movement-the led is on-movement- led off again etc). This is the code I have so far. There is a problem the led is constantly on so I wanted to see the reading on the serial screen and it is constantly at 0. I have no idea what is wrong with the code.
int inPin = A0; // the number of the input pin
int outPin = 10; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
int minval=0;//= the minimal value of the sensor- i dont know it yet)
int maxval=1023; //= maximum value of the sensor - i dont know it yet)
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
reading = digitalRead(inPin);
reading = map(1,minval,maxval,0,1);
Serial.println(reading);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == 1 && previous == 0 && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
Any help is big help
Thank You!