Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int buttonPin1 = 6;
- constint MOTOR_RELAY = 7;
- int buttonState1 = HIGH;
- int lastButtonState1 = HIGH;
- unsigned long lastDebounceTime1 = 0;
- unsigned long debounceInterval = 50;
- volatile int rev = 0; //otacky, obsluzi interrupt
- volatile unsigned long cas_rutina = 0; //cas pre debounce po sebe iducich otacok
- void isr() {
- if ((millis() - cas_rutina) > 15 ) { //AK JE MEDZI OTACKAMI 15 ms, pripocitaj otacku
- rev++; //otacka +1
- cas_rutina = millis();
- }
- }
- void setup() {
- Serial.begin(9600);
- pinMode(buttonPin1, INPUT_PULLUP); //INPUT_PULLUP +5V vstup, normal HIGH
- pinMode(MOTOR_RELAY, OUTPUT); //RELE MOTORA AKO VYSTUP
- digitalWrite(MOTOR_RELAY, LOW); // NASTAVIME RELE MOTORA NA POCIATOCNU UROVEN
- }
- void loop() {
- int reading1 = digitalRead(buttonPin1);
- if (reading1 != lastButtonState1) { // ak zaznamenam novy stav tlacidla, t.j. ak bol stav HIGH a nastane LOW, alebo bol stav LOW a nastane HIGH
- lastDebounceTime1 = millis();
- }
- //DEBOUNCE TLACIDLA
- if ((millis() - lastDebounceTime1) > debounceInterval) {
- if (reading1 != buttonState1) {
- buttonState1 = reading1;
- if (buttonState1 == HIGH) { //Ak bolo tlacidlo stlacene a pustene
- Serial.println(F("Tlacidlo bolo stlacene, zapinam motor"));
- rev = 0; //vynulujeme pocet otacok
- attachInterrupt(digitalPinToInterrupt (2), isr, RISING); //interrupt pin na RISING hranu t.j. do LOG 1
- digitalWrite(MOTOR_RELAY, HIGH); //RELE MOTORA ZAPNUTE
- }
- }
- }
- lastButtonState1 = reading1; // nastavim stav tlacidla
- if (rev >= 10) {
- Serial.println(F("Motor vykonal 10 otacok, vypinam motor"));
- detachInterrupt(digitalPinToInterrupt(2)); //vypnutie prerusenia, interrupt nebude reagovať na dalsie otacky
- digitalWrite(MOTOR_RELAY, LOW); //RELE MOTORA VYPNUTE
- rev = 0; //vynulujeme pocet otacok
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement