arduino.ino 11.4 KB
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <SoftwareSerial.h>

#define BT_RXD A5
#define BT_TXD A4
SoftwareSerial bluetooth(BT_RXD, BT_TXD);
int bufferposition;

int A = 2;
int B = 3;
int C = 4;
int D = 5;
int Data_Red = 6;
int Data_Green = 7;
int Clk = 8;
int LE = 9;
int OE = 10;


//#define LED_1

//#define LED_2
#define LED_3
//*/
const int __attribute__((progmem)) string[][16] = {
  0x1f, 0xff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x1fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x1fff, 0x1fff, 0xfff, 0x7ff, 0x3ff,
  0xc000, 0xf000, 0xfc00, 0xff00, 0xff80, 0xffc0, 0xffe0, 0xffe0, 0xfff0, 0xfff0, 0xfff8, 0xfff8, 0xfffc, 0xfffc, 0xfffc, 0xfffe,
  0x3, 0xf, 0x3f, 0xff, 0x1ff, 0x3ff, 0x7ff, 0x7ff, 0xfff, 0xfff, 0x1fff, 0x1fff, 0x3fff, 0x3fff, 0x3fff, 0x7fff,
  0xf800, 0xff00, 0xffc0, 0xffe0, 0xfff0, 0xfff8, 0xfff8, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfff8, 0xfff8, 0xfff0, 0xffe0, 0xffc0,
  
  0x8, 0xc, 0xc, 0xe, 0xe, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x7f, 0x7ff, 0x3fff, 0xffff,
  0x0, 0x0, 0xc, 0x1c, 0x38, 0x78, 0xf8, 0x81f0, 0x83f0, 0xc7f0, 0xefe0, 0xffe0, 0xffe0, 0xffe0, 0xffc0, 0xffc0,
  0x0, 0x0, 0x3000, 0x3800, 0x1c00, 0x1e00, 0x1f00, 0xf81, 0xfc1, 0xfe3, 0x7f7, 0x7ff, 0x7ff, 0x7ff, 0x3ff, 0x3ff,
  0x1000, 0x3000, 0x3000, 0x7000, 0x7000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xf000, 0xfe00, 0xffe0, 0xfffc, 0xffff,
  
  0x0, 0x3, 0x3f, 0xff, 0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x1fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x7fff, 0x7fff,
  0x0, 0xc000, 0xfc00, 0xff00, 0xff80, 0xffc0, 0xffe0, 0xfff0, 0xfff8, 0xfff8, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffe, 0xfffe,
  0x0, 0x3, 0x3f, 0xff, 0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x1fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x7fff, 0x7fff,
  0x0, 0xc000, 0xfc00, 0xff00, 0xff80, 0xffc0, 0xffe0, 0xfff0, 0xfff8, 0xfff8, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffe, 0xfffe,
  
  0x0, 0x0, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff,
  0x0, 0x0, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc,
  0x0, 0x0, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff,
  0x0, 0x0, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc, 0xfffc,
  
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
};

unsigned char Dot_char_cnt = 12; //도트매트릭스에 표시 될 문자 카운팅!
unsigned char flag_cnt = 0; //도트매트릭스에 표시 될 색상 카운팅!
unsigned char move_motion = 0;
unsigned int Move_cnt = 16;
unsigned int Move_cnt2 = 0;
unsigned int str_speed_cnt = 0;
bool flag_Oe = 0;

