(smart toilet) 논문

arduino_2016. 1. 13. 22:27

https://www.youtube.com/watch?v=ugbl0RBU5bo

 

일부 공개

#include <Wire.h>

const int fnd_a1 = 2;
const int fnd_a2 = 14;
int k=0;
int count=1;
int i=1;
int result = 0;
int n = 0;
const int trig_pin = 22;
const int echo_pin = 23;
const int led_pin = 29;
unsigned char table1[10][8] =
{
//h,g,f,e,d,c,b,a
  {1,0,1,1,1,1,1,1},//0
  {1,0,0,0,0,1,1,0},//1
  {1,1,0,1,1,0,1,1},//2
  {1,1,0,0,1,1,1,1},//3
  {1,1,1,0,0,1,1,0},//4
  {1,1,1,0,1,1,0,1},//5
  {1,1,1,1,1,1,0,1},//6
  {1,0,0,0,0,1,1,1},//7
  {1,1,1,1,1,1,1,1},//8
  {1,1,1,0,1,1,1,1}//9
};
unsigned char table2[10][8] =
{
//h,g,f,e,d,c,b,a
  {0,0,1,1,1,1,1,1},//0
  {0,0,0,0,0,1,1,0},//1
  {0,1,0,1,1,0,1,1},//2
  {0,1,0,0,1,1,1,1},//3
  {0,1,1,0,0,1,1,0},//4
  {0,1,1,0,1,1,0,1},//5
  {0,1,1,1,1,1,0,1},//6
  {0,0,0,0,0,1,1,1},//7
  {0,1,1,1,1,1,1,1},//8
  {0,1,1,0,1,1,1,1}//9
};
void setup() {
  // put your setup code here, to run once:
  //seven segment1
  Wire.begin(); //i2c 버스에 참여
  Serial.begin(9600); // 직렬 통신 초기화
  for(int i=fnd_a1; i<fnd_a1+8; i++)
  {
    pinMode(i, OUTPUT);
  }
  //seven segment2
  for(int j=fnd_a2; j<fnd_a2+8; j++)
  {
    pinMode(j, OUTPUT);
  }
  //0
  for(int j=0; j<8; j++){//a~g
    digitalWrite(fnd_a1 + j, table1[0][7-j]);
  }
  //0
  for(int j=0; j<8; j++){//a~g
    digitalWrite(fnd_a2 + j, table2[0][7-j]);
  }
  pinMode(trig_pin, OUTPUT);
  pinMode(echo_pin, INPUT);
  pinMode(led_pin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trig_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(trig_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_pin, LOW);
  long distance = pulseIn(echo_pin, HIGH)/58.2;
  Serial.println(distance);
  if(distance <60)
  { 
    n = 1; 
    for(int j=0; j<8; j++)
    {
      if(i==10)
      {
        for(int s=0; s<8; s++)
        {
          digitalWrite(fnd_a2 + s, table2[k][7-s]);
        }
        i = 0;
     }
     else
     {
       digitalWrite(fnd_a1 + j, table1[i][7-j]);
       if(i==1 && k==0)
       {
         digitalWrite(led_pin, HIGH);
       }
     }
    }
    delay(300);
    digitalWrite(led_pin, LOW);
    count++;
    i++;
    //Serial.println("count");
    if(count == 10)
    {     
      k++;
      count = 0;
      if(k>9)
      {
        k = 0;
      }
    }
  }
  else//
  {
    Wire.requestFrom(5,1);//slave 5에게 1byte 요구
    while(Wire.available())
    {
      long c = Wire.read();
      Serial.println(c);
      delay(300);
      if(c != 0 && n ==0)
      {
        if( i == 0)
        {
          for(int j=0; j<8; j++)
          {           
              digitalWrite(fnd_a1 + j, table1[9][7-j]);
          }
          if(k!=0)
          {           
            k -= 1;
            for(int j=0; j<8; j++)
            {
              digitalWrite(fnd_a2 + j, table2[k][7-j]);
            }
         }
         i= 9;
         count = 9;
        }
        else
        {
          i -=1;
          count -=1;
         
           for(int j=0; j<8; j++)
           {           
            digitalWrite(fnd_a1 + j, table1[i][7-j]);
           }
       }
      
       
      }
    }
  }
  n = 0;