Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //define A pin for rotary encoder
- #define outputA 6
- //define B pin for rotary encoder
- #define outputB 7
- //define pin for pwm
- #define PWM_pin 10
- //define int for rotary encoder
- int counter = 80;
- int increment = 5;
- int maxpwm = 180;
- int minpwm = 35;
- int aState;
- int aLastState;
- int truepwm = 0;
- void setup() {
- pinMode (outputA,INPUT_PULLUP);
- pinMode (outputB,INPUT_PULLUP);
- pinMode (PWM_pin, OUTPUT);
- Serial.begin (115200);
- // Reads the initial state of the outputA
- aLastState = digitalRead(outputA);
- //Speed up gently on firststart
- for (int i = 25; i <= counter; i++) {
- analogWrite(PWM_pin, i);
- Serial.println(i);
- delay(50);
- }
- analogWrite(PWM_pin, counter);
- Serial.println("Finished Start Sq ");
- Serial.print(counter);
- }
- void loop() {
- //Rotary encoder things
- aState = digitalRead(outputA); // Reads the "current" state of the outputA
- // If the previous and the current state of the outputA are different, that means a Pulse has occured
- if (aState != aLastState){
- // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
- if (digitalRead(outputB) != aState) {
- if (counter >= maxpwm) {
- counter = maxpwm;
- } else {
- counter = counter + increment;
- }
- } else {
- if (counter <= minpwm) {
- counter = minpwm;
- } else {
- counter = counter - increment;
- }
- }
- }
- aLastState = aState; // Updates the previous state of the outputA with the current state
- //Set Speed
- analogWrite(PWM_pin, counter);
- Serial.println(counter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement