Merhabalar, Üzerinde çalıştığımız projede mekanik bir kol aynı strok mesafesinde bir sağa bir sola doğrusal hareket yapıyor. Mobilize bir buton tasarımı yaptım. Bunun için 1/2dk’da, 1 dk’da 2dk’da hareket sayısını ölçeceğiz. Butonlardaki bounce probleminden dolayı verimli sonuç alınamadı. Araştırırken debounce mantığını gördüm. Fakat temel bilgim eksik olduğu için debounce özelliği eklenilen kodu seri ekrana, dolayısıyla LCD’ye de aktaramadım. Birkaç varyasyon deniyorum ama nerde hata yaptığımı da çözemedim. Öncelikle normal buton counter kodu şu şekilde, onları yazayım; int state=LOW; int lastState=LOW; int count=0; void setup(){ Serial.begin(9600); pinMode(8, INPUT); state=digitalRead(8); } void loop(){ if (state==HIGH && lastState==LOW){ count++; Serial.println(count); } lastState=state; state=digitalRead(8); } Bulduğum debounce kodu da şu şekilde; const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // Variables will change: int ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin int count=0; //BENİM EKLEDİĞİM // the following variables are long’s because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { Serila.begin(9600); //BENİM EKLEDİĞİM pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin); // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you’ve waited // long enough since the last press to ignore any noise: // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it’s been there for longer // than the debounce delay, so take it as the actual current state: buttonState = reading; } // set the LED using the state of the button: digitalWrite(ledPin, buttonState); count++ //BENİM EKLEDİĞİM Serial.println(count); // BENİM EKLEDİĞİM // save the reading. Next time through the loop, // it’ll be the lastButtonState: lastButtonState = reading; } Sadece 13. ledi yakıyor buton aktif olduğunda. Bir öncekine bakarak counter olayını eklemeye çalıştım (BENİM EKLEDİĞİM) şeklinde de karşılarına yazdım. Bu şekilde seri ekranda karmaşık sayılar çıkıyor. 13.led de sürekli yanık. count kodunu ve seri ekrana aktarma işini debounce’lu code a nasıl uygulayacağım konusunda fikir verebilirseniz çok memnun olurum.
gerek kalmadı. şu koda rastladım. istediğim gibi çalıştırdı. eksiğini fazlasını şimdilik bilmiyorum. göz atmak isteyenler olursa, buyrun kod şu şekilde; include <liquidcrystal.h>
LiquidCrystal lcd(12, 11, 8, 7, 6, 5);
int ledPin = 13; // choose the pin for the LED
int switchPin =2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;
void setup() {
lcd.begin(16, 2);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(switchPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(switchPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED on
currentState = 1;
}
else {
digitalWrite(ledPin, LOW); // turn LED off
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
lcd.setCursor(0,1);
lcd.print(counter);
}
}
previousState = currentState;
delay(50);
}</liquidcrystal.h>
arduinoda yeniyim fakat bi miktar pic programlama bilgim var. üzerinden biraz zaman geçmiş ancak başkaları faydalansın diye belirtmek istedim. pushbuttonlarda bounce (yanılmıyorsam sıçrama) sorununu delay ile çözülür. ki ikinci kodda sona eklenmiş. ayrıca bu tür sayıcılarda optik sensör en iyi çözümdür aslında.cny70 kontrast sensörüyle ile güzel bi sensor devrem vardı şimdi bulamıyorum ilgilenen olur belirtirse bulur eklerim.
button kütüphanesini kullanrak deneyebilirsiniz.