Progress
[monami] auto aero system
monami
D-CaFE
Issue
2021-05-28
.
.
.
#include
//PROGMEM 메모리 필요 라이브러리
#include //아두이노 보드의 내장된 EEPROM 메모리사용 필요 라이브러리
int getMedianNum(int bArray[], int iFilterLen);
boolean serialDataAvailable(void);
byte uartParse();
void doCalibration(byte mode);
#define DoSensorPin A1 //DO센서 핀을 A1에 연결
#define VREF 5000 //ADC(아두이노에서 아날로그 값을 디지털값으로 변환)참조 5V
float doValue; //읽어내려는 현재 용존 산소값 변수 정의
float temperature = 25; //현재의 온도를 25도 라고 설정
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof xss=removed xss=removed pp[i]=EEPROM.read(address+i);} xss=removed xss=removed xss=removed xss=removed xss=removed> 30U) //30밀리초마다 아날로그 값 읽기
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(DoSensorPin); //아날로그 값을 읽어 버퍼에 저장
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long tempSampleTimepoint = millis();
if(millis()-tempSampleTimepoint > 500U) //500밀리초마다 온도 값 읽기
{
tempSampleTimepoint = millis();
float temperature(); //수온센서코딩
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) { //CRC 오류검사(네트워크를 통하여 데이터를 전송할때 전송된 데이터에 오류가 있는지를 확인하기 위한 체크값을 결정하는 방식)
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1);
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for (int i = 0; i < 9 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> 1000U) // 출력되는데 1000밀리초 걸린다
{
printTimepoint = millis(); // 출력되는 시간 1000밀리초
for(copyIndex=0;copyIndex 0) // 유효한 데이터 값이 0이상일 때
{
byte modeIndex = uartParse(); // 받은 명령을 분석한다
doCalibration(modeIndex); // do보정함수 호출한다
}
}
boolean serialDataAvailable(void) // 데이터통신 값을 구한다
{
char receivedChar;
static unsigned long receivedTimeOut = millis();
while ( Serial.available() > 0 )
{
if (millis() - receivedTimeOut > 500U)
{
receivedBufferIndex = 0;
memset(receivedBuffer,0,(ReceivedBufferLength+1));
}
receivedTimeOut = millis();
receivedChar = Serial.read();
if (receivedChar == '\n' || receivedBufferIndex == ReceivedBufferLength)
{
receivedBufferIndex = 0;
strupr(receivedBuffer);
return true;
}else{
receivedBuffer[receivedBufferIndex] = receivedChar;
receivedBufferIndex++;
}
}
return false;
}
byte uartParse() // 통신분석하기
{
byte modeIndex = 0;
if(strstr(receivedBuffer, "CALIBRATION") != NULL) // CALIBRATION입력시 보정모드 들어가기
modeIndex = 1; // 순서 1번
else if(strstr(receivedBuffer, "EXIT") != NULL) // EXIT입력시 보정모드 종료
modeIndex = 3; // 순서 3번
else if(strstr(receivedBuffer, "SATCAL") != NULL) // SATCAL입력시 보정성공여부 확인
modeIndex = 2; // 순서 2번
return modeIndex;
}
void doCalibration(byte mode) // do보정 모드
{
char *receivedBufferPtr;
static boolean doCalibrationFinishFlag = 0,enterCalibrationFlag = 0;
float voltageValueStore;
switch(mode)
{
case 0:
if(enterCalibrationFlag)
Serial.println(F("Command Error")); // 입력 시 최종 보정 완료
break;
case 1:
enterCalibrationFlag = 1;
doCalibrationFinishFlag = 0;
Serial.println();
Serial.println(F(">>>Enter Calibration Mode<<<"));
Serial.println(F(">>>Please put the probe into the saturation oxygen water! <<<"));
Serial.println();
break;
case 2:
if(enterCalibrationFlag)
{
Serial.println();
Serial.println(F(">>>Saturation Calibration Finish!<<<"));
Serial.println();
EEPROM_write(SaturationDoVoltageAddress, averageVoltage);
EEPROM_write(SaturationDoTemperatureAddress, temperature);
SaturationDoVoltage = averageVoltage;
SaturationDoTemperature = temperature;
doCalibrationFinishFlag = 1;
}
break;
case 3:
if(enterCalibrationFlag)
{
Serial.println();
if(doCalibrationFinishFlag)
Serial.print(F(">>>Calibration Successful"));
else
Serial.print(F(">>>Calibration Failed"));
Serial.println(F(",Exit Calibration Mode<<<"));
Serial.println();
doCalibrationFinishFlag = 0;
enterCalibrationFlag = 0;
}
break;
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}
void readDoCharacteristicValues(void) //EEPROM에 저장된 do값을 읽음
{
EEPROM_read(SaturationDoVoltageAddress, SaturationDoVoltage); //EEPROM에 저장된 포화산소전압주소, 포화do전압 읽어냄
EEPROM_read(SaturationDoTemperatureAddress, SaturationDoTemperature); //EEPROM에 저장된 포화do온도 주소, 포화do온도 읽어냄
if(EEPROM.read(SaturationDoVoltageAddress)==0xFF && EEPROM.read(SaturationDoVoltageAddress+1)==0xFF && EEPROM.read(SaturationDoVoltageAddress+2)==0xFF && EEPROM.read(SaturationDoVoltageAddress+3)==0xFF)
{
SaturationDoVoltage = 1127.6; //default voltage:1127.6mv
EEPROM_write(SaturationDoVoltageAddress, SaturationDoVoltage);
}
if(EEPROM.read(SaturationDoTemperatureAddress)==0xFF && EEPROM.read(SaturationDoTemperatureAddress+1)==0xFF && EEPROM.read(SaturationDoTemperatureAddress+2)==0xFF && EEPROM.read(SaturationDoTemperatureAddress+3)==0xFF)
{
SaturationDoTemperature = 25.0; //default temperature is 25^C
EEPROM_write(SaturationDoTemperatureAddress, SaturationDoTemperature);
}
}
//여기까지 용존산소 계측, 값 무선통신 코드
//dc모터 제어 코드
int motor_1 = 10;
int motor_2 = 9;
void setup() {
// put your setup code here, to run once:
pinMode(motor_1, OUTPUT);
pinMode(motor_2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
analogWrite(motor_1, 255/3);
digitalWrite(motor_2, 0);
delay(2000);
analogWrite(motor_1, 255*2/3);
digitalWrite(motor_2, 0);
delay(2000);
analogWrite(motor_1, 255);
digitalWrite(motor_2, 0);
delay(1000);
}
//유량계측 코드
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
flow_frequency++;
}
void setup()
{
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
}
}
.