segunda-feira, 30 de abril de 2012

Projeto - Primeiro Código Teclado.java

import java.util.*;
public class Teclado extends javax.swing.JFrame{
    int teclas[] = new int[20];
    double tempoPressionado[] = new double[20];
    int qtdElementos=0;
    String filetxt;
    int x=0;
    long tempoSemTocarInicial=0, tempoSemTocarFinal, tempoSemTocarTotal;
    long tempoApertadoDo, tempoSoltoDo, tempoTotalDo;
    long tempoApertadoRe, tempoSoltoRe, tempoTotalRe;
    long tempoApertadoMi, tempoSoltoMi, tempoTotalMi;
    long tempoApertadoFa, tempoSoltoFa, tempoTotalFa;
    long tempoApertadoSol, tempoSoltoSol, tempoTotalSol;
    long tempoApertadoLa, tempoSoltoLa, tempoTotalLa;
    long tempoApertadoSi, tempoSoltoSi, tempoTotalSi;
    File_IO readWrite = new File_IO();
    Scanner keyboard = new Scanner(System.in);
    public Teclado(){
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents(){

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Dó");
        jButton1.setMaximumSize(new java.awt.Dimension(73, 23));
        jButton1.setMinimumSize(new java.awt.Dimension(73, 23));
        jButton1.setPreferredSize(new java.awt.Dimension(73, 23));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mousePressed(java.awt.event.MouseEvent evt){
                jButton1MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt){
                jButton1MouseReleased(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setBackground(new java.awt.Color(255, 255, 255));
        jButton2.setText("Ré");
        jButton2.setMaximumSize(new java.awt.Dimension(73, 23));
        jButton2.setMinimumSize(new java.awt.Dimension(73, 23));
        jButton2.setPreferredSize(new java.awt.Dimension(73, 23));
        jButton2.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mousePressed(java.awt.event.MouseEvent evt){
                jButton2MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt){
                jButton2MouseReleased(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setBackground(new java.awt.Color(255, 255, 255));
        jButton3.setText("Mi");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mousePressed(java.awt.event.MouseEvent evt){
                jButton3MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt){
                jButton3MouseReleased(evt);
            }
        });
        jButton3.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setBackground(new java.awt.Color(255, 255, 255));
        jButton4.setText("Fa");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mousePressed(java.awt.event.MouseEvent evt){
                jButton4MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt){
                jButton4MouseReleased(evt);
            }
        });

        jButton5.setBackground(new java.awt.Color(255, 255, 255));
        jButton5.setText("Sol");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mousePressed(java.awt.event.MouseEvent evt){
                jButton5MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt){
                jButton5MouseReleased(evt);
            }
        });

        jButton6.setBackground(new java.awt.Color(255, 255, 255));
        jButton6.setText("La");
        jButton6.addMouseListener(new java.awt.event.MouseAdapter()
            public void mousePressed(java.awt.event.MouseEvent evt){
                jButton6MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt){
                jButton6MouseReleased(evt);
            }
        });

        jButton7.setBackground(new java.awt.Color(255, 255, 255));
        jButton7.setText("Si");
        jButton7.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mousePressed(java.awt.event.MouseEvent evt)
                jButton7MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt){
                jButton7MouseReleased(evt);
            }
        });
        jButton7.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("End");
        jButton8.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Selecionar Arquivo txt");
        jButton9.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("Stop");
        jButton10.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setText("Start");
        jButton11.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                jButton11ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton11)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton8)))
                .addGap(66, 66, 66))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton8)
                    .addComponent(jButton9)
                    .addComponent(jButton10)
                    .addComponent(jButton11))
                .addGap(20, 20, 20))
        );

        pack();
    }                       

    private void passarParaTxt(int elementos){
        readWrite.openOutputFile(filetxt+""+x);
        x++;
        for(int i = 0;i<elementos;i++){
            readWrite.writeOutputLine(""+teclas[i]);
            readWrite.writeOutputLine(""+tempoPressionado[i]+"");
        }
        readWrite.closeOutputFile();
    }
    private void comecarVazio(){
        tempoSemTocarInicial = System.currentTimeMillis();
    }
    private void terminarVazio(){
        tempoSemTocarFinal = System.currentTimeMillis();
        tempoSemTocarTotal = tempoSemTocarFinal - tempoSemTocarInicial;
        teclas[qtdElementos]= 0;
        tempoPressionado[qtdElementos] = tempoSemTocarTotal;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
    }
    private int checarCheio(int numeroDeElementos){
        if(numeroDeElementos>=19){
            passarParaTxt(numeroDeElementos);
            numeroDeElementos = 0;
        }
        return numeroDeElementos;
       
    }
   
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt){                                
    }                                       

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt){                                         
    }                                       
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                  
    }                                       

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt){                                        
             System.exit(0);
    }                                       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){                                  
    }                                       

    private void jButton1MousePressed(java.awt.event.MouseEvent evt){                                     
        terminarVazio();
        tempoApertadoDo = System.currentTimeMillis();
    }                                    

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt){                                      
        tempoSoltoDo = System.currentTimeMillis();
        tempoTotalDo = tempoSoltoDo - tempoApertadoDo;
        teclas[qtdElementos]= 1;
        tempoPressionado[qtdElementos] = tempoTotalDo;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
        comecarVazio();
    }                                     

    private void jButton2MousePressed(java.awt.event.MouseEvent evt){                                     
        terminarVazio();
        tempoApertadoDo = System.currentTimeMillis();
       
    }                                    

    private void jButton2MouseReleased(java.awt.event.MouseEvent evt){                                      
        tempoSoltoRe = System.currentTimeMillis();
        tempoTotalRe = tempoSoltoRe - tempoApertadoRe;
        teclas[qtdElementos]= 2;
        tempoPressionado[qtdElementos] = tempoTotalDo;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
        comecarVazio();
    }                                     

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt){                                        
        filetxt = keyboard.next();
    }                                       

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt){                                         
        tempoSemTocarFinal = System.currentTimeMillis();
        tempoSemTocarTotal = tempoSemTocarFinal - tempoSemTocarInicial;
        passarParaTxt(qtdElementos);
    }                                        

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt){                                         
        tempoSemTocarInicial = System.currentTimeMillis();
    }                                        

    private void jButton3MousePressed(java.awt.event.MouseEvent evt){                                     
        terminarVazio();
        tempoApertadoMi = System.currentTimeMillis();
    }                                    

    private void jButton3MouseReleased(java.awt.event.MouseEvent evt){                                      
        tempoSoltoMi = System.currentTimeMillis();
        tempoTotalMi = tempoSoltoMi - tempoApertadoMi;
        teclas[qtdElementos]= 3;
        tempoPressionado[qtdElementos] = tempoTotalMi;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
        comecarVazio();
    }                                     

    private void jButton4MousePressed(java.awt.event.MouseEvent evt){                                     
        terminarVazio();
        tempoApertadoFa = System.currentTimeMillis();
    }                                    

    private void jButton4MouseReleased(java.awt.event.MouseEvent evt){                                      
        tempoSoltoFa = System.currentTimeMillis();
        tempoTotalFa = tempoSoltoFa - tempoApertadoFa;
        teclas[qtdElementos]= 4;
        tempoPressionado[qtdElementos] = tempoTotalFa;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
        comecarVazio();
    }                                     

    private void jButton5MousePressed(java.awt.event.MouseEvent evt){                                     
        terminarVazio();
        tempoApertadoSol = System.currentTimeMillis();
    }                                    

    private void jButton5MouseReleased(java.awt.event.MouseEvent evt){                                      
        tempoSoltoSol = System.currentTimeMillis();
        tempoTotalSol = tempoSoltoSol - tempoApertadoSol;
        teclas[qtdElementos]= 5;
        tempoPressionado[qtdElementos] = tempoTotalSol;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
        comecarVazio();
    }                                     

    private void jButton6MousePressed(java.awt.event.MouseEvent evt){                                     
        terminarVazio();
        tempoApertadoLa = System.currentTimeMillis();
    }                                    

    private void jButton6MouseReleased(java.awt.event.MouseEvent evt){                                      
        tempoSoltoLa = System.currentTimeMillis();
        tempoTotalLa = tempoSoltoLa - tempoApertadoLa;
        teclas[qtdElementos]= 6;
        tempoPressionado[qtdElementos] = tempoTotalLa;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
        comecarVazio();
    }                                     

    private void jButton7MousePressed(java.awt.event.MouseEvent evt){                                     
        terminarVazio();
        tempoApertadoSi = System.currentTimeMillis();
    }                                    

    private void jButton7MouseReleased(java.awt.event.MouseEvent evt){                                      
        tempoSoltoSi = System.currentTimeMillis();
        tempoTotalSi = tempoSoltoSi - tempoApertadoSi;
        teclas[qtdElementos]= 7;
        tempoPressionado[qtdElementos] = tempoTotalSi;
        qtdElementos++;
        qtdElementos = checarCheio(qtdElementos);
        comecarVazio();
    }                                     

    public static void main(String args[]){
       
try{
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()){
                if ("Nimbus".equals(info.getName())){
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }

        catch (ClassNotFoundException ex){
            java.util.logging.Logger.getLogger(Teclado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        catch (InstantiationException ex){
            java.util.logging.Logger.getLogger(Teclado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        catch (IllegalAccessException ex){
            java.util.logging.Logger.getLogger(Teclado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        catch (javax.swing.UnsupportedLookAndFeelException ex){
            java.util.logging.Logger.getLogger(Teclado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable(){

            public void run() {
                new Teclado().setVisible(true);
            }
        });
    }
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
}

      

segunda-feira, 16 de abril de 2012

Projeto - Primeiro Código Piano.ino

int som;
int musica;
int entrada1, entrada2, entrada3, entrada4, entrada5, entrada6, entrada7, entrada8;
int vB1=0, vB2=0, vB3=0, vB4=0, vB5=0, vB6=0, vB7=0, vB8=0;
int tensao1, tensao2, tensao3, tensao4, tensao5, tensao6, tensao7, tensao8;
void setup(){
       Serial.begin(9600);
       pinMode(9,OUTPUT);
       pinMode(A0, INPUT);
       pinMode(A1, INPUT);
       pinMode(A2, INPUT);
       pinMode(A3, INPUT);
       pinMode(A4, INPUT);
       pinMode(A5, INPUT);
       pinMode(A6, INPUT);
}
void PlayDo(int duracao){
       tone(9, 261, 1000);
}
void PlayRe(int duracao){
       tone(9, 293, 1000);
}
void PlayMi(int duracao){
       tone(9, 329, 1000);
}
void PlayFa(int duracao){
       tone(9, 349, 1000);
}
void PlaySol(int duracao){
       tone(9, 392, 1000);
}
void PlayLa(int duracao){
       tone(9, 440, 1000);
}
void PlaySi(int duracao){
       tone(9, 494, 1000);
}
void loop(){
       entrada1 = analogRead(A0);
       vB1 = entrada1 * (5.000000/1024);
       tensao1 = 5-vB1;
       entrada1 = analogRead(A1);
       vB2 = entrada2 * (5.000000/1024);
       tensao2 = 5-vB2;
       entrada3 = analogRead(A2);
       vB3 = entrada3 * (5.000000/1024);
       tensao3 = 5-vB3;
       entrada1 = analogRead(A3);
       vB4 = entrada4 * (5.000000/1024);
       tensao4 = 5-vB4;
       entrada1 = analogRead(A4);
       vB5 = entrada5 * (5.000000/1024);
       tensao5 = 5-vB5;
       entrada1 = analogRead(A5);
       vB6 = entrada6 * (5.000000/1024);
       tensao6 = 5-vB6;
       entrada1 = analogRead(A6);
       vB7 = entrada7 * (5.000000/1024);
       tensao7 = 5-vB7;
       if(tensao1>4) PlayDo(1);
       if(tensao2>4) PlayRe(1);
       if(tensao3>4) PlayMi(1);
       if(tensao4>4) PlayFa(1);
       if(tensao5>4) PlaySol(1);
       if(tensao6>4) PlayLa(1);
       if(tensao7>4) PlaySi(1);
       som = map(musica, 80.0, 180.0, 264, 264);
       tone(9, som, 10000);
}

segunda-feira, 2 de abril de 2012

Projeto - Primeira Apresentação

Projeto Hit The Light

Integrantes:
Augusto Cesar Pupo Vieira
Cássia de Souza Corrêa
Denilson Gianeiz Junior
Filipe Ricardo Reis Sbragio

Materiais:
Acrílico ou vidro e molde para a criação das teclas
Arduino
Caixa de som
LED’s coloridas
Resistores

Projeto:
A primeira parte do projeto consiste em criar um teclado normal. O teclado inicialmente terá 8 teclas para testes, após os testes iremos criar o teclado inteiro.
As teclas serão feitas de acrílico ou vidro e serão ocas, dentro de cada tecla haverá um LED de cor diferente, ao tocar uma nota, a tecla irá acender.
Após o teclado estar completo, iremos programar o arduino para reconhecer arquivos de texto em que estão escritos a frequência e a duração das notas que serão tocadas. O teclado irá então tocar uma música.