Project Code
(Download "ELEC254_Demo_Project_Code.zip")
The following is "main.c":
/******************************************
* HKUST ELEC254 Project - Summer 2008
* Mobile Island: Sound Controlled Car
* Group #4: Wang Zhen, Zhu Shucheng, Zhuo Wei
******************************************/
#include "main.h"
// the pressed key
extern unsigned char Key;
//main function ================================================================
void main()
{
//initilization
motor_LCD_8255_initial();
LED8255_initial();
motor_initial();
keypad_initial();
//LCD_Init();
START:
//welcome message
//LCD_Welcome();
LED8255_on();
while(1) //wait for the "Enter" to go to the menu
{
Key = Keypad_Decoder();
if(Key == KEYPAD_ENTER)
{
//LCD_menu();
break;
}
}
//manu
while(1) //wait for the keypad input to choose different modes
{
LED8255_menu();
Key = Keypad_Decoder();
switch(Key)
{
case KEYPAD_ONE: //button 1: sound control mode
Mode1_Sound();
break;
case KEYPAD_TWO: //button 2: keypad control mode
Mode2_Keypad();
break;
case KEYPAD_THREE: //button 3: free run mode
Mode3_Free();
break;
case KEYPAD_FOUR: //button 4: show the welcome message
goto START;
break;
default: //other buttons ignored
break;
}
}
}
//mode 1: sound control mode =======================================================
void Mode1_Sound()
{
LED8255_mode1();
mic_init();
mic_sampleEnvironment();
while(1){
Key = Keypad_Decoder();
if (Key==KEYPAD_ENTER) {
//LCD_menu();
return;
}
mic_cont_check();
testResponse();
}
}
//mode 2: keypad control mode ======================================================
void Mode2_Keypad()
{
//LCD display
//LCD_Mode2_Keypad(5); // still car
LED8255_mode2();
//keypad control
while(1)
{
Key = Keypad_Decoder();
switch(Key)
{
case KEYPAD_ENTER: //go back to the menu
// LCD_menu();
return;
case KEYPAD_DOWN: //move backward
motor_backward();
// LCD_Mode2_Keypad(2);
break;
case KEYPAD_LEFT: //turn left
motor_left();
// LCD_Mode2_Keypad(4);
break;
case KEYPAD_RIGHT: //turn right
motor_right();
// LCD_Mode2_Keypad(6);
break;
case KEYPAD_UP: //move forward<
motor_forward();
// LCD_Mode2_Keypad(8);
break;
default: //stop
motor_stop();
// LCD_Mode2_Keypad(5);
break;
}
}
}
void Mode3_Free()
{
uint ttime;
LED8255_mode3();
//keep moving forward until seeing the obstacle
while(1)
{
Key = Keypad_Decoder();
if (Key==KEYPAD_ENTER) {return;}
//obstacle=ultrasound_obstacle();
ttime=ultrasound_everageTime();
if (ttime<25000)
{
Key = Keypad_Decoder();
if (Key==KEYPAD_ENTER) {return;}
if(ttime>10000){motor_backward_s(1);} //move back first
else{ motor_backward_s(2); }
Key = Keypad_Decoder();
if (Key==KEYPAD_ENTER) {return;}
motor_left_s(2); //then turn left
//keep moving
}
motor_forward_s(3);
motor_stop();
delay_ms(500);
}
}