Hello Forum....
i dont exactly know how to explain this, and i have no experience programming with C or any other language other than Arduino. Ive used my Arduino for a while, but i set it aside for a good 6ish months and im starting to fiddle with it again. Im working on a project to use a Parallax Passive IR Sensor to tell an Arduino communicating with a Processing sketch to update my cat's Twitter status when he goes to eat.
absolutely pointless i know, but its fun.
thats why i use the Arduino, its fun in a nerdy way, which i love, and i learn while doing it and enjoy it. Ive just started getting into using Processing and i have some of my basic code written out, but i digress. On to the background behind my question.
BACKGROUND BEHIND QUESTION:
I have the Arduino reading a digital value, either HIGH or LOW from the PIR to determine whether there is motion. The data pin from the PIR is connected to digital pin 2. It reads a value from the PIR every 2 seconds. When the arduino reads a HIGH value it will output a "1" to the serial port. Just for ease of paying attention while keeping my cat from eating my cords, i also have it output a message "movement detected". Now, when it doesn't detect a HIGH value, it doesnt output anything.
Now i dont want to have my Twitter App overloaded with status updates, so i want to create a function/loop/statement (i dont know the correct term) that checks how long its been since the last time its received a HIGH value. The reason for this is the PIR, when it detects motion it turns on its own onboard red LED and it stays on for about 2-3 seconds, so for 2-3 seconds the serial port is receiving "1"s and "movement detected" messages. That means that if i finished this app everytime my cat went to eat there would be about 3 or 4 status updates in a row saying, im eating, which would be stupid. My question is....
QUESTION:
How can i create a function/loop/statement (i dont know the correct term) that checks how long its been since the last time its received a HIGH value, and depending on whether the function/loop/statement is true, it will output a message to the serial port?
Oh and here is my code for anyone who wants to review it....its simple and to the point.
int sensorPin = 2; // select the input pin for motion sensor
int sensorValue = 0; // variable to store the value coming from the sensor
int activationPin = 13; //light up a giant matte red LED when motion is detected for shits and giggles.
void setup() {
Serial.begin(9600);
pinMode(activationPin, OUTPUT);
pinMode(sensorPin, INPUT);
}
void loop() {
sensorValue = digitalRead(sensorPin); // read the input pin
delay(2000); // wait a bit
if (sensorValue == HIGH) {
Serial.println("1"); // data that will this trigger status update --> yummmm....nom nom time! =) - example status update sent by processing
delay(1000); //wait a bit
Serial.println("movement detected"); // let me know movement was detected
digitalWrite(activationPin, HIGH); // light up an LED in case im busy
delay(250); // wait a bit
digitalWrite(activationPin, LOW); // turn off the LED
}
else (sensorValue == LOW ); { // if the sensor value is low, dont do shit.
}
}