Advertisement
Guest User

Rele, motor - Arduino, interrupt, 10 revs

a guest
Jan 7th, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buttonPin1 = 6;
  2. constint MOTOR_RELAY = 7;
  3.  
  4. int buttonState1 = HIGH;
  5. int lastButtonState1 = HIGH;
  6. unsigned long lastDebounceTime1 = 0;
  7. unsigned long debounceInterval = 50;
  8.  
  9. volatile int rev = 0; //otacky, obsluzi interrupt
  10. volatile unsigned long cas_rutina = 0; //cas pre debounce po sebe iducich otacok
  11.  
  12. void isr() {
  13.   if ((millis() - cas_rutina) > 15 ) {  //AK JE MEDZI OTACKAMI 15 ms, pripocitaj otacku
  14.     rev++; //otacka +1
  15.     cas_rutina = millis();
  16.   }
  17. }
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21.   pinMode(buttonPin1, INPUT_PULLUP); //INPUT_PULLUP +5V vstup, normal HIGH
  22.   pinMode(MOTOR_RELAY, OUTPUT); //RELE MOTORA AKO VYSTUP
  23.   digitalWrite(MOTOR_RELAY, LOW); // NASTAVIME RELE MOTORA NA POCIATOCNU UROVEN
  24. }
  25.  
  26. void loop() {
  27.   int reading1 = digitalRead(buttonPin1);
  28.   if (reading1 != lastButtonState1) { // ak zaznamenam novy stav tlacidla, t.j. ak bol stav HIGH a nastane LOW, alebo bol stav LOW a nastane HIGH
  29.     lastDebounceTime1 = millis();
  30.   }
  31.  
  32.   //DEBOUNCE TLACIDLA
  33.   if ((millis() - lastDebounceTime1) > debounceInterval) {
  34.     if (reading1 != buttonState1) {
  35.       buttonState1 = reading1;
  36.       if (buttonState1 == HIGH) { //Ak bolo tlacidlo stlacene a pustene
  37.         Serial.println(F("Tlacidlo bolo stlacene, zapinam motor"));
  38.         rev = 0; //vynulujeme pocet otacok
  39.         attachInterrupt(digitalPinToInterrupt (2), isr, RISING); //interrupt pin na RISING hranu t.j. do LOG 1
  40.         digitalWrite(MOTOR_RELAY, HIGH); //RELE MOTORA ZAPNUTE
  41.       }
  42.     }
  43.   }
  44.   lastButtonState1 = reading1; // nastavim stav tlacidla
  45.   if (rev >= 10) {
  46.     Serial.println(F("Motor vykonal 10 otacok, vypinam motor"));
  47.     detachInterrupt(digitalPinToInterrupt(2)); //vypnutie prerusenia, interrupt nebude reagovať na dalsie otacky
  48.     digitalWrite(MOTOR_RELAY, LOW); //RELE MOTORA VYPNUTE
  49.     rev = 0; //vynulujeme pocet otacok
  50.   }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement