groundFungus:
I just checked my smart full graphic controller and the pullups are not installed on the board. You will need to supply them. Fortunately it is as easy as setting the pinMode for each of the encoder inputs (encoderPin1, encoderPin2, pinEncButt) to INPUT_PULLUP.
Thanks. I've changed them all to INPUT_PULLUP.
cattledog:
No, Pins 7 and 8 are not external interrupt pins on the UNO. See attachInterrupt() - Arduino ReferenceThere are three paths for you to take.
- change the external interrupt pins used by the encoder to 2 and 3. No major changes to your code are required.Just change the A and B pins to 2 and 3 from 7 and 8. Use INPUT_PULLUP as pin mode. Change the wiring to go to the correct pins.
I've selected path 1 to take since the interrupt pins on Uno is 2 and 3 and i don't really know any difficult codings. Thank you for your clear reference on the interrupt pins.
I now changed to motor movement and encoder function to change the direction of the motor. Now the LCD could work well on responsiveness, however, the motor shows lag/jittering movement instead of smooth movement with only 10 millis per step. I'm not sure whether is there any blocking in coding or not. I hope you guys could further guide me. By only uploading the sample code referred by Robin2, the motor could run smoothly. This code is a combination of LCD sample code and Robin2's sample code on stepper motor movement.
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
#include "U8glib.h"
#define encoderPin1 2
#define encoderPin2 3
byte directionPin = 8;
byte stepPin = 9;
byte pinEncButt = 4;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 10; // milliseconds
int flag =0;
int a=0;
int reading;
int zz=0;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
U8GLIB_ST7920_128X64_1X u8g(18, 16, 17); // SPI Com: en=18,rw=16,di=17
void draw(void) {
u8g.setFont(u8g_font_fub11);
u8g.drawStr( 0, 12, "Test Encoder");
u8g.setFont(u8g_font_fub20);
char buf[8];
sprintf(buf, "%d %d",digitalRead(pinEncButt), encoderValue);
u8g.drawStr( 10, 40, buf);
u8g.setFont(u8g_font_6x12);
u8g.drawStr( 0, 63, "www.mauroalfieri.it");
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1);
int LSB = digitalRead(encoderPin2);
int encoded = (MSB << 1) |LSB;
int sum = (lastEncoded << 2) | encoded;
if(sum==0b1101 || sum==0b0100 || sum==0b0010 || sum==0b1011) encoderValue++;
if(sum==0b1110 || sum==0b0111 || sum==0b0001 || sum==0b1000) encoderValue--;
lastEncoded = encoded; //store this value for next time
}
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with millis()");
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
if (u8g.getMode()==U8G_MODE_R3G3B2) { u8g.setColorIndex(255); }
else if (u8g.getMode()==U8G_MODE_GRAY2BIT) { u8g.setColorIndex(3); }
else if (u8g.getMode()==U8G_MODE_BW) { u8g.setColorIndex(1); }
else if (u8g.getMode()==U8G_MODE_HICOLOR) { u8g.setHiColorByRGB(255,255,255);}
pinMode(encoderPin1, INPUT_PULLUP);
pinMode(encoderPin2, INPUT_PULLUP);
pinMode(pinEncButt, INPUT_PULLUP);
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
digitalWrite(pinEncButt, HIGH);
digitalWrite(directionPin, LOW);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
singleStep();
u8g.firstPage();
do { draw(); } while( u8g.nextPage() );
}
void readButtons() {
reading = digitalRead(pinEncButt);
if (reading == HIGH && flag == 0) {
a=!a;
flag=1;
}
else if(reading==LOW){
flag=0;
}
if (a==1)
{
zz=1;;
}
else if (a==0)
{
zz=0;
}
}
void actOnButtons() {
if (zz == 1){
digitalWrite(directionPin, HIGH);
}
else if (zz == 0){
digitalWrite(directionPin, LOW);
}
Serial.println(digitalRead(directionPin));
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis += millisBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}