trying to control encoder October 04, 2016 06:04AM |
Registered: 7 years ago Posts: 9 |
Re: trying to control encoder October 04, 2016 12:22PM |
Registered: 10 years ago Posts: 580 |
Re: trying to control encoder October 04, 2016 01:22PM |
Registered: 7 years ago Posts: 9 |
Quote
Paul Wanamaker
Looking at the code you posted
- It is an endless loop
- There is no wait between each time through, so it will try to output very fast. How many thousand times a second did you want it to read the inputs and write to the display?
- At 9600 baud the display will not be able to keep up. I'm not sure if the serial driver code will buffer it a bit. In any case you will be trying to cram a lot of info to the display at a very high rate.
Re: trying to control encoder October 04, 2016 06:23PM |
Registered: 10 years ago Posts: 580 |
/* Arduino Rotary Encoder Tutorial * * by Dejan Nedelkovski, www.HowToMechatronics.com * */ #define outputA 6 #define outputB 7 int counter = 0; int aState; int aLastState; 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(output != aState) { counter ++; } else { counter --; } Serial.print("Position: "); Serial.println(counter); } aLastState = aState; // Updates the previous state of the outputA with the current state }