Advertisement
Guest User

Untitled

a guest
Aug 12th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <avr/power.h>
  2. #include <avr/pgmspace.h>
  3.  
  4. #define PIN_ENCODER_A 0
  5. #define PIN_ENCODER_B 2
  6. #define TRINKET_PINx  PINB
  7. #define PWM_PIN 1
  8. #define krok 9
  9.  
  10. #define CIELPWM(a) (pgm_read_word_near(CIEL8+a))
  11. const uint8_t CIEL8[] PROGMEM ={ 0,1,2,3,4,5,7,9,12,15,18,22,27,32,38,44,
  12.                                 51,58,67,76,86,96,108,120,134,148,163,180,
  13.                                 197,216,235,255};
  14.  
  15. static uint8_t enc_prev_pos = 0;
  16. static uint8_t enc_flags    = 0;
  17. short int stan_pwm =0;
  18.  
  19. void stepCorrectedPWM(int b) {
  20.   analogWrite(PWM_PIN,CIELPWM(b));
  21. }
  22.  
  23. void setup()
  24. {
  25.  if(F_CPU == 16000000)
  26.    clock_prescale_set(clock_div_1);
  27.   // set pins as input with internal pull-up resistors enabled
  28.   pinMode(PIN_ENCODER_A, INPUT);
  29.   pinMode(PIN_ENCODER_B, INPUT);
  30.   pinMode(PWM_PIN, OUTPUT);
  31.   digitalWrite(PIN_ENCODER_A, HIGH);
  32.   digitalWrite(PIN_ENCODER_B, HIGH);
  33.   analogWrite(PWM_PIN,0);
  34.  
  35.   // get an initial reading on the encoder pins
  36.   if (digitalRead(PIN_ENCODER_A) == LOW) {
  37.     enc_prev_pos |= (1 << 0);
  38.   }
  39.   if (digitalRead(PIN_ENCODER_B) == LOW) {
  40.     enc_prev_pos |= (1 << 1);
  41.   }
  42. }
  43.  
  44. void loop()
  45. {
  46.   int8_t enc_action = 0; // 1 or -1 if moved, sign is direction
  47.  
  48.   uint8_t enc_cur_pos = 0;
  49.   // read in the encoder state first
  50.   if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_A)) {
  51.     enc_cur_pos |= (1 << 0);
  52.   }
  53.   if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_B)) {
  54.     enc_cur_pos |= (1 << 1);
  55.   }
  56.  
  57.   // if any rotation at all
  58.   if (enc_cur_pos != enc_prev_pos)
  59.   {
  60.     if (enc_prev_pos == 0x00)
  61.     {
  62.       // this is the first edge
  63.       if (enc_cur_pos == 0x01) {
  64.         enc_flags |= (1 << 0);
  65.       }
  66.       else if (enc_cur_pos == 0x02) {
  67.         enc_flags |= (1 << 1);
  68.       }
  69.     }
  70.  
  71.     if (enc_cur_pos == 0x03)
  72.     {
  73.       // this is when the encoder is in the middle of a "step"
  74.       enc_flags |= (1 << 4);
  75.     }
  76.     else if (enc_cur_pos == 0x00)
  77.     {
  78.       // this is the final edge
  79.       if (enc_prev_pos == 0x02) {
  80.         enc_flags |= (1 << 2);
  81.       }
  82.       else if (enc_prev_pos == 0x01) {
  83.         enc_flags |= (1 << 3);
  84.       }
  85.  
  86.       // check the first and last edge
  87.       // or maybe one edge is missing, if missing then require the middle state
  88.       // this will reject bounces and false movements
  89.       if (bit_is_set(enc_flags, 0) && (bit_is_set(enc_flags, 2) || bit_is_set(enc_flags, 4))) {
  90.         enc_action = 1;
  91.       }
  92.       else if (bit_is_set(enc_flags, 2) && (bit_is_set(enc_flags, 0) || bit_is_set(enc_flags, 4))) {
  93.         enc_action = 1;
  94.       }
  95.       else if (bit_is_set(enc_flags, 1) && (bit_is_set(enc_flags, 3) || bit_is_set(enc_flags, 4))) {
  96.         enc_action = -1;
  97.       }
  98.       else if (bit_is_set(enc_flags, 3) && (bit_is_set(enc_flags, 1) || bit_is_set(enc_flags, 4))) {
  99.         enc_action = -1;
  100.       }
  101.  
  102.       enc_flags = 0; // reset for next time
  103.     }
  104.   }
  105.  
  106.   enc_prev_pos = enc_cur_pos;
  107.  
  108.   if (enc_action > 0) {
  109.    // Jaśniej
  110.     stan_pwm = stan_pwm + krok;
  111.    //   stan_pwm = stan_pwm++;
  112.   }
  113.   if (enc_action < 0) {
  114.    //ciemniej
  115.    stan_pwm = stan_pwm - krok;
  116.   //stan_pwm = stan_pwm--;
  117.   }
  118.   if (stan_pwm >= 255)
  119.            {
  120.             stan_pwm=255;
  121.            }
  122.  if (stan_pwm <=2)
  123.          {
  124.             analogWrite(PWM_PIN,0);
  125.          }
  126.  else
  127.          {        
  128.           stepCorrectedPWM(abs(stan_pwm/8));
  129.           }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement