Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/power.h>
- #include <avr/pgmspace.h>
- #define PIN_ENCODER_A 0
- #define PIN_ENCODER_B 2
- #define TRINKET_PINx PINB
- #define PWM_PIN 1
- #define krok 9
- #define CIELPWM(a) (pgm_read_word_near(CIEL8+a))
- const uint8_t CIEL8[] PROGMEM ={ 0,1,2,3,4,5,7,9,12,15,18,22,27,32,38,44,
- 51,58,67,76,86,96,108,120,134,148,163,180,
- 197,216,235,255};
- static uint8_t enc_prev_pos = 0;
- static uint8_t enc_flags = 0;
- short int stan_pwm =0;
- void stepCorrectedPWM(int b) {
- analogWrite(PWM_PIN,CIELPWM(b));
- }
- void setup()
- {
- if(F_CPU == 16000000)
- clock_prescale_set(clock_div_1);
- // set pins as input with internal pull-up resistors enabled
- pinMode(PIN_ENCODER_A, INPUT);
- pinMode(PIN_ENCODER_B, INPUT);
- pinMode(PWM_PIN, OUTPUT);
- digitalWrite(PIN_ENCODER_A, HIGH);
- digitalWrite(PIN_ENCODER_B, HIGH);
- analogWrite(PWM_PIN,0);
- // get an initial reading on the encoder pins
- if (digitalRead(PIN_ENCODER_A) == LOW) {
- enc_prev_pos |= (1 << 0);
- }
- if (digitalRead(PIN_ENCODER_B) == LOW) {
- enc_prev_pos |= (1 << 1);
- }
- }
- void loop()
- {
- int8_t enc_action = 0; // 1 or -1 if moved, sign is direction
- uint8_t enc_cur_pos = 0;
- // read in the encoder state first
- if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_A)) {
- enc_cur_pos |= (1 << 0);
- }
- if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_B)) {
- enc_cur_pos |= (1 << 1);
- }
- // if any rotation at all
- if (enc_cur_pos != enc_prev_pos)
- {
- if (enc_prev_pos == 0x00)
- {
- // this is the first edge
- if (enc_cur_pos == 0x01) {
- enc_flags |= (1 << 0);
- }
- else if (enc_cur_pos == 0x02) {
- enc_flags |= (1 << 1);
- }
- }
- if (enc_cur_pos == 0x03)
- {
- // this is when the encoder is in the middle of a "step"
- enc_flags |= (1 << 4);
- }
- else if (enc_cur_pos == 0x00)
- {
- // this is the final edge
- if (enc_prev_pos == 0x02) {
- enc_flags |= (1 << 2);
- }
- else if (enc_prev_pos == 0x01) {
- enc_flags |= (1 << 3);
- }
- // check the first and last edge
- // or maybe one edge is missing, if missing then require the middle state
- // this will reject bounces and false movements
- if (bit_is_set(enc_flags, 0) && (bit_is_set(enc_flags, 2) || bit_is_set(enc_flags, 4))) {
- enc_action = 1;
- }
- else if (bit_is_set(enc_flags, 2) && (bit_is_set(enc_flags, 0) || bit_is_set(enc_flags, 4))) {
- enc_action = 1;
- }
- else if (bit_is_set(enc_flags, 1) && (bit_is_set(enc_flags, 3) || bit_is_set(enc_flags, 4))) {
- enc_action = -1;
- }
- else if (bit_is_set(enc_flags, 3) && (bit_is_set(enc_flags, 1) || bit_is_set(enc_flags, 4))) {
- enc_action = -1;
- }
- enc_flags = 0; // reset for next time
- }
- }
- enc_prev_pos = enc_cur_pos;
- if (enc_action > 0) {
- // Jaśniej
- stan_pwm = stan_pwm + krok;
- // stan_pwm = stan_pwm++;
- }
- if (enc_action < 0) {
- //ciemniej
- stan_pwm = stan_pwm - krok;
- //stan_pwm = stan_pwm--;
- }
- if (stan_pwm >= 255)
- {
- stan_pwm=255;
- }
- if (stan_pwm <=2)
- {
- analogWrite(PWM_PIN,0);
- }
- else
- {
- stepCorrectedPWM(abs(stan_pwm/8));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement