Welcome! Log In Create A New Profile

Advanced

trying to control encoder

Posted by ag.chumpitazr 
trying to control encoder
October 04, 2016 06:04AM
First, sorry for m english...
I am using Arduino mega, full graphic smart controller and ramps 1.4; and i am trying to control encoder of the screen, I know that pins 33 -> CLK, 31->DT , 35 -> SW , but i am using this code but the result is a bucle where the number doesn't stop to increase or decrementing... someone could say me what is happen?? pls

#define outPutA 33 //35
#define outPutB 31

int counter = 0;
int aState=0;
int aLastState=0;

void setup(){
pinMode (outPutA, INPUT);
pinMode (outPutB, INPUT);

Serial.begin(9600);
aLastState = digitalRead(outPutA);

}

void loop(){
aState = digitalRead(outPutA);
if(aState != aLastState){
if(digitalRead(outPutB ) != aState){
counter=counter+1;
}else{
counter=counter-1;
}
Serial.print("Position: " );
Serial.println(counter);
}
aLastState = aState;
}

Edited 1 time(s). Last edit at 10/04/2016 06:04AM by ag.chumpitazr.
Re: trying to control encoder
October 04, 2016 12:22PM
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.


My printer: Raptosaur - Large Format Delta - [www.paulwanamaker.wordpress.com]
Can you answer questions about Calibration, Printing issues, Mechanics? Write it up and improve the Wiki!
Re: trying to control encoder
October 04, 2016 01:22PM
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.

What I want is that every time I move the encoder which is next to the screen, it prints the "angle" of encoder in the screen or in te serial monitor. like in this video :video minute 4:20 ; but using Arduino mega, full graphic smart controller and ramps 1.4.

Edited 2 time(s). Last edit at 10/04/2016 01:35PM by ag.chumpitazr.
Re: trying to control encoder
October 04, 2016 06:23PM
Ok, I looked at the video.

I misundersood a bit what it was doing - it should only be updating when the position changes. Having the code indented helps a lot in reading it.

The following is the code directly from his website which is a bit different from what you had. It may be you are reading from the wrong pin. I do not have ramps etc, so I can go no further.

/*     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(outputcool smiley != aState) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
 }
Sorry, only registered users may post in this forum.

Click here to login