Hi guys,
I have been planning to do a barn door tracker with LCD controls using Arduino Uno. I've seen photos and videos of people successfully interfacing both LCD and stepper motor with Arduino Uno so i decided to try it myself. I am a complete newbie in Arduino and i need some guidance and help here. I have successfully connect the Arduino Uno with a 12V 2A power supply with a NEMA 17 stepper motor controlled by A4988 driver, and a RepRap Full Graphic Smart Controller LCD (which requires ug8lib).
I have used the test codes from different sources and combined them together into one. The problem i'm facing here is when i turn the encoder, the numbers in LCD should update immediately but they have very very slow response or no response at all. Please help.
This is my code.
#include "U8glib.h"
#define encoderPin1 8
#define encoderPin2 9
#define pinEncButt 10
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
}
int dirPin =4;
int stepperPin = 5;
void setup(void)
{
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
Serial.begin(9600);
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);
pinMode(encoderPin2, INPUT);
pinMode(pinEncButt, INPUT);
digitalWrite(encoderPin1, HIGH);
digitalWrite(encoderPin2, HIGH);
digitalWrite(pinEncButt, HIGH);
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void step(boolean dir,int steps)
{
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++)
{
digitalWrite(stepperPin, HIGH);
delayMicroseconds(600);//Adjust the speed of motor. Increase the value, motor speed become slower.
digitalWrite(stepperPin, LOW);
delayMicroseconds(600);
}
}
void loop(void)
{
//steps per revolution for 200 pulses = 360 degree full cycle rotation
step(true,1000);//(direction ,steps per revolution). This is clockwise rotation.
delay(500);
step(false,1000);//Turn (direction ,steps per revolution). This is anticlockwise rotation.
delay(500);
Serial.println( digitalRead(pinEncButt) );
u8g.firstPage();
do { draw(); } while( u8g.nextPage() );
delay(50);
}
My setup is as the photo attached.
Thank you for the patience and i hope i could get some help here.
Have a great day ahead.
