是否有可能使用公共阳极配置以正方形的形式将5 * 5矩阵中的led发光 [英] Is there a possibility to glow the led in 5*5 matrix in a square shape with the common anode configuration

查看:96
本文介绍了是否有可能使用公共阳极配置以正方形的形式将5 * 5矩阵中的led发光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个led矩阵,它在行方向上是公共阳极,在列方向上是公共阴极.我需要以正方形的形状发光矩阵(第一行&&&&&第一列&&&最后一列),但我无法做到这一点.我能够分别显示第一行和最后一行以及第一列和第二列)

void first_last_row();
void first_last_column();

int Led_Row_Pins[] = { 2 , 3 , 4 , 5 , 6 } ;             //   Anode pins are shorted in row_wise_manner
int Led_Column_Pins[] = {8  , 9 , 10 , 11 , 12} ;          //   Column Pins are shorted in column_wise_manner   
int Loop_Count = 5 ;
int Wait_Time_On = 1000 ;
int Wait_Time_Off = 500 ;
int i = 0 ;
int j = 0 ;
int state = 1 ;

void setup() {
  for( i = 0 ; i < Loop_Count ; i++ ){     // Anode Pins are connected in row_wise manner and are made LOW so that they dont conduct       
    pinMode(Led_Row_Pins[i],OUTPUT);
    digitalWrite(Led_Row_Pins[i],LOW);
    pinMode(Led_Column_Pins[i],OUTPUT);    // Cathode Pins are connected in column_wise manner and are made HIGH so that they dont conduct
    digitalWrite(Led_Column_Pins[i],HIGH); 
  }
}

void loop() { 
      first_last_row();
      delay(1000);
      first_last_column();
}


void first_last_row()
{
  for( i = 0 ; i < Loop_Count ; i++ )// Led First And Last Row 
   {
     for( j = 0 ; j < Loop_Count ; j++)
     {
       if( i == 0 || i == 4 )
       {
         digitalWrite(Led_Row_Pins[i],state); //Led_On_State
         digitalWrite(Led_Column_Pins[j],!state);
       }
     }
   }
}


void first_last_column()
{

  for( j = 0 ; j < Loop_Count ; j++ )// Led First And Last Column
   {
     for( i = 0 ; i < Loop_Count ; i++)
     {
       if( j == 0 || j == 4 )
       {
        digitalWrite(Led_Row_Pins[i],state);//    Led_On_State
        digitalWrite(Led_Column_Pins[j],!state);
       }
     }
   }
}

我需要第一行和最后一行以及第一列和最后一列led发光在一起,以便形成正方形,但是我只能分别发光第一行和最后一行以及第一列和最后一个发光列.

解决方案

在您的特定情况下,您可以打开第一行和最后一行,例如1毫秒,然后打开第一列和最后一列1毫秒,然后重复此步骤.我进一步建议在打开新图案之前先关闭所有LED.

void loop() { 
      static int show_rows = 0;

      switch_all_off();
      if(show_rows) {
          first_last_row();
      } else {
          first_last_column();
      }
      show_rows = !show_rows;
      delay(10);
}

void switch_all_off()
{
    int off = 0;
    for( j = 0 ; j < Loop_Count ; j++ )
    {
          digitalWrite(Led_Row_Pins[i],off);
          digitalWrite(Led_Column_Pins[j],!off);
    }
}

第一步,行和列中包含的LED将更亮.要解决此问题,您可以从行或列中排除角LED,例如

void first_last_column()
{

    for( j = 0 ; j < Loop_Count ; j++ )// Led First And Last Column
    {
        for( i = 0 ; i < Loop_Count ; i++)
        {
            if((i != 0) && (i != 4) && ( j == 0 || j == 4 ))
            {
                digitalWrite(Led_Row_Pins[i],state);//    Led_On_State
                digitalWrite(Led_Column_Pins[j],!state);
            }
        }
    }
}

对于更通用的解决方案,您必须遍历各行,依次激活另一行,并根据需要切换当前行的列引脚.

#define MAXLEDS 5
int states[MAXLEDS][MAXLEDS] = {
    { 1, 0, 0, 0, 1 },
    { 0, 1, 0, 1, 0 },
    { 0, 0, 1, 0, 0 },
    { 0, 1, 0, 1, 0 },
    { 1, 0, 0, 0, 1 }
};

void switch_leds(int row) {
    int i;

    /* switch off all rows */
    for(i = 0; i < MAXLEDS, i++) {
        digitalWrite(Led_Row_Pins[i], 0);
    }

    /* switch columns according to current row */
    for(i = 0; i < MAXLEDS; i++) {
        digitalWrite(Led_Column_Pins[i], !states[row][i]);
    }

    /* switch on current row */
    digitalWrite(Led_Row_Pins[row], 1);

}

void loop() { 
    static int row = 0;
    /* switch on LEDs in a single row */
    switch_leds(row);
    /* next row */
    row++; row %= MAXLEDS;
    /* The processing delay between calls to loop() is added to this delay. */
    delay(5);
}

