void solve() { findAnswer = false; if(spiderDepth > pipe[spiderPipe].bottom || spiderDepth < pipe[spiderPipe].top) { return; } topLevel = pipe[0].top; memset(visited, false, sizeof(visited)); flowTime = 0; fillWater(0); }
int main(void) { //Variable Declarations initSolenoid(); /* initialize solenoid valve */ TIM_TIMERCFG_Type timerCfg; initTimeStruct(); RTC_TIME_Type* watertime = malloc(sizeof(RTC_TIME_Type)); uint8 fed = 0; uint8 watered = 0; watertime->HOUR = 5; watertime->MIN = 0; //Initialize timer0 for delays TIM_ConfigStructInit(TIM_TIMER_MODE, &timerCfg); /* initialize timer config struct */ TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &timerCfg); /* initialize timer0 */ //Initialize Real Time Clock RTC_Init(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); RTC_ResetClockTickCounter(LPC_RTC); // Initialize Peripherals INIT_SDRAM(); /* initialize SDRAM */ servoInit(); /* initialize FSR servo motor for panning camera */ initStepper(); /* initialize stepper motor for dispensing food */ initFSR(); /* initialize force sensitive resistor circuit for food and water full signals */ initWiFi(AUTO_CONNECT); /* initialize WiFi module -- must be attached*/ audio_initialize(); audio_reset(); //audio_test(); audio_setupMP3(); int i = 0, retval; uint32 length; /* length variable for photo */ printf("Entering while loop\n\r"); //audio_storeVoice(); // Enter an infinite loop while(1) { if(STATE == DISPENSING_FOOD){ printf("Entering food dispense state\n\r"); /* Execute commands to dispense food */ //spinUntilFull(); spinStepper(300); reverseSpin(250); STATE = CONNECTED; } if(STATE == DISPENSING_WATER){ printf("Entering water dispense state\n\r"); /* Execute commands to dispense water */ fillWater(); STATE = CONNECTED; } if(STATE == CAPTURING){ printf("Entering camera taking state\n\r"); /* Initialize camera and set it up to take a picture */ if(cameraInit()) printf("Camera not initialized!\n\r"); retval = stopFrame(); length = getBufferLength(); printf("length: %i\n\r", length); /* Send length to Android application */ int temp_len = length; while(temp_len){ uart1PutChar(temp_len % 10); temp_len = temp_len / 10; } /* Send photo and finish set up */ getAndSendPhoto(length); resumeFrame(); STATE = CONNECTED; } if(STATE == TALKING1){ audio_playVoice(1); STATE = CONNECTED; } if(STATE == TALKING2){ audio_playVoice(2); STATE = CONNECTED; } if(STATE == TALKING3){ audio_playVoice(3); STATE = CONNECTED; } if(STATE == PAN_LEFT){ /* Execute commands to pan servo left */ panServo(LEFT); STATE = CONNECTED; } if(STATE == PAN_RIGHT){ /* Execute commands to pan servo right */ panServo(RIGHT); STATE = CONNECTED; } if(STATE == SCHEDULING){ /* Execute commands to schedule a feeding time */ STATE = CONNECTED; } /* Scheduling */ RTC_GetFullTime(LPC_RTC, time); //Fill water bowl at predetermined time if (time->HOUR == watertime->HOUR + 1 && watered == 1) watered = 0; if (watertime->HOUR == time->HOUR && watertime->MIN < time->MIN && watered == 0) { fillWater(); watered = 1; } //Feed dog on schedule if any cannot feed dog two consecutive hours for(i = 0; i < scheduled_feeds; i++) { if (time->HOUR == feedtime[i]->HOUR + 1 && fed == 1) fed = 0; if (feedtime[i]->HOUR == time->HOUR && feedtime[i]->MIN < time->MIN && fed == 0) { spinUntilFull(); fed = 1; } } } return 0; }
int fillWater(int pipeIndex) { int currentPipeNumber = 1; int waterLevel = pipe[pipeIndex].bottom; visited[pipeIndex] = true; if(topLevel < pipe[pipeIndex].top) { topLevel = pipe[pipeIndex].top; } #ifdef DEBUG printf("Index: %d Top: %d Bottom %d\n", pipeIndex + 1, topLevel, waterLevel); #endif while(true) { for(int i=0; i<linkNumber; ++i) { if(link[i].depth == waterLevel) { for(int j=0; j<pipeNumber; ++j) { if(visited[j]) { int k; for(k=0; k<pipeNumber; ++k) { if((pipe[j].right == link[i].left && pipe[k].left == link[i].right) || (pipe[k].right == link[i].left && pipe[j].left == link[i].right)) { if(!visited[k]) { #ifdef DEBUG printf("LINK %d ", i + 1); #endif currentPipeNumber += fillWater(k); if(findAnswer) { return currentPipeNumber; } } else if(i == pipeIndex || j == pipeIndex) { return currentPipeNumber; } break; } } if(k < pipeNumber) { break; } } } break; } } if(waterLevel <= topLevel || spiderDepth <= topLevel) { findAnswer = false; return currentPipeNumber; } if(visited[spiderPipe] && waterLevel == spiderDepth) { findAnswer = true; return currentPipeNumber; } waterLevel--; flowTime += currentPipeNumber; #ifdef DEBUG printf("Index: %d Water: %d Time: %d Number: %d\n", pipeIndex + 1, waterLevel, flowTime, currentPipeNumber); #endif } return currentPipeNumber; }