赞
踩
提示:记录毕设
基于STM32的智能药盒,主控使用STM32F103(正点原子战舰板子)
该药盒可避免其忘记服药、服药拖延、反复服药等状况,让患者准确无误的做到遵守医嘱按时服药。
QQ2424644692 volatile u8 t=0; volatile u8 i=0; volatile u8 key; volatile u16 led0pwmval = 499; volatile u16 adcx0; volatile u16 adcx1; void main() { if (Serial.available()) { char message = ' '; message = Serial.read(); if (message == 't') { if (!isTaskTake) { isTaskTake = true; Serial.println("*************\n*开始取药过程*\n*************"); // xTaskCreate(taskTakePills, "taskTakePills", 20000, // pillsParameters, 1, NULL); takePills(3, 1); } else { Serial.println("已经有一个取药任务了"); } } if (message == 'i') { //开盖 if (!isTaskTake) { isTaskTake = true; taskOpenBox(NULL); isTaskTake = false; } else { Serial.println("已经有一个取药任务了"); } } if (message == 'j') { //关盖 if (!isTaskTake) { isTaskTake = true; taskCloseBox(NULL); digitalWrite(PIN_ENABLE, HIGH); isTaskTake = false; } else { Serial.println("已经有一个取药任务了"); } } if (message == 'm') { //关盖 if (!isTaskTake) { isTaskTake = true; digitalWrite(PIN_ENABLE, LOW); takePillsTest(); digitalWrite(PIN_ENABLE, HIGH); isTaskTake = false; } else { Serial.println("已经有一个取药任务了"); } } if (message == 'g') { //左旋 if (!isTaskTake) { isTaskTake = true; digitalWrite(PIN_ENABLE, LOW); taskRotate(LEFT); boxdisplacement--; digitalWrite(PIN_ENABLE, HIGH); isTaskTake = false; } else { Serial.println("已经有一个取药任务了"); } } if (message == 'h') { //右旋 if (!isTaskTake) { isTaskTake = true; digitalWrite(PIN_ENABLE, LOW); taskRotate(RIGHT); boxdisplacement++; digitalWrite(PIN_ENABLE, HIGH); isTaskTake = false; } else { Serial.println("已经有一个取药任务了"); } } if (message == 'k') { if (!isTaskTake) { isTaskTake = true; Serial.println("开始推出药盒"); digitalWrite(PIN_ENABLE, LOW); taskPushBoxOut(NULL); delay(1000); Serial.println("开始推入药盒"); taskPullBoxIn(NULL); digitalWrite(PIN_ENABLE, HIGH); isTaskTake = false; } else { Serial.println("已经有一个取药任务了"); } } if (message == '3') { taskPillsOut(NULL); } if (message == 'a') { //开气泵 openPump(); } if (message == 'b') { //关气泵 closePump(); } } } void rotateToOpen(int boxName) { int currentPosition = boxName + boxdisplacement; int targetRotation = OPEN_POSITION - currentPosition; if (targetRotation >= 0) { #ifdef DEBUG Serial.println("rorate right " + String(targetRotation) + " step to open."); Serial.println("向右旋转 " + String(targetRotation) + " 步以开盖."); #endif for (int i = 0; i < targetRotation; i++) { taskRotate(RIGHT); boxdisplacement++; } } else { #ifdef DEBUG Serial.println("rorate left " + String(-targetRotation) + " step to open."); Serial.println("向左旋转 " + String(-targetRotation) + " 步以开盖."); #endif for (int i = 0; i < -targetRotation; i++) { taskRotate(LEFT); boxdisplacement--; } } } void rotateToClose(int boxName) { int currentPosition = boxName + boxdisplacement; int targetRotation = OPEN_POSITION - currentPosition; if (targetRotation >= 0) { #ifdef DEBUG Serial.println("rorate right " + String(targetRotation) + " step to close."); Serial.println("向右旋转 " + String(targetRotation) + " 步以关盖."); #endif for (int i = 0; i < targetRotation; i++) { taskRotate(RIGHT); boxdisplacement++; } } else { #ifdef DEBUG Serial.println("rorate right " + String(-targetRotation) + " step to close."); Serial.println("向左旋转 " + String(-targetRotation) + " 步以关盖."); #endif for (int i = 0; i < -targetRotation; i++) { taskRotate(LEFT); boxdisplacement--; } } } void rotateToTake(int boxName) { int currentPosition = boxName + boxdisplacement; int targetRotation = TAKE_POSITION - currentPosition; if (targetRotation >= 0) { #ifdef DEBUG Serial.println("rorate right " + String(targetRotation) + " step to take pills."); Serial.println("向右旋转 " + String(targetRotation) + " 步以取药."); #endif for (int i = 0; i < targetRotation; i++) { taskRotate(RIGHT); boxdisplacement++; } } else { #ifdef DEBUG Serial.println("rorate right " + String(-targetRotation) + " step to take pills."); Serial.println("向左旋转 " + String(-targetRotation) + " 步以取药."); #endif for (int i = 0; i < -targetRotation; i++) { taskRotate(LEFT); boxdisplacement--; } } } void rotateToOut(int boxName) { int currentPosition = boxName + boxdisplacement; int targetRotation = OUT_POSITION - currentPosition; if (targetRotation >= 0) { #ifdef DEBUG Serial.println("rorate right " + String(targetRotation) + " step to add pills."); Serial.println("向右旋转 " + String(targetRotation) + " 步以换(加)药."); #endif for (int i = 0; i < targetRotation; i++) { taskRotate(RIGHT); boxdisplacement++; } } else { #ifdef DEBUG Serial.println("rorate right " + String(-targetRotation) + " step to add pills."); Serial.println("向左旋转 " + String(-targetRotation) + " 步以换(加)药."); #endif for (int i = 0; i < -targetRotation; i++) { taskRotate(LEFT); boxdisplacement--; } } } void takePills(int boxName, int pillsNumber) { digitalWrite(PIN_ENABLE, LOW); // taskPillsUp(NULL); rotateToOpen(boxName); taskOpenBox(NULL); rotateToTake(boxName); // stepper2.runToNewPosition(5000); midToLeft(); openPump(); // stepper2.runToNewPosition(6400); delay(1000); digitalWrite(PIN_STEPPER2_DIR, LOW); for (int j = 0; j <= 5; j++) { for (int i = 0; i < 500; i++) { digitalWrite(PIN_STEPPER2_STEP, HIGH); delayMicroseconds(400); digitalWrite(PIN_STEPPER2_STEP, LOW); delayMicroseconds(400); } delay(500); digitalWrite(PIN_STEPPER2_DIR, HIGH); for (int k = 0; k < 500; k++) { digitalWrite(PIN_STEPPER2_STEP, HIGH); delayMicroseconds(400); digitalWrite(PIN_STEPPER2_STEP, LOW); delayMicroseconds(400); } digitalWrite(PIN_STEPPER2_DIR, LOW); } leftToMid(); taskMidToRight(NULL); closePump(); openAir(); delay(3000); closeAir(); delay(2000); taskPillsOut(NULL); taskRightToMid(NULL); // xTaskCreate(taskRightToMid, "taskRightToMid", 1000, NULL, 1, NULL); isTaskTake = false; digitalWrite(PIN_ENABLE, HIGH); } void taskTakePills(void *pvParameters) { int boxName = ((int *)pvParameters)[0]; int pillsNumber = ((int *)pvParameters)[1]; #ifdef DEBUG Serial.println("boxName: " + String(boxName)); Serial.println("pillsNumber: " + String(pillsNumber)); #endif takePills(boxName, pillsNumber); vTaskDelete(NULL); } void takePillsTest() { midToLeft(); openPump(); // stepper2.runToNewPosition(6400); delay(1000); digitalWrite(PIN_STEPPER2_DIR, LOW); for (int j = 0; j <= 5; j++) { for (int i = 0; i < 500; i++) { digitalWrite(PIN_STEPPER2_STEP, HIGH); delayMicroseconds(400); digitalWrite(PIN_STEPPER2_STEP, LOW); delayMicroseconds(400); } delay(500); digitalWrite(PIN_STEPPER2_DIR, HIGH); for (int k = 0; k < 500; k++) { digitalWrite(PIN_STEPPER2_STEP, HIGH); delayMicroseconds(400); digitalWrite(PIN_STEPPER2_STEP, LOW); delayMicroseconds(400); } digitalWrite(PIN_STEPPER2_DIR, LOW); } leftToMid(); taskMidToRight(NULL); closePump(); openAir(); delay(3000); closeAir(); taskRightToMid(NULL); }
承接stm32单片机定做设计。嵌入式类设计定做。联系扣扣见代码顶部
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。