22. pulsen/snelheid meten

Opto Coupler



aansluitschema:


Code:

const int buttonPin = 2;

int buttonState;

int lastButtonState = LOW;

long lastDebounceTime = 0;

long debounceDelay = 50;

int counter = 0;

int ledpin=13;

void setup() {

Serial.begin(9600);

pinMode(buttonPin, INPUT);

pinMode(ledpin,OUTPUT);

}

void loop() {

boolean pressed = debounce();

if (pressed == true) {

counter++;

Serial.print("aantal pulsen");

Serial.print (":");

Serial.println(counter);

if (counter==5)

{

digitalWrite(ledpin,HIGH);

}

if (counter<5|| counter>5)

{ digitalWrite (ledpin,LOW);

}

}

}

boolean debounce() {

boolean retVal = false;

int reading = digitalRead(buttonPin);

if (reading != lastButtonState) {

lastDebounceTime = millis();

}

if ((millis() - lastDebounceTime) > debounceDelay) {

if (reading != buttonState) {

buttonState = reading;

if (buttonState == HIGH) {

retVal = true;

}

}

}

lastButtonState = reading;

return retVal;

}

 



 


info:

 

 

library:

 

leveranciers:

 

magazijn: