miklik

megapi_encodcmotor1

Aug 6th, 2020
1,590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <MeMegaPiPro.h>
  2.  
  3. const byte interruptPin =18;
  4. const byte NE1=31;
  5. long count=0;
  6. unsigned long time;
  7. unsigned long last_time;
  8.  
  9. MeMegaPiDCMotor motor1(PORT1B);
  10. int motorSpeed = 100;
  11.  
  12. void setup(){
  13.   pinMode(interruptPin, INPUT_PULLUP);
  14.   pinMode(NE1, INPUT);
  15.   attachInterrupt(digitalPinToInterrupt(interruptPin), blink,RISING);
  16.   Serial.begin(9600);
  17. }
  18. void loop(){
  19.   motor1.run(motorSpeed);       // value: between -255 and 255
  20.   time =millis();
  21.   if(time-last_time>1000) {
  22.      Serial.println(count);
  23.      last_time=time;
  24.      if(abs(count) > 1000) {
  25.         motorSpeed = -1 * motorSpeed;
  26.         Serial.print("M ");
  27.         Serial.println(motorSpeed);
  28.      }        
  29.   }
  30. }
  31.  
  32. void blink(){
  33.    if (digitalRead(NE1)>0)
  34.       count++;
  35.    else
  36.       count--;
Advertisement
Add Comment
Please, Sign In to add comment