所有代码示例未经测试,都可能包含错误.

I have a led-matrix that is common anode in row-wise and common cathode in column-wise. I need to glow the matrix in the shape of a square(first row && last row && first column && last column) , but I was not able to do it. I was able to glow the first and last row separately and first and second column separately)

void first_last_row();
void first_last_column();

int Led_Row_Pins[] = { 2 , 3 , 4 , 5 , 6 } ;             //   Anode pins are shorted in row_wise_manner
int Led_Column_Pins[] = {8  , 9 , 10 , 11 , 12} ;          //   Column Pins are shorted in column_wise_manner   
int Loop_Count = 5 ;
int Wait_Time_On = 1000 ;
int Wait_Time_Off = 500 ;
int i = 0 ;
int j = 0 ;
int state = 1 ;

void setup() {
  for( i = 0 ; i < Loop_Count ; i++ ){     // Anode Pins are connected in row_wise manner and are made LOW so that they dont conduct       
    pinMode(Led_Row_Pins[i],OUTPUT);
    digitalWrite(Led_Row_Pins[i],LOW);
    pinMode(Led_Column_Pins[i],OUTPUT);    // Cathode Pins are connected in column_wise manner and are made HIGH so that they dont conduct
    digitalWrite(Led_Column_Pins[i],HIGH); 
  }
}

void loop() { 
      first_last_row();
      delay(1000);
      first_last_column();
}


void first_last_row()
{
  for( i = 0 ; i < Loop_Count ; i++ )// Led First And Last Row 
   {
     for( j = 0 ; j < Loop_Count ; j++)
     {
       if( i == 0 || i == 4 )
       {
         digitalWrite(Led_Row_Pins[i],state); //Led_On_State
         digitalWrite(Led_Column_Pins[j],!state);
       }
     }
   }
}


void first_last_column()
{

  for( j = 0 ; j < Loop_Count ; j++ )// Led First And Last Column
   {
     for( i = 0 ; i < Loop_Count ; i++)
     {
       if( j == 0 || j == 4 )
       {
        digitalWrite(Led_Row_Pins[i],state);//    Led_On_State
        digitalWrite(Led_Column_Pins[j],!state);
       }
     }
   }
}

I need the first row and the last row and first column and last column led glow together so that it forms the shape of a square but I was able to glow them separately only first row and last row together and first column and last column.

解决方案

In your specific case you could switch on the first and last row for e.g. 1msec then switch on the first and last column for 1msec and repeat this. I further suggest to switch off all LEDs before switching on a new pattern.

void loop() { 
      static int show_rows = 0;

      switch_all_off();
      if(show_rows) {
          first_last_row();
      } else {
          first_last_column();
      }
      show_rows = !show_rows;
      delay(10);
}

void switch_all_off()
{
    int off = 0;
    for( j = 0 ; j < Loop_Count ; j++ )
    {
          digitalWrite(Led_Row_Pins[i],off);
          digitalWrite(Led_Column_Pins[j],!off);
    }
}

With this first step the LEDs that are contained both in the rows and in the columns will be brighter. To fix this you can exclude the corner LEDs either from the rows or from the columns, e.g.

void first_last_column()
{

    for( j = 0 ; j < Loop_Count ; j++ )// Led First And Last Column
    {
        for( i = 0 ; i < Loop_Count ; i++)
        {
            if((i != 0) && (i != 4) && ( j == 0 || j == 4 ))
            {
                digitalWrite(Led_Row_Pins[i],state);//    Led_On_State
                digitalWrite(Led_Column_Pins[j],!state);
            }
        }
    }
}

For a more general solution you have to loop over the rows, activate one row after the other and switch the column pins as necessary for the current row.

#define MAXLEDS 5
int states[MAXLEDS][MAXLEDS] = {
    { 1, 0, 0, 0, 1 },
    { 0, 1, 0, 1, 0 },
    { 0, 0, 1, 0, 0 },
    { 0, 1, 0, 1, 0 },
    { 1, 0, 0, 0, 1 }
};

void switch_leds(int row) {
    int i;

    /* switch off all rows */
    for(i = 0; i < MAXLEDS, i++) {
        digitalWrite(Led_Row_Pins[i], 0);
    }

    /* switch columns according to current row */
    for(i = 0; i < MAXLEDS; i++) {
        digitalWrite(Led_Column_Pins[i], !states[row][i]);
    }

    /* switch on current row */
    digitalWrite(Led_Row_Pins[row], 1);

}

void loop() { 
    static int row = 0;
    /* switch on LEDs in a single row */
    switch_leds(row);
    /* next row */
    row++; row %= MAXLEDS;
    /* The processing delay between calls to loop() is added to this delay. */
    delay(5);
}

All code examples are untested and may contain errors.

这篇关于是否有可能使用公共阳极配置以正方形的形式将5 * 5矩阵中的led发光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