Skip to content

Prototyping my controller

I have begun prototyping the controller for my project on breadboard.  The current system consists of an Arduino Uno, a 4×20 LCD screen, and a few buttons.  I have begun implementing the menu design seen in a previous post.  Currently I have built a basic menu, with > indicating the currently selected option.  The user can move up, down, or forwards in the menu.

As can be seen in the current video, after pressing the ok button, the menu moves fowards but does not redraw the > symbol.  The menu option being selected to move forwards on is also based on what is said on the main menu, not the current menu.  This is because I have coded these parts to be interwoven currently.  The following is extracts of my code:

int menuItem=0;
int menuItemOld=0;
int menuLevel=0;

char menu10[]=”Main menu           “;
char menu11[]=”Flight control      “;
char menu12[]=”Waypoints           “;
char menu13[]=”Power              “;

char menu20[]=”Flight mode         “;
char menu21[]=”Up/down mode        “;
char menu22[]=”Move mode           “;
char menu23[]=”Metrics            “;

char menu30[]=”Drop control        “;
char menu31[]=”Open/drop           “;
char menu32[]=”                    “;
char menu33[]=”Metrics            “;

char menu40[]=”Waypoints           “;
char menu41[]=”Waypoint 1          “;
char menu42[]=”Waypoint 2          “;
char menu43[]=”Metrics            “;

char menu50[]=”Power               “;
char menu51[]=”Controller power    “;
char menu52[]=”UAV power           “;
char menu53[]=”Metrics            “;

int sw = checkSwitches();
if (lastsw != sw){
lastsw = sw;

if (sw == switchback){
lcd.setCursor (0, 0);
lcd.print(“back pressed”);
}

if (sw == switchup){
menuItemOld=menuItem;
menuItem–;

if (menuItem<=0){menuItem=3;}
lcd.setCursor (0, menuItem);
lcd.print(“>”);
menuLevel=menuItem+1;
if (menuItemOld!=0){
lcd.setCursor (0, menuItemOld);
lcd.print(” “);
}
}

if (sw == switchdown){
// Serial.println(“Switch down pressed”);
//lcd.setCursor (0, 1);
//lcd.print(“DOWN pressed        “);
//light=20;
//lcd.setBacklight(HIGH);
menuItemOld=menuItem;
menuItem++;

if (menuItem==4){menuItem=1;}
lcd.setCursor (0, menuItem);
lcd.print(“>”);
menuLevel=menuItem+1;
if (menuItemOld!=0){
lcd.setCursor (0, menuItemOld);
lcd.print(” “);
}
}

if (sw == switchok){
Serial.println(“Switch ok pressed”);
menuLevel++;

if(menuLevel==1){
lcd.setCursor (0, 0);
lcd.print(menu10);

lcd.setCursor (1, 1);
lcd.print(menu11);
lcd.setCursor (1, 2);
lcd.print(menu12);
lcd.setCursor (1, 3);
lcd.print(menu13);
}

if(menuLevel==2){
lcd.setCursor (0, 0);
lcd.print(menu20);
lcd.setCursor (1, 1);
lcd.print(menu21);
lcd.setCursor (1, 2);
lcd.print(menu22);
lcd.setCursor (1, 3);
lcd.print(menu23);}

if(menuLevel==3){
lcd.setCursor (0, 0);
lcd.print(menu30);
lcd.setCursor (1, 1);
lcd.print(menu31);
lcd.setCursor (1, 2);
lcd.print(menu32);
lcd.setCursor (1, 3);
lcd.print(menu33);}

if(menuLevel==4){
lcd.setCursor (0, 0);
lcd.print(menu40);
lcd.setCursor (1, 1);
lcd.print(menu41);
lcd.setCursor (1, 2);
lcd.print(menu42);
lcd.setCursor (1, 3);
lcd.print(menu43);}

if(menuLevel==5){
lcd.setCursor (0, 0);
lcd.print(menu50);
lcd.setCursor (1, 1);
lcd.print(menu51);
lcd.setCursor (1, 2);
lcd.print(menu52);
lcd.setCursor (1, 3);
lcd.print(menu53);}

}
}

This code does not really work well for a menu system.  I have decided that I will redo my code, having all of my data stored in a 2D array (or 3D if you consider that strings in Arduino C are character arrays).  I will have the first dimension of the array signify the menu, and the second the line.  So, [1][2] would mean the second line of the first menu (“Flight Control”).  I will then have a much easier time navigating the menu system, and working out which option is currently highlighted.  I can also make use of a redraw function which can be passed the current menu value to draw.

While creating this code, I have learned about a peculiar feature of the screen’s library.  When redrawing the screen, the first element of the screen seemed to keep getting wiped.  I tried changing what data was being entered, shortening it, etc.  In the end it turns out that the last character I was trying to display on the screen was being looped around to go back to the start.  This is because I had put enough spaces in each string to make up to 20 characters (the width of the screen), but had not considered that I was drawing on the 2nd character of the line to create an indent.

I have also had general hardware issues due to wires moving/falling out, and have found having access to a multimeter to be very useful in debugging.  In general I am enjoying working with hardware for my project, and find it a lot more satisfying to have code that runs on an Arduino/makes a screen light up than having working code simply on the computer screen.

Published inBSc Dissertation

Be First to Comment

Leave a Reply

Your email address will not be published.