티스토리 뷰
#include <Wire.h>
#define ADC_ADDRESS 0x68
#define ADC_CONFIG 0XFC
#define PIN_MUXINA 4
#define PIN_MUXINB 5
#define PIN_MUXINC 6
#define PIN_MUX0 7
#define PIN_MUX1 8
#define PIN_MUX2 9
#define PIN_RELAY 12
int CommandStatus = 0;
int MuxNum = -1;
int PortNum = -1;
long ADCValue = -1;
void setup() {
Wire.begin();
Wire.beginTransmission(ADC_ADDRESS);
Wire.write(ADC_CONFIG);
Wire.endTransmission();
pinMode(PIN_MUXINA, OUTPUT);
pinMode(PIN_MUXINB, OUTPUT);
pinMode(PIN_MUXINC, OUTPUT);
pinMode(PIN_RELAY, OUTPUT);
pinMode(PIN_MUX0, OUTPUT);
pinMode(PIN_MUX1, OUTPUT);
pinMode(PIN_MUX2, OUTPUT);
digitalWrite(PIN_MUX0, HIGH);
digitalWrite(PIN_MUX1, HIGH);
digitalWrite(PIN_MUX2, HIGH);
Serial.begin(9600);
// analogReference(DEFAULT);
}
void loop() {
if(Serial.available() > 0){
char SerialIn = Serial.read();
switch(CommandStatus){
case 0:
PortNum = -1;
MuxNum = -1;
ADCValue = -1;
h_ClearMuxes();
case 80:
case 51:
if(SerialIn == '<') CommandStatus = 10;
// Serial.println(SerialIn+60);
break;
case 10:
if(SerialIn == 'U') CommandStatus = 20;
else CommandStatus = 0;
break;
case 20:
switch(SerialIn){
case 'A':
MuxNum = 1;
CommandStatus = 30;
break;
case 'B':
MuxNum = 2;
CommandStatus = 30;
break;
case 'C':
MuxNum = 3;
CommandStatus = 30;
break;
case 'I':
MuxNum = -1;
CommandStatus = 30;
break;
default:
CommandStatus = 0;
break;
}
break;
case 30:
PortNum = SerialIn - 0x30;
if((PortNum < 0) || (PortNum > 9)){
CommandStatus = 0;
break;
}
CommandStatus = 40;
break;
case 40:
if(SerialIn == '/') CommandStatus = 50;
else CommandStatus = 0;
break;
case 50:
if(SerialIn == '>'){
if(MuxNum == -1) CommandStatus = 80;
else CommandStatus = 51;
} else CommandStatus = 0;
break;
default:
CommandStatus = 0;
break;
}
}
switch(CommandStatus){
case 51:
h_SetMux(PortNum);
switch(MuxNum){
case 1:
digitalWrite(PIN_MUX0, LOW);
break;
case 2:
digitalWrite(PIN_MUX1, LOW);
break;
case 3:
digitalWrite(PIN_MUX2, LOW);
break;
}
delay(500);
ADCValue = h_ReadADCValue();
h_RespondResult();
CommandStatus = 0;
break;
case 80:
if(PortNum == 0) digitalWrite(PIN_RELAY, LOW);
if(PortNum == 1) digitalWrite(PIN_RELAY, HIGH);
break;
default:
break;
}
}
void h_RespondResult(){
Serial.print("<V");
switch(MuxNum){
case 1:
Serial.print("V"); // Voltage
break;
case 2:
Serial.print("I"); // Current
break;
case 3:
Serial.print("A"); // AUX
break;
default:
CommandStatus = 0;
break;
}
Serial.print(PortNum);
Serial.print(ADCValue);
Serial.println("/>");
}
long h_ReadADCValue(){
Wire.requestFrom((int)ADC_ADDRESS, (int)4);
byte Byte1 = Wire.read();
byte Byte2 = Wire.read();
byte Byte3 = Wire.read();
byte Byte4 = Wire.read();
/*
Serial.print(" : ");
Serial.print(Byte1);
Serial.print(", ");
Serial.print(Byte2);
Serial.print(", ");
Serial.print(Byte3);
Serial.print(", ");
Serial.print(Byte4);
*/
long Rst = ((Byte1*256L)+Byte2)*256L+Byte3;
return Rst;
}
void h_ClearMuxes(){
digitalWrite(PIN_MUX0, HIGH);
digitalWrite(PIN_MUX1, HIGH);
digitalWrite(PIN_MUX2, HIGH);
}
void h_SetMux(int PortNum){
byte MuxInA = (byte) PortNum & 0x01;
byte MuxInB = (byte) PortNum & 0x02;
byte MuxInC = (byte) PortNum & 0x04;
if(MuxInA == 0) digitalWrite(PIN_MUXINA, LOW);
else digitalWrite(PIN_MUXINA, HIGH);
if(MuxInB == 0) digitalWrite(PIN_MUXINB, LOW);
else digitalWrite(PIN_MUXINB, HIGH);
if(MuxInC == 0) digitalWrite(PIN_MUXINC, LOW);
else digitalWrite(PIN_MUXINC, HIGH);
}
반응형
'HWDesk > HardwareControl' 카테고리의 다른 글
[Arduino활용22] 무선통신 이용하기 (0) | 2020.02.09 |
---|---|
[Arduino활용18] 원격으로 외부 장치 제어하기(적외선 리모컨 등) (0) | 2020.02.09 |
ADXL345 가속도 센서를 이용한 기울어짐 상태 파악 (0) | 2020.02.06 |
Dual Type Common Anode 7-segment 동작 테스트 (0) | 2020.02.03 |
기울어짐 알람(2) (0) | 2020.02.01 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 전압
- Innovations
- badp
- 심심풀이치매방지기
- image
- bilient
- Hurdles
- 치매
- 빌리언트
- 허들
- 티스토리챌린지
- Innovation&Hurdles
- 전압전류모니터링
- Innovations&Hurdles
- BSC
- 오블완
- 심심풀이
- 치매방지
- ServantClock
- 혁신
- 혁신과허들
- arduino
- Decorator
- 절연형
- 배프
- Video
- 아두이노
- 둎
- 전류
- DYOV
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
글 보관함