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

From XPUB & Lens-Based wiki
< User:Laurier Rochon
Revision as of 16:13, 24 October 2010 by Laurier Rochon (talk | contribs) (Created page with "== 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 woul...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

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.

#include <LiquidCrystal.h>

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();

LiquidCrystal lcd(4, 5, 0, 1, 2, 3);

String pat = "999099090990";
String mov = "555055050550";
const int buttonPin = 9;
int buttonState = 0;

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

void loop() {   
  
  buttonState = digitalRead(buttonPin);
  
    if (buttonState == HIGH) {     
      int patlen = pat.length();

      for (int p=0; p<patlen; p++) {
        
        //tone stuff
        
        int val1 = pat.charAt(p);
        int val2 = mov.charAt(p);
        int diff = (val1-val2)*100;
          
        tone(8, diff, 10);
          
        String first = mov.charAt(0);
        String second = "";
         
        for(int i=1;i<12;i++){
          second.concat(mov.charAt(i));
        }
         
        diff = diff/100;
        
        mov = second+first;
        
        //LED stuff
        
        if(a<8){row=0;}else{row=1;}
        if(lpos==8){lpos=0;}
        
        lcd.setCursor(lpos,row);
        
        if (diff == 4) {
          lcd.print('4', BYTE); 
        }else{
          lcd.print('5', BYTE);             
        }
        
        lpos++;
        a++;
        if(a==16){a=0;lcd.clear();}
        delay(200);
      }
    
    }else{
      txt="NOT PLAYING";
      readtext();
    }
  
  

}

void readtext(){
  for (int lindex = 0; lindex <= lentxt; lindex++) {
     
      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(); 
}