ISR(TIMER2_OVF_vect) {
  static bool output = HIGH;
  digitalWrite(1, output);
  output = !output;

  static unsigned int string_moving_speed_value = 500;
  static unsigned int cnt = 0;

  if (flag_Oe)
  {
    digitalWrite(OE, LOW);
    cnt++;
    if (cnt >= 5)
    {
      flag_Oe = 0;
      cnt = 0;
      digitalWrite(OE, HIGH);
    }
  }
  str_speed_cnt++;
  if (str_speed_cnt > string_moving_speed_value) //문자가 머문 후 시프트 시작!
  {
    str_speed_cnt = 0;
    Move_cnt--;
    Move_cnt2++;
    if (Move_cnt == 0 || Move_cnt2 == 16) {
      Move_cnt = 16;
      Move_cnt2 = 0;

      move_motion++;
      if (move_motion > 0) {
        move_motion = 0;
        Dot_char_cnt += 0;
        
        if (flag_cnt == 3)
        {
          flag_cnt = 0;
        }
      }
    }
  }
  TCNT2 = 0xe7;
}

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(Data_Red, OUTPUT);
  pinMode(Data_Green, OUTPUT);
  pinMode(Clk, OUTPUT);
  pinMode(LE, OUTPUT);
  pinMode(OE, OUTPUT);
  pinMode(1, OUTPUT);
  
  Serial.begin(9600);
  bluetooth.begin(9600);
  
  bufferposition = 0;
  TCCR2A = 0x00;
  TCCR2B = 0x04;
  TIMSK2 = 0x01;
  TCNT2 = 0xe7;

  SREG = 0x80;
}

void row_dynamic()
{
  static unsigned int str_cnt = 0;

  switch (str_cnt) //ROW SHIFT!
  {
    case 0: digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(C, LOW); digitalWrite(D, LOW); break;        //1행 LED
    case 1: digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, LOW); digitalWrite(D, LOW); break;       //2행 LED
    case 2: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, LOW); digitalWrite(D, LOW); break;       //3행 LED
    case 3: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, LOW); digitalWrite(D, LOW); break;      //4행 LED
    case 4: digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(C, HIGH); digitalWrite(D, LOW); break;       //5행 LED
    case 5: digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, HIGH); digitalWrite(D, LOW); break;      //6행 LED
    case 6: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, LOW); break;      //7행 LED
    case 7: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, LOW); break;     //8행 LED
    case 8: digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(C, LOW); digitalWrite(D, HIGH); break;       //9행 LED
    case 9: digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, LOW); digitalWrite(D, HIGH); break;      //10행 LED
    case 10: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, LOW); digitalWrite(D, HIGH); break;     //11행 LED
    case 11: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, LOW); digitalWrite(D, HIGH); break;    //12행 LED
    case 12: digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(C, HIGH); digitalWrite(D, HIGH); break;     //13행 LED
    case 13: digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, HIGH); digitalWrite(D, HIGH); break;    //14행 LED
    case 14: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); break;    //15행 LED
    case 15: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); break;   //16행 LED
  }
  str_cnt++;
  if (str_cnt == 16)
  {
    str_cnt = 0;
  }
}

void shift_Register(unsigned char out)
{
  unsigned char clk = 0;

  for (clk = 0; clk < 8; clk++) { //8비트 데이터를 1비트씩 시프트 레지스터에 입력
    if (out & (0x80 >> clk))
    {
      switch (flag_cnt) {
        case 0: digitalWrite(Data_Green, LOW); digitalWrite(Data_Red, HIGH); break; //Red
        case 1: digitalWrite(Data_Green, HIGH); digitalWrite(Data_Red, LOW); break; //Green
        case 2: digitalWrite(Data_Green, HIGH); digitalWrite(Data_Red, HIGH); break; //Orange
      }
    }
    else
    {
      digitalWrite(Data_Green, LOW); digitalWrite(Data_Red, LOW);
    }
    digitalWrite(Clk, HIGH);
    digitalWrite(Clk, LOW);
  }
}

void ActivePulse()
{

  digitalWrite(LE, HIGH);
  digitalWrite(LE, LOW); //래치 출력
  digitalWrite(OE, LOW);
  flag_Oe = 1;
}

