User:Laurier Rochon/prototyping/clapping music with arduino LED screen pushbutton: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 1: Line 1:
== Shifting pattern /w Arduino ==
== Clapping music + shifting pattern /w Arduino ==




Line 22: Line 22:


<source lang="C">
<source lang="C">
//LED screen library
#include <LiquidCrystal.h>
#include <LiquidCrystal.h>


//globals
const int numRows = 2;
const int numRows = 2;
const int numCols = 16;
const int numCols = 16;
Line 36: Line 38:
int lentxt=txt.length();
int lentxt=txt.length();


//pins on Arduino
LiquidCrystal lcd(4, 5, 0, 1, 2, 3);
LiquidCrystal lcd(4, 5, 0, 1, 2, 3);


//normal pattern
String pat = "999099090990";
String pat = "999099090990";
//shifting pattern
String mov = "555055050550";
String mov = "555055050550";
//pushbutton pin
const int buttonPin = 9;
const int buttonPin = 9;
//start in OFF position
int buttonState = 0;
int buttonState = 0;


void setup() {
void setup() {
  //initialize stuffs
   pinMode(buttonPin, INPUT);   
   pinMode(buttonPin, INPUT);   
   lcd.begin(numCols,numRows);
   lcd.begin(numCols,numRows);
Line 49: Line 57:


void loop() {   
void loop() {   
    
   //read the button
   buttonState = digitalRead(buttonPin);
   buttonState = digitalRead(buttonPin);
    
   //if it's pressed
     if (buttonState == HIGH) {     
     if (buttonState == HIGH) {     
      //get the pattern length
       int patlen = pat.length();
       int patlen = pat.length();


Line 61: Line 70:
         int val1 = pat.charAt(p);
         int val1 = pat.charAt(p);
         int val2 = mov.charAt(p);
         int val2 = mov.charAt(p);
        //compute the difference in value
         int diff = (val1-val2)*100;
         int diff = (val1-val2)*100;
         
        //play it 
         tone(8, diff, 10);
         tone(8, diff, 10);
         
       
        //starting the shifting
         String first = mov.charAt(0);
         String first = mov.charAt(0);
         String second = "";
         String second = "";
       
 
        //is there an easier way to do this? I don't know
         for(int i=1;i<12;i++){
         for(int i=1;i<12;i++){
           second.concat(mov.charAt(i));
           second.concat(mov.charAt(i));
         }
         }
       
       
         diff = diff/100;
         diff = diff/100;
          
          
        //put the new mov value together
         mov = second+first;
         mov = second+first;
          
          
         //LED stuff
         //LED stuff
          
         //if you're in the 8 first spots, you should be row #1. otherwise row #2
        //this hack was needed because my screen is 16x1, not 16x2
         if(a<8){row=0;}else{row=1;}
         if(a<8){row=0;}else{row=1;}
        //if you get to spot 9, go back to 0
         if(lpos==8){lpos=0;}
         if(lpos==8){lpos=0;}
          
          
        //set the cursor on the LED
         lcd.setCursor(lpos,row);
         lcd.setCursor(lpos,row);
          
          
        //simply could NOT manage to print the byte directly, this is retarded
         if (diff == 4) {
         if (diff == 4) {
           lcd.print('4', BYTE);  
           lcd.print('4', BYTE);  
Line 89: Line 108:
         }
         }
          
          
       //done. increment the values
         lpos++;
         lpos++;
         a++;
         a++;
Line 96: Line 116:
      
      
     }else{
     }else{
      //if the button is not 'on', then scroll through this text
       txt="NOT PLAYING";
       txt="NOT PLAYING";
       readtext();
       readtext();
Line 107: Line 128:
   for (int lindex = 0; lindex <= lentxt; lindex++) {
   for (int lindex = 0; lindex <= lentxt; lindex++) {
      
      
      //same here
       if(a<8){row=0;}else{row=1;}
       if(a<8){row=0;}else{row=1;}
       if(lpos==8){lpos=0;}
       if(lpos==8){lpos=0;}


Line 118: Line 139:
       a++;
       a++;
        
        
       if(a==16){
       if(a==16){ a=0; lcd.clear();
        a=0;
        lcd.clear();
       }   
       }   
   }
   }

Revision as of 23:24, 24 October 2010

Clapping music + shifting pattern /w Arduino

Constant pattern + shifting pattern using a single channel piezo and values subtraction. I didn't want to use two piezos/speakers as that would simply duplicate the code into something slightly less interesting. Result of the subtraction of the two patterns is printed to the screen and loops.

Got some help from http://www.ladyada.net/learn/lcd/charlcd.html

Hard

  • 16-pin 16x1 LED display
  • pushbutton
  • piezo
  • 3 resistors
  • breadboard
  • Arduino 168 Diecimila

{{#ev:vimeo|16137046|640}}

Soft

This code needs more work (i.e. more modular). Some things are hard-coded and no comments are in yet...more to come. I had much trouble converting my string/int to char to send to the LCD screen. Atoi wouldn't do it and the built-in C functions wouldn't do it = ugly IF.

//LED screen library
#include <LiquidCrystal.h>

//globals
const int numRows = 2;
const int numCols = 16;
int a=0;
int row=0;
int lpos=0;

int pos1=0;
int pos2=0;

String txt="NOT PLAYING";
int lentxt=txt.length();

//pins on Arduino
LiquidCrystal lcd(4, 5, 0, 1, 2, 3);

//normal pattern
String pat = "999099090990";
//shifting pattern
String mov = "555055050550";
//pushbutton pin
const int buttonPin = 9;
//start in OFF position
int buttonState = 0;

void setup() {
  //initialize stuffs
  pinMode(buttonPin, INPUT);  
  lcd.begin(numCols,numRows);
}

void loop() {   
  //read the button
  buttonState = digitalRead(buttonPin);
  //if it's pressed
    if (buttonState == HIGH) {     
      //get the pattern length
      int patlen = pat.length();

      for (int p=0; p<patlen; p++) {
        
        //tone stuff
        
        int val1 = pat.charAt(p);
        int val2 = mov.charAt(p);
        //compute the difference in value
        int diff = (val1-val2)*100;
        //play it  
        tone(8, diff, 10);
        
        //starting the shifting
        String first = mov.charAt(0);
        String second = "";

        //is there an easier way to do this? I don't know
        for(int i=1;i<12;i++){
          second.concat(mov.charAt(i));
        }
        
 
        diff = diff/100;
        
        //put the new mov value together
        mov = second+first;
        
        //LED stuff
        //if you're in the 8 first spots, you should be row #1. otherwise row #2
        //this hack was needed because my screen is 16x1, not 16x2
        if(a<8){row=0;}else{row=1;}

        //if you get to spot 9, go back to 0
        if(lpos==8){lpos=0;}
        
        //set the cursor on the LED
        lcd.setCursor(lpos,row);
        
        //simply could NOT manage to print the byte directly, this is retarded
        if (diff == 4) {
          lcd.print('4', BYTE); 
        }else{
          lcd.print('5', BYTE);             
        }
        
        //done. increment the values
        lpos++;
        a++;
        if(a==16){a=0;lcd.clear();}
        delay(200);
      }
    
    }else{
      //if the button is not 'on', then scroll through this text
      txt="NOT PLAYING";
      readtext();
    }
  
  

}

void readtext(){
  for (int lindex = 0; lindex <= lentxt; lindex++) {
     
      //same here
      if(a<8){row=0;}else{row=1;}
      if(lpos==8){lpos=0;}

      lcd.setCursor(lpos,row);
      lcd.print(txt.charAt(lindex), BYTE);
      delay(100); 

      lpos++;
      a++;
      
      if(a==16){ a=0; lcd.clear();
      }  
   }
   lcd.clear(); 
}