#include <Arduino.h>
#include <EEPROM.h>
#define LOCK_PIN 2
#define PWM_PIN 3
#define DIR_PIN 4
#define Microstep 6000
#define STORGE_CMD 0x7E
#define PUIOUT_CMD 0x7D
volatile int BottleLeftNum;
int state;
int medicNum;
int buff[3];
volatile int step = 0;
volatile int backStepNum = 0;
int putMedicOutNum = 0;
int bottle_num = 0;
int out_bottle_num = 0;
// int cmd_id = 0;
//
void writeDataToEeprom(int BottleLeftNum,int step,int backStepNum,int putMedicOutNum)
{
// int i = 0;
// EEPROM.begin();
// for(i; i < 50; i++){
// EEPROM.write(i,0);
// }
EEPROM.put(0, BottleLeftNum);
// for(i; i < 100; i++){
// EEPROM.write(i,0);
// }
EEPROM.put(50, step);
// for(i; i < 150; i++){
// EEPROM.write(i,0);
// }
EEPROM.put(100, backStepNum);
// for(i; i < 200; i++){
// EEPROM.write(i,0);
// }
EEPROM.put(150, putMedicOutNum);
EEPROM.end();
}
void initPin()
{
pinMode(PWM_PIN,OUTPUT);
pinMode(DIR_PIN,OUTPUT);
pinMode(LOCK_PIN,OUTPUT);
}
void openLock()
{
digitalWrite(LOCK_PIN,HIGH);
}
void closeLock()
{
digitalWrite(LOCK_PIN,LOW);
}
void runOneStep()//正向转
{
digitalWrite(DIR_PIN,HIGH);//正向
for(int i = 0; i < Microstep; i++){//转一格
digitalWrite(PWM_PIN,HIGH);
delayMicroseconds(50);
digitalWrite(PWM_PIN,LOW);
delayMicroseconds(50);
}
}
void putOutMedicBottle(int num)
{
for(int i = 0; i < num; i++){
runOneStep();
delay(2000);
}
BottleLeftNum = BottleLeftNum - num;//重新计算剩余存储药瓶数量
// for(int j; j < 50; j++){
// EEPROM.write(j,0);
// }
EEPROM.put(0, BottleLeftNum);
memset(buff,'\0',3);
}
void Positive_transfer(int num)
{
if(num > 0){
num = 20 - num;
digitalWrite(DIR_PIN,HIGH);//正向
for(int j = 0; j < num; j++){
for(int i = 0; i < Microstep; i++){//转一格
digitalWrite(PWM_PIN,HIGH);
delayMicroseconds(50);
digitalWrite(PWM_PIN,LOW);
delayMicroseconds(50);
}
}
}
}
void Inversion_transfer(int num)//反向转
{
num = 20 - num;
digitalWrite(DIR_PIN,LOW);//反向
for(int j = 0; j < num; j++){
for(int i = 0; i < Microstep; i++){//转一格
digitalWrite(PWM_PIN,HIGH);
delayMicroseconds(50);
digitalWrite(PWM_PIN,LOW);
delayMicroseconds(50);
}
}
}
void stopMotor()//电机停转
{
digitalWrite(PWM_PIN,LOW);
digitalWrite(PWM_PIN,LOW);
}
void Single_bottle_storage(int BottleLeftNum)
{
Inversion_transfer(BottleLeftNum);
openLock();
delay(4000);
closeLock();
Positive_transfer(BottleLeftNum);
stopMotor();
memset(buff,'\0',3);
}
void Multi_bottle_storage(int step,int medicNum,int backStepNum)
{
Inversion_transfer(step);
for(int j = 0; j < medicNum; j++){//放入药品
openLock();
delay(4000);
closeLock();
runOneStep();
}
Positive_transfer(backStepNum);
stopMotor();//电机停转
memset(buff,'\0',3); //清空数据
}
void medicMessage()
{
Serial.print("剩余药品数量:");
Serial.println(BottleLeftNum);
}
void warningMessage()
{
Serial.println("存药空间已满,仅支持退药!");
Serial.print("目前剩余药品数量:");
Serial.println(BottleLeftNum);
}
uint8_t get_serial_cmd_data(int* get_cmd){
int tmp_cmd = 0;
if(Serial.available() > 0){
tmp_cmd = Serial.parseInt();
if(tmp_cmd == STORGE_CMD) {
bottle_num = Serial.parseInt();
Serial.print("STORGE_CMD ==");
Serial.println(bottle_num);
*get_cmd = tmp_cmd;
return 1;
}else if(tmp_cmd == PUIOUT_CMD){
out_bottle_num = Serial.parseInt();
Serial.print("PUIOUT_CMD ==");
Serial.println(out_bottle_num);
*get_cmd = tmp_cmd;
return 1;
}
}
return 0;
}
void setup() {
// put your setup code here, to run once:
initPin();
Serial.begin(9600);
closeLock();
// / medicMessage();
Serial.print("复位后:BottleLeftNum = ");
Serial.println(EEPROM.read(0));
BottleLeftNum += EEPROM.read(0);//如果复位后需要加上断电之前剩余药品数
// writeDataToEeprom();
// writeDataToEeprom();
}
void temp_work(){
medicNum = bottle_num;//每次需要放入药品数量
putMedicOutNum = out_bottle_num;//需要出药瓶的数量
if(BottleLeftNum <= 19)
{
switch(state){
case STORGE_CMD :
if(medicNum == 1){//一瓶一瓶的放
Single_bottle_storage(BottleLeftNum);
Serial.println("已经成功存入:1瓶");
medicMessage();
}else if(medicNum > 1){//多瓶放入
Multi_bottle_storage(step,medicNum,backStepNum);
Serial.print("已经成功存入:");
Serial.print(medicNum);
Serial.println("瓶");
medicMessage();
}
break;
case PUIOUT_CMD :
putOutMedicBottle(putMedicOutNum);
stopMotor();
Serial.print("已经成功退出:");
Serial.print(putMedicOutNum);
Serial.println("瓶");
medicMessage();
break;
}
}else{
warningMessage();
delay(2000);
if(state == 1){//出药
putOutMedicBottle(putMedicOutNum);
stopMotor();
medicMessage();
}
}
}
void loop() {
if(get_serial_cmd_data(&state) == 1){
step = BottleLeftNum + 1;//多瓶存退步所需参数
BottleLeftNum += bottle_num;//暂存药品总数
writeDataToEeprom(BottleLeftNum,step,backStepNum,putMedicOutNum);
backStepNum = BottleLeftNum + 1;//多瓶存入电机正转所需参数
Serial.println(BottleLeftNum);
Serial.println(step);
temp_work();
}
}
药箱控制-arduino
最新推荐文章于 2024-04-06 13:40:48 发布