Making a personalized library

Hi,
This is my first post and i need some help.

I'm helping building a project - is a school one.

The language is in Portuguese but i think that everyone understands the project.

However, i want to simplify the code by making a library.

void a(){
  //Primeira fita
  SendByte(apagado, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(cor, 4, 3);
  latch(5); 

  //Segunda fita
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  latch(11);

  //Terceira Fita
  SendByte(apagado, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(cor, 26, 24);
  latch(28);

  //Quarta Fita
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  latch(40);

  //Quinta Fita
  SendByte(apagado, 46, 44);
  SendByte(cor, 46, 44);
  SendByte(apagado, 46, 44);
  SendByte(cor, 46, 44);
  latch(48);
}

We have the whole alfabet plus the numbers from 0 to 10 and some others like "-", space... It's about 45 times that peace of code...!

Then the case:

void lista (char letra){
 
  switch (letra) {
    
  case 'a':
  a();
  break;
  
  case 'b':
  b();
  break;
  
  case 'c':
  c();
  break;

I've read the library tutorial, but i can' implement this (big) part of the code in one library file. Is this possible?

Thx a lot
EddY

P.S.
Could i put this in the same library?

// INPUTS DE TODAS AS FITAS

//1ª FITA
#define SIPIN 2 // cabo verde
#define DIPIN 3 // cabo branco
#define CLKPIN 4 // cabo cinzento
#define LATCHPIN 5 // cabo castanho

//2ªFITA
#define SIPIN 8 // cabo verde
#define DIPIN 9 // cabo branco
#define CLKPIN 10 // cabo cinzento
#define LATCHPIN 11 // cabo castanho

//3ªFITA
#define SIPIN 22 // cabo verde
#define DIPIN 24 // cabo branco
#define CLKPIN 26 // cabo cinzento
#define LATCHPIN 28 // cabo castanho

//4ªFITA
#define SIPIN 34 // cabo verde
#define DIPIN 36 // cabo branco
#define CLKPIN 38 // cabo cinzento
#define LATCHPIN 40 // cabo castanho

//5ªFITA
#define SIPIN 42 // cabo verde
#define DIPIN 44 // cabo branco
#define CLKPIN 46// cabo cinzento
#define LATCHPIN 48 // cabo castanho

And this...:

  //SPEED Definido fio verde
  pinMode(2, OUTPUT); //Fita 1
  pinMode(8, OUTPUT); // Fita 2
  pinMode(22, OUTPUT); // Fita 3
  pinMode(34, OUTPUT); // Fita 4
  pinMode(42, OUTPUT); // Fita 5

  //DIPIN Definido fio branco
  pinMode(3, OUTPUT); //Fita 1
  pinMode(9, OUTPUT); // Fita 2
  pinMode(24, OUTPUT); // Fita 3
  pinMode(36, OUTPUT); // Fita 4
  pinMode(44, OUTPUT); // Fita 5

  //CLOCK Definido fio cinzento
  pinMode(4, OUTPUT); // Fita 1
  pinMode(10, OUTPUT); // Fita 2
  pinMode(26, OUTPUT); // Fita 3
  pinMode(38, OUTPUT); // Fita 4
  pinMode(46, OUTPUT); // Fita 5

  //LATCH definido fio castanho
  pinMode(5, OUTPUT); // Fita 1
  pinMode(11, OUTPUT); // Fita 2
  pinMode(28, OUTPUT); // Fita 3
  pinMode(40, OUTPUT); // Fita 4
  pinMode(48, OUTPUT); // Fita 5

  //SPEED Definido fio verde
  digitalWrite(2, LOW); // Fita 1
  digitalWrite(8, LOW); // Fita 2
  digitalWrite(22, LOW); // Fita 2
  digitalWrite(34, LOW); // Fita 4
  digitalWrite(42, LOW); // Fita 5

  //DIPIN Definido fio branco
  digitalWrite(3, LOW); //Fita 1
  digitalWrite(9, LOW); //Fita 2
  digitalWrite(24, LOW); //Fita 3
  digitalWrite(36, LOW); //Fita 4
  digitalWrite(44, LOW); //Fita 5

  //CLOCK Definido fio cinzento
  digitalWrite(4, LOW); //Fita 1
  digitalWrite(10, LOW); //Fita 2
  digitalWrite(26, LOW); //Fita 3
  digitalWrite(38, LOW); //Fita 4
  digitalWrite(46, LOW); //Fita 5

  //LATCH definido fio castanho
  digitalWrite(5, LOW); //Fita 1 
  digitalWrite(11, LOW); //Fita 2
  digitalWrite(28, LOW); //Fita 3
  digitalWrite(40, LOW); //Fita 4
  digitalWrite(48, LOW);

There are two approaches that you can use, but you will need to learn C++ (or at least the C-ish subset). I don't have the time (or desire frankly) to duplicate an intro to programming course.

  1. Since each letter just writes some parts and leaves other parts blank, you would have an array of values to turn on and off. Given memory is at a premium in the Arduino, you need to use a space efficient way of encoding this. Given you seem to be using a 5x3 matrix for the LEDs, you could presumably use a 16 bit unsigned value to hold the values, and use 15 of those bits to hold each LED value. Then in the setup function you initialize this array at runtime.

Something like:

// Encode 15 digits in a single int.  Because ints are 16 bits, you would need another
// encoding to use something larger than 5x3

#define COL(A,B,C) (((A) << 2) | ((B) << 1) | (C))
#define ROW(A,B,C,D,E) (((A) << 12) | ((B) << 9) | ((C) << 6) | ((D) << 3) | (E))

// ....

unsigned int letters[256];

void setup (void)
{
  letters['a'] = ROW (COL (1, 1, 1),
                                     COL (1, 0, 1),
                                     COL (1, 0, 1),
                                     COL (1, 1, 1),
                                     COL (1, 0, 0));
  // ...
}

void print_char (unsigned char ch)
{
   unsigned val = letter[ch];
   int r, c;

   for (r = 0; r < 5; r++)
      {
         for (c = 0; c < 3; c++)
            {
               if ((val & (1u << ((r * 3) + c))  != 0)
                  {
                      // turn on row r, column c led
                   }
               else
                   {
                       // turn off row r, column c led
                    }
             }
      }
}
  1. Use function pointers:
typedef void void_func (void);

// Array of 256 functions, for each letter or NULL if no
// function is present.
void_func *letter_func[256];

extern void a (void);

void setup (void)
{
   letter_func['a'] = a;
   // ....
}

void print_char (unsigned char ch)
{
   if (letter_func[ch] != (void_func *)0)
      letter_func[ch] ();
}
[quote author=MichaelMeissner link=topic=113095.msg850492#msg850492 date=1341543164]
(...)  I don't have the time (or desire frankly) to duplicate an intro to programming course.

[/quote]

Hi, 1st of all thx for your reply, Michael. I did not ask a programming course, obviously. I'm gonna show the full program...
The intention is to control each led independently. The strip uses the HL1606.

We control the leds in the strip like FIFO (First In First Out). For this i used this function:

void SendByte(unsigned char it, int clkpin, int dipin)
{
   
  digitalWrite(clkpin, LOW);

  char x;
  for(x=0;x < 8; x++)
  { 
    if(B10000000 & it)
      digitalWrite(dipin, HIGH); 
    else
      digitalWrite(dipin, LOW); 
    it = it<<1;
    delayMicroseconds(CLOCK_DELAY);
    digitalWrite(clkpin, HIGH);
    digitalWrite(clkpin, LOW);

  }
}

void latch(int LATCH)
{
  digitalWrite(LATCH, HIGH);
  delayMicroseconds(1);
  digitalWrite(LATCH, LOW);
}

This was the easiest way that i found to do this:
(perhaps the code could be improved... for sure)

char apagado=B10000000;
char cor = B10000100;

void d(){
  //Primeira fita
  SendByte(apagado, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(apagado, 4, 3);
  latch(5); 

  //Segunda fita
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  latch(11);

  //Terceira Fita
  SendByte(apagado, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(apagado, 26, 24);
  SendByte(cor, 26, 24);
  latch(28);

  //Quarta Fita
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  latch(40);

  //Quinta Fita
  SendByte(apagado, 46, 44);
  SendByte(cor, 46, 44);
  SendByte(cor, 46, 44);
  SendByte(apagado, 46, 44);
  latch(48);
}

FULL CODE


// INPUTS DE TODAS AS FITAS

//1ª FITA
#define SIPIN 2 // cabo verde
#define DIPIN 3 // cabo branco
#define CLKPIN 4 // cabo cinzento
#define LATCHPIN 5 // cabo castanho

//2ªFITA
#define SIPIN 8 // cabo verde
#define DIPIN 9 // cabo branco
#define CLKPIN 10 // cabo cinzento
#define LATCHPIN 11 // cabo castanho

(..)

int aux=0;

#define LEDS 20    //Define o número de LEDS disponíveis na fita
#define CLOCK_DELAY (LEDS * 250)/1000  // tempo necessário entre os leds e o clock

char apagado=B10000000;
char cor = B10000100;

char letra;

//Teclado
#include <PS2Keyboard.h>

const int DataPin = 18;

//clock pin teclado
const int IRQpin =  19;

PS2Keyboard keyboard;

void setup()
{

// Teclado
keyboard.begin(DataPin, IRQpin);
Serial.begin(9600);

// outputs de todas as fitas
    
  //SPEED Definido fio verde
  pinMode(2, OUTPUT); //Fita 1
  pinMode(8, OUTPUT); // Fita 2
(...)
}  

void loop()
{
if (aux==0){
apagar_fita();
aux=1;
}

// Lê o primeiro caracter para a cor
letra = teclado ();
lista (letra);
  
}

/********************************************************/

/*
ÁREA DE FUNÇÕES
*/

void SendByte(unsigned char it, int clkpin, int dipin)
{
   
  digitalWrite(clkpin, LOW);

  char x;
  for(x=0;x < 8; x++)
  { 
    if(B10000000 & it)
      digitalWrite(dipin, HIGH); 
    else
      digitalWrite(dipin, LOW); 
    it = it<<1;
    delayMicroseconds(CLOCK_DELAY);
    digitalWrite(clkpin, HIGH);
    digitalWrite(clkpin, LOW);

  }
}


void latch(int LATCH)
{
  digitalWrite(LATCH, HIGH);
  delayMicroseconds(1);
  digitalWrite(LATCH, LOW);
}

// funções números & letras 
void zero (){
  //Primeira fita
  SendByte(apagado, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(cor, 4, 3);
  latch(5); 

  //Segunda fita
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  latch(11);

  //Terceira Fita
  SendByte(apagado, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(apagado, 26, 24);
  SendByte(cor, 26, 24);
  latch(28);

  //Quarta Fita
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  latch(40);

  //Quinta Fita
  SendByte(apagado, 46, 44);
  SendByte(cor, 46, 44);
  SendByte(cor, 46, 44);
  SendByte(cor, 46, 44);
  latch(48);
}  

(...)
}  

void a(){
  //Primeira fita
  SendByte(apagado, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(cor, 4, 3);
  SendByte(cor, 4, 3);
  latch(5); 

  //Segunda fita
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(cor, 10, 9);
  latch(11);

  //Terceira Fita
  SendByte(apagado, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(cor, 26, 24);
  latch(28);

  //Quarta Fita
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(cor, 38, 36);
  latch(40);

  //Quinta Fita
  SendByte(apagado, 46, 44);
  SendByte(cor, 46, 44);
  SendByte(apagado, 46, 44);
  SendByte(cor, 46, 44);
  latch(48);
}

(...)
}

  
char teclado (){
 if (keyboard.available()) {
       
//leitura do caracter
    
    char c = keyboard.read();
    
//permite asssociar ao esc a funçao de apagar a fita
    if  (c == PS2_ESC) {
          apagar_fita(); 
    
  }
//COR DE ROSA   // Choose coulors!
    if (c == PS2_LEFTARROW) {
          cor = B10000101;
          
    } 
//COR VERMELHA  
  if (c == PS2_RIGHTARROW) {
          cor = B10000100;
    } 
//COR AZUL
  if (c == PS2_DOWNARROW) {
          cor = B10000001;
    } 
//COR AMARELO    
  if (c== PS2_UPARROW) {
          cor = B10010100;
  } 

//COR 3 JUNTAS 
   if (c== PS2_PAGEDOWN) {
         cor = B10101010;
   }
   
//COR VERMELHO CLARO
    if (c== PS2_PAGEUP) {
         cor = B10101110;
    }

// COR LILÁS
    if (c== PS2_DELETE) {
         cor = B11001010;
    } 

// COR BRANCA
    if (c== PS2_ENTER) {
         cor = B11111111;
    }    

     
//ESCREVER HIFEN  
if (c == PS2_HYPHEN){
hifen();
  }
// ESCREVER Ç  
  if (c == PS2_C_CEDILLA){
cedilha();
  }
//ESPAÇO  
 if (c ==   PS2_TAB){ 
espaco ();
  }
    }
}


// Função que apaga a fita toda
void apagar_fita ()
{
     
  for (int i=0 ; i<30 ; i++)
      {
  //Primeira fita
  SendByte(B10000000, 4, 3);
  
  latch(5); 

  //Segunda fita
  SendByte(B10000000, 10, 9);
  
  latch(11);

  // Terceira Fita
  SendByte(B10000000, 26, 24);
  
  latch(28);

  // Quarta Fita
  SendByte(B10000000, 38, 36);
  
  latch(40);

  // Quinta Fita
  SendByte(B10000000, 46, 44);
  
  latch(48);
      }
}


void hifen (){
  //Primeira fita
  SendByte(apagado, 4, 3);
  SendByte(apagado, 4, 3);
  SendByte(apagado, 4, 3);
  SendByte(apagado, 4, 3);
  latch(5); 

  //Segunda fita
  SendByte(apagado, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(apagado, 10, 9);
  latch(11);

  //Terceira Fita
  SendByte(apagado, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(cor, 26, 24);
  SendByte(cor, 26, 24);
  latch(28);

  //Quarta Fita
  SendByte(apagado, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(apagado, 38, 36);
  latch(40);

  //Quinta Fita
  SendByte(apagado, 46, 44);
  SendByte(apagado, 46, 44);
  SendByte(apagado, 46, 44);
  SendByte(apagado, 46, 44);
  latch(48);
}

void espaco (){
  //Primeira fita
  SendByte(apagado, 4, 3);
  SendByte(apagado, 4, 3);
  SendByte(apagado, 4, 3);
  SendByte(apagado, 4, 3);
  latch(5); 

  //Segunda fita
  SendByte(apagado, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(apagado, 10, 9);
  SendByte(apagado, 10, 9);
  latch(11);

  //Terceira Fita
  SendByte(apagado, 26, 24);
  SendByte(apagado, 26, 24);
  SendByte(apagado, 26, 24);
  SendByte(apagado, 26, 24);
  latch(28);

  //Quarta Fita
  SendByte(apagado, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(apagado, 38, 36);
  SendByte(apagado, 38, 36);
  latch(40);

  //Quinta Fita
  SendByte(apagado, 46, 44);
  SendByte(apagado, 46, 44);
  SendByte(apagado, 46, 44);
  SendByte(apagado, 46, 44);
  latch(48);
}

// cases de todas as letras

void lista (char letra){
 
  switch (letra) {
    
  case '0':
  zero();
  break;
  
  case '1':
  um();
  break;
  
  case '2':
  dois();
  break;

(...)
  
  case 'a':
  a();
  break; 

(...)
  
} 
}

The program as it is works 100%. However, i've tried to write the letters by sending a text message by phone.
I'm using the SM5100B shield.

I can't write letters in the two ways - only if the code is separated.

// Definições de SMS
#include <math.h>
#include <SPI.h>
#include <TinyGPS.h>


char inchar1=0;                //Will hold the incoming character from the Serial Port (sms).


(...)

#include <PS2Keyboard.h>

const int DataPin = 18;

//clock pin teclado
const int IRQpin =  19;

PS2Keyboard keyboard;

(...)
//Initialize GSM module serial port for communication.
    Serial1.begin(9600); //  for GSM module communication
    delay(30000); // give time for GSM module to register on network etc.
    Serial1.println("AT+CMGF=1"); // set SMS mode to text
    delay(200);
    Serial1.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
    delay(200);
    Serial1.println("AT+CMGD=1,4"); // delete all SMS
    delay(200);

(...)

if(Serial1.available() >0)
{
      inchar1=Serial1.read();    
      lista (inchar1); // read the letter and call the function to write the letter in the strips
      delay(10);
}
else 
{
// function to write using PS2 keyboard. Program in the post above.
}

}