Go Down

Topic: Sensor PIR + display seven leds counter (Read 398 times) previous topic - next topic

ivnnvi

Hi people!  8)
I need some help. I'm doing the arduino. I wanna use a sensor pir, and when every time that the sensor is active (sensorPir==HIGH) a counter plus one(+1). But the problem that i have is that the counter never stop. thnks.


Code: [Select]
int pirPin = 10;


int i =0;

void setup() {              

 Serial.begin(9600);
 pinMode(pirPin, INPUT);
 pinMode(ledPin, OUTPUT);
 digitalWrite(pirPin, LOW);
pinMode(2, OUTPUT);  
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);

}
void print_9(){
// write '9'
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
}

void print_8(){
// write '8'
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}

void print_7(){
// write '7'
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}

void print_6(){
// write '6'
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}

void print_5(){
// write '5'
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
}

void print_4(){
// write '4'
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
}

void print_3(){
// write '3'
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
}

void print_2(){
// write '2'
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 0);
}

void print_1(){
// write '1'
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}

void print_0(){
// write '0'
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
}

 
void loop(){

    if(digitalRead(pirPin) == HIGH){
     
        print_1();   //the led visualizes the sensors output pin state
     
    }
      else
      print_0();
 }



mkwired

The code that you posted does not use a counter.  Post the code that you are having a problem with.

PaulS

Quote
every time that the sensor is active (sensorPir==HIGH) a counter plus one(+1).

I don't think you want to increment the counter every time the sensor IS active. I think you want to increment the counter when the sensor BECOMES active. This requires that you keep track of the previous state of the sensor, to compare to the current state of the sensor. Only when the state changes, and the state is HIGH should you increment the counter.

Go Up