void dot1_display_shift(unsigned char first, unsigned char second, unsigned char third, unsigned char fourth)
{
  static unsigned int i_cnt = 0;
  unsigned int buff1[16] = {0};     //Dot1
  unsigned int buff2[16] = {0};     //Dot2
  unsigned int buff3[16] = {0};
  unsigned int buff4[16] = {0};
  unsigned char high1 = 0;          //Dot1
  unsigned char low1 = 0;           //Dot1
  unsigned char high3 = 0;
  unsigned char low3 = 0;
  unsigned char high2 = 0;          //Dot2
  unsigned char low2 = 0;           //Dot2
  unsigned char high4 = 0;
  unsigned char low4 = 0;
  
  register unsigned int i = 0;

  for (i_cnt = 0; i_cnt < 16; i_cnt++) { //우에서 좌로 시프트
    if (move_motion == 0)
    {
      buff1[i_cnt] = pgm_read_word(&string[first][i_cnt]); // 1st String Dot1
      buff2[i_cnt] = pgm_read_word(&string[second][i_cnt]); // 2st String Dot2
      buff3[i_cnt] = pgm_read_word(&string[third][i_cnt]); // 2st String Dot2
      buff4[i_cnt] = pgm_read_word(&string[fourth][i_cnt]); // 2st String Dot2
      
    }
  }

  for (i = 0; i < 16; i++)
  {
    high1 = (buff1[i] >> 8);
    low1 = (buff1[i] & 0xff);
    high2 = (buff2[i] >> 8);
    low2 = (buff2[i] & 0xff);
    high3 = (buff3[i] >> 8);
    low3 = (buff3[i] & 0xff);
    high4 = (buff4[i] >> 8);
    low4 = (buff4[i] & 0xff);

    shift_Register(high1);
    shift_Register(low1);
    shift_Register(high2);
    shift_Register(low2);
    shift_Register(high3);
    shift_Register(low3);
    shift_Register(high4);
    shift_Register(low4);

    row_dynamic();
    ActivePulse();
  }
}

char* buffer = new char[100];
int count = 0;

int byteArrayToInt(byte bytes[]) {
   return ((((int)bytes[0] & 0xff) << 24) |
            (((int)bytes[1] & 0xff) << 16) |
             (((int)bytes[2] & 0xff) << 8) |
              (((int)bytes[3] & 0xff))); 
}

String str_data;
char rem[10] = "Ready";
void loop() {
  count++;
    char data;

  if(count == 50) {
     count = 0; 
     Dot_char_cnt = 16;
  }
  dot1_display_shift(Dot_char_cnt, Dot_char_cnt+1, Dot_char_cnt+2, Dot_char_cnt+3);
  if (bluetooth.available()) {
    //Serial.write(bluetooth.read());
    delay(10);
    data = bluetooth.read();
    if(data != '\n') {
      str_data += data;
    }
    else if(data == '\n') {
    if(strstr(str_data.c_str(),rem)) {
        str_data = "";
      }
      #if defined(LED_1)
      if(strcmp(str_data.c_str(),"11")==0) {
       Dot_char_cnt = 0; 
       count = 0;
       flag_cnt++;
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "12")==0) {
       Dot_char_cnt = 4;
       count = 0;
       flag_cnt++; 
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "13")==0) {
        Dot_char_cnt = 8; 
        count = 0;
        flag_cnt++;  
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "14")==0) {
        Dot_char_cnt = 12; 
        count = 0;
        flag_cnt++;  
        str_data = "";
      }
      else {
        str_data = "";
      }
      #endif
      
      #if defined(LED_2)
      if(strcmp(str_data.c_str(),"21")==0) {
       Dot_char_cnt = 0; 
       count = 0;
       flag_cnt++;
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "22")==0) {
       Dot_char_cnt = 4;
       count = 0;
       flag_cnt++; 
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "23")==0) {
        Dot_char_cnt = 8; 
        count = 0;
        flag_cnt++;  
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "24")==0) {
        Dot_char_cnt = 12; 
        count = 0;
        flag_cnt++;  
        str_data = "";
      }
      else {
        str_data = "";
      }
      #endif
      
      #if defined(LED_3)
      if(strcmp(str_data.c_str(),"31")==0) {
       Dot_char_cnt = 0; 
       count = 0;
       flag_cnt++;
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "32")==0) {
       Dot_char_cnt = 4;
       count = 0;
       flag_cnt++; 
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "33")==0) {
        Dot_char_cnt = 8; 
        count = 0;
        flag_cnt++;  
        str_data = "";
      }
      else if(strcmp(str_data.c_str(), "34")==0) {
        Dot_char_cnt = 12; 
        count = 0;
        flag_cnt++;  
        str_data = "";
      }
      else {
        str_data = "";
      }
      #endif
    }
  }
  
  if (Serial.available()) {
    bluetooth.write(Serial.read());
    
  }
}