Counting events, getting weird results from ++ ?


hi!

i'm working on project using pir sensor, lights led , counts number of events. after reaching number going happen (though that's not in code now).
however i'm getting weird numbers. instead of increasing one, expected too, increasing, in intervals don't understand pattern of. please point me in right direction..?

code: [select]



 
int ledpin = 13;                // pin led
int inputpin = 8;               // input pin pir sensor
int pirstate = low;             // no motion detected start with
int val = 0;                    // var reading pin status
int counter = 0;                // count events


void setup() {
  pinmode(ledpin, output);      // led output
  pinmode(inputpin, input);     // sensor input
  serial.begin(9600);
}

void loop(){
 
  val = digitalread(inputpin);   // read input value
  if (val == high) {                // check if input high
    digitalwrite(ledpin, high);  // turn led on
    counter++;
    delay(150);
   
    if (pirstate == low) {               
      serial.println("motion!");  // want print on output change, not state
      pirstate = high;
      serial.println(counter);
    }
   
  } else {
      digitalwrite(ledpin, low); // turn led off
      delay(300);   
      if (pirstate == high){
      // have turned of
      serial.println("motion ended!");
      // want print on output change, not state
      pirstate = low;
    }
  }

}


how pir wired ?  input pin floating @ unknown voltage or have got pulldown resistor in circuit ?


Arduino Forum > Using Arduino > Programming Questions > Counting events, getting weird results from ++ ?


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code