GLCD motor control monitor

So I got that idea of creating an interface to control the speed of a stepper motor over arduino mega 2560 while monitoring the progress over an LCD..

the setup is pretty straight forward:
FILE0217

Also here is a video demonstration:

Bellow i attached the source code:
1) it is plug and play..just make sure you got the wiring correct.
2) if you got questions, drop a comment bellow, and i will answer it.

/*OBJECTIVE: creat menu to control 3 motors' speed*/

/*IN THIS BACKUP:
creating the motor control menu:
Motor_X |------Progress bar--------|
Motor_y |------Progress bar--------|
Motor_Z |------Progress bar--------|
EDIT: add the knob feature and the select button(scroll up/down)
EDIT: add motor shield variables/parameters and control the motors(1 motor since current problem)
*/

#include <glcd.h>
#include "fonts/allFonts.h" // system and arial14 fonts are used
#include "bitmaps/allBitmaps.h" // all images in the bitmap dir 
gText textArea;

#define KNOB 0
# define ESC 3
# define UP 4
# define DOWN 5
# define RIGHT 6
# define LEFT 7

#define DIR_X 8
#define STP_X 9
#define DIR_Y 10
#define STP_Y 11
#define DIR_Z 12
#define STP_Z 13

#define PROGRESS_BAR_LENGTH 60 //used in showMenu
#define PROGRESS_BAR_HIGHT 7 //used in showMenu

 /*Variables*/
boolean emptyScreen = false, alreadyCleared = true;
boolean up,down,right, left; 
double knob =0.0;
String s1 = "Motor_X", s2 = "Motor_Y", s3 = "Motor_Z" ,s4 = "Esc";
int down_key_clicks_count = 0;
void setup()
{
 Serial.begin(9600);
 GLCD.Init(); //initialise the library, non inverted writes pixels onto a clear screen 
 GLCD.ClearScreen(); 
 GLCD.SelectFont(System5x7, BLACK); //font for the default text a-rea 
 
 pinMode(KNOB,INPUT);//knob(potentumeter)
 pinMode(DIR_X,OUTPUT);
 pinMode(STP_X,OUTPUT);
 pinMode(DIR_Y,OUTPUT);
 pinMode(STP_Y,OUTPUT);
 
 digitalWrite(DIR_X,LOW);
 digitalWrite(STP_X,LOW);
 digitalWrite(DIR_Y,LOW);
 digitalWrite(STP_Y,LOW); 
}

void loop(){
 if(digitalRead(ESC)==HIGH){
 GLCD.ClearScreen(); 
 emptyScreen =true;
 alreadyCleared = true;
 }
 showMenu();
}
//------------------------------------------------------------------------------------------------------------------------------------
void showMenu(){ 
 showArea(textAreaRIGHT, "Motor Ctrl");
 /*listing menu items:*/
 //"left" part''
 textArea.DefineArea(textAreaRIGHT);
 textArea.SetFontColor(BLACK); 
 textArea.DrawString(s1, 2,12); //Motor_X 
 textArea.DrawString(s2, 2,23); //Motor_Y
 textArea.DrawString(s3, 2,34); //Motor_Z
 textArea.DrawString(s4, 2,51); //Esc
 
 //progress bar:
 GLCD.DrawRect(0,12,PROGRESS_BAR_LENGTH, PROGRESS_BAR_HIGHT); //X axis
 GLCD.DrawRect(0,23,PROGRESS_BAR_LENGTH, PROGRESS_BAR_HIGHT); //Y axis
 GLCD.DrawRect(0,34,PROGRESS_BAR_LENGTH, PROGRESS_BAR_HIGHT); //Z axis
 
 read_data();
 char axis = down_key_clicks_count%3 + 'x'; 
 scroll(knob,'x'); 
}//end showMenu()
//------------------------------------------------------------------------------------------------------------------------------------
void scroll(double knob, char axis ){ 
 double progress = 0.0; //the amount to fill in the progress bar
 progress = (knob/1024)*PROGRESS_BAR_LENGTH;
 Serial.print(axis);Serial.print(" "); Serial.print(knob); Serial.print(" "); Serial.print(progress); Serial.print(" "); Serial.println(down); 
 switch(axis){
 case 'x':
 GLCD.FillRect(1,13,progress,PROGRESS_BAR_HIGHT-2);
 GLCD.FillRect(progress,13,PROGRESS_BAR_LENGTH-progress,PROGRESS_BAR_HIGHT-2,WHITE);
 GLCD.FillRect(1,24,PROGRESS_BAR_LENGTH-1,PROGRESS_BAR_HIGHT-2,WHITE);
 GLCD.FillRect(1,35,PROGRESS_BAR_LENGTH-1,PROGRESS_BAR_HIGHT-2,WHITE);
 break;
 case 'y': 
 GLCD.FillRect(1,24,progress,PROGRESS_BAR_HIGHT-2);
 GLCD.FillRect(progress,24,PROGRESS_BAR_LENGTH-progress,PROGRESS_BAR_HIGHT-2,WHITE);
 GLCD.FillRect(1,13,PROGRESS_BAR_LENGTH-1,PROGRESS_BAR_HIGHT-2,WHITE);
 GLCD.FillRect(1,35,PROGRESS_BAR_LENGTH-1,PROGRESS_BAR_HIGHT-2,WHITE); 
 break;
 case 'z': 
 GLCD.FillRect(1,35,progress,PROGRESS_BAR_HIGHT-2);
 GLCD.FillRect(progress,35,PROGRESS_BAR_LENGTH-progress,PROGRESS_BAR_HIGHT-2,WHITE);
 GLCD.FillRect(1,24,PROGRESS_BAR_LENGTH-1,PROGRESS_BAR_HIGHT-2,WHITE);
 GLCD.FillRect(1,13,PROGRESS_BAR_LENGTH-1,PROGRESS_BAR_HIGHT-2,WHITE); 
 break;
 }//end switch(axis)
 opertare_motors(PROGRESS_BAR_LENGTH-progress,axis);
}//end scroll
//------------------------------------------------------------------------------------------------------------------------------------
void showArea(predefinedArea area, char *description)
{
 textArea.DefineArea(area);
 textArea.SelectFont(SystemFont5x7);
 textArea.SetFontColor(WHITE); 
 textArea.DrawString(description, 0,0);
 textArea.SetFontColor(BLACK); 
}
//------------------------------------------------------------------------------------------------------------------------------------
void read_data(){ 
 knob = analogRead(KNOB);
 //reading all the button states and knobs and what not..
}//end read_data()
//------------------------------------------------------------------------------------------------------------------------------------
void opertare_motors(int progress,char axis){
 switch(axis){
 case 'x':
 digitalWrite(STP_X,HIGH);
 delay(progress+1);
 digitalWrite(STP_X,LOW); 
 break;
 case 'y':
 digitalWrite(STP_Y,HIGH);
 delay(progress+1);
 digitalWrite(STP_Y,LOW); 
 break;
 case 'z':
 digitalWrite(STP_Z,HIGH);
 delay(progress+1);
 digitalWrite(STP_Z,LOW);
 break; 
 }//end switch(axis)
}//end operate_motors

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: