Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define LED 13
- #define BAUDRATE 57600
- #define DEBUGOUTPUT 0
- // checksum variables
- byte generatedChecksum = 0;
- byte checksum = 0;
- int queue[20]={0};
- int payloadLength = 0;
- byte payloadData[64] = {0};
- byte poorQuality = 0;
- byte attention = 0;
- byte meditation = 0;
- int eyeblink = 0;
- int blinkStrength = 0;
- int blinkLength = 0;
- byte vlen;
- int rawData;
- int _min, _max;
- byte tt = 0;
- // system variables
- long lastReceivedPacket = 0;
- boolean bigPacket = false;
- //////////////////////////
- // Microprocessor Setup //
- //////////////////////////
- void setup()
- {
- pinMode(LED, OUTPUT);
- Serial.begin(BAUDRATE); // USB
- }
- ////////////////////////////////
- // Read data from Serial UART //
- ////////////////////////////////
- byte ReadOneByte()
- {
- int ByteRead;
- while(!Serial.available());
- ByteRead = Serial.read();
- return ByteRead;
- }
- /////////////
- //MAIN LOOP//
- /////////////
- int read_3byte_int(int i) {
- return ((payloadData[i] << 16) + (payloadData[i+1] << 8) + payloadData[i+2]);
- }
- void loop()
- {
- // Look for sync bytes
- if(ReadOneByte() == 170)
- {
- if(ReadOneByte() == 170)
- {
- payloadLength = ReadOneByte();
- if(payloadLength > 169) //Payload length can not be greater than 169
- return;
- generatedChecksum = 0;
- for(int i = 0; i < payloadLength; i++)
- {
- payloadData[i] = ReadOneByte(); //Read payload into memory
- generatedChecksum += payloadData[i];
- }
- checksum = ReadOneByte(); //Read checksum byte from stream
- generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
- if(checksum == generatedChecksum)
- {
- poorQuality = 200;
- bigPacket = false;
- attention = 0;
- meditation = 0;
- for(int i = 0; i < payloadLength; i++)
- { // Parse the payload
- switch (payloadData[i])
- {
- case 2:
- i++;
- poorQuality = payloadData[i];
- break;
- case 4:
- i++;
- attention = payloadData[i];
- break;
- case 5:
- i++;
- meditation = payloadData[i];
- Serial.print("호이");
- bigPacket = true;
- break;
- case 0x80:
- digitalWrite(LED, LOW);
- _min = 9999;
- _max = -9999;
- for(int j=8;j>=0;j--){
- if(_min >= queue[j]) _min = queue[j];
- if(_max <= queue[j]) _max = queue[j];
- queue[j+1] = queue[j];
- }
- i++;
- rawData = (payloadData[i+1] * 256);
- rawData += payloadData[i+2];
- queue[0] = rawData;
- i+=2;
- if(_max - _min > 230){
- // Serial.println("Blink!");
- blinkStrength ++;
- }
- // delay(100);
- break;
- case 0x83:
- i+=25;
- break;
- default:
- break;
- } // switch
- } // for loop
- if(bigPacket == true){
- if(50 < poorQuality && poorQuality < 200){
- Serial.println("기기가 연결되어있지 않음");
- return;
- }
- else if(10 < poorQuality){
- Serial.println("신호가 너무 약함.");
- return;
- }
- digitalWrite(LED, LOW);
- Serial.print("Attention : ");
- Serial.print(attention, DEC);
- Serial.print(" Mediation : ");
- Serial.print(meditation, DEC);
- Serial.print(" 눈 움직임 강도 : ");
- Serial.print(blinkStrength, DEC);
- Serial.print(" PoorQuality : ");
- Serial.print(poorQuality, DEC);
- if(blinkStrength > 10){
- Serial.print("깜빡!");
- digitalWrite(LED, HIGH);
- }
- if(0 < blinkLength && blinkLength < 3){
- Serial.print("졸림 1차 경보!");
- }
- else if(3 <= blinkLength && blinkLength < 5){
- Serial.print("졸림 2차 경보!");
- }
- else if(5 <= blinkLength && blinkLength <= 7){
- Serial.print("졸림 3차 경보!");
- }
- // 눈 움직임 강도가 10이하이고 Mediation < Attention 일떄
- if(attention < meditation && blinkStrength < 10){
- blinkLength ++;
- // blinkLength 를 증가시키고 조건에 만족안하면 초기화 시킨다.
- // 즉 blinkLength 는 '조건'이 몇초동안 유지되는가를 담는 변수임
- }
- else {
- blinkLength = 0;
- }
- Serial.print("\n");
- blinkStrength = 0;
- }
- }
- }
- } // end if read 0xAA byte
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement