Rotary Encoder in degrees

Hello, I have a Rotary Encoder that I am using with my Arduino 101. I have it wired up fine, and I can get a count of how many clicks either forwards or backwards, but I can't seem to get this into degrees. I multiplied the count by 9, so it is in degrees, and I can set it to zero, but I can't get both values stuck out of zero if I go past 360.

Here is my code:

/*     Arduino Rotary Encoder Tutorial
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  
 */
 
 #define outputA 6
 #define outputB 7

 int counter = 0; 
 int aState;
 int aLastState;  
 int angle = 0;
 void setup() { 
   pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   Serial.begin (9600);
   // Reads the initial state of the outputA
   aLastState = digitalRead(outputA);   
 } 

 void loop() { 
   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) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
     Serial.print("Angle: ");
     angle = (counter*9);
     if (angle >= 360){
      angle = 0;
     }
     Serial.println(angle);
   } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
 }

Here is something I would get if I turned it past 360:
Position: 62
Angle: 0

Thank You for your help.

 if (angle >= 360){
      angle = 0;
     }

If it goes past 360, that code makes it zero. If angle is greater than 360, what do you want to display? Say that angle is 375, do you want angle to be 375 or do you want it to be 15 (375-360)?

Look up the stepper motor "step angle"

Step Angle in Stepper Motor. Definition: Step angle is defined as the angle which the rotor of a stepper motor moves when one pulse is applied to the input of the stator. The positioning of a motor is decided by the step angle and is expressed in degrees.

The step angle should help with your calculations.

If it is 375 degrees, I want it to display 15 degrees. Likewise. if it is -375 degrees, I want it to show -15. Also, I know 1.8's significance - it makes 200(steps)*1.8 = 360. 9 does the same thing. However, what do I do to make the variable reset to zero, than keep counting?

The modulo function might be useful.

Idahowalker:
Look up the stepper motor "step angle"

The step angle should help with your calculations.

Tbot1000:
If it is 375 degrees, I want it to display 15 degrees. Likewise. if it is -375 degrees, I want it to show -15. Also, I know 1.8's significance - it makes 200(steps)*1.8 = 360. 9 does the same thing. However, what do I do to make the variable reset to zero, than keep counting?

You, quite nicely, describe what you want to do.

A few conditionals will follow the rules as described. Something like if greater than 325 do some math thingies else if less then -375 then do some other math thingies.

Also the variable could be rest to 0 with a conditional such as if xxx > some number then x = 0.

I just found something AMAZING!!!:

This will solve all of my problems!
I am really using the encoders for a wind direction sensor, so I need N, NE, S, SE, E, W, SW, NW.
Thank You though, for all of your help.

Those switches have detents, so your wind direction indicator may not be able to turn the shaft.

Rotary encoders eventually loose track of the zero position.
You need an absolute encoder.
4-bit Grey-code for your 16 wind directions.
Leo..