User:Laurier Rochon/prototyping/clapping music with arduino LED screen pushbutton: Difference between revisions
No edit summary |
No edit summary |
||
Line 24: | Line 24: | ||
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. | 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. | ||
'''UPDATED : OCT 26 2010''' (please play around with it) | |||
<source lang="C"> | <source lang="C"> | ||
//import LED screen library (this is not necessary unless you have a screen) | |||
#include <LiquidCrystal.h> | #include <LiquidCrystal.h> | ||
//globals | |||
const int numRows = 2; | const int numRows = 2; | ||
const int numCols = 16; | const int numCols = 16; | ||
Line 37: | Line 41: | ||
int pos2=0; | int pos2=0; | ||
//default text | |||
String txt="NOT PLAYING"; | String txt="NOT PLAYING"; | ||
int lentxt=txt.length(); | int lentxt=txt.length(); | ||
//pins for the LED | |||
LiquidCrystal lcd(6, 7, 2, 3, 4, 5); | LiquidCrystal lcd(6, 7, 2, 3, 4, 5); | ||
//shifting patterns, only 1 necessary unless you're doing the shift | |||
String pat = "999999999999"; | String pat = "999999999999"; | ||
String mov = "555755757557"; | String mov = "555755757557"; | ||
//button vars | |||
const int buttonPin = 9; | const int buttonPin = 9; | ||
int buttonState = 0; | int buttonState = 0; | ||
void setup() { | void setup() { | ||
//initializing | |||
pinMode(buttonPin, INPUT); | pinMode(buttonPin, INPUT); | ||
lcd.begin(numCols,numRows); | lcd.begin(numCols,numRows); | ||
Line 54: | Line 64: | ||
void loop() { | void loop() { | ||
//read if the button is pressed | |||
buttonState = digitalRead(buttonPin); | buttonState = digitalRead(buttonPin); | ||
if (buttonState == HIGH) { | if (buttonState == HIGH) { | ||
//if so, get the length of the pattern | |||
int patlen = pat.length(); | int patlen = pat.length(); | ||
//loop for every part of the pattern... | |||
for (int p=0; p<patlen; p++) { | for (int p=0; p<patlen; p++) { | ||
//tone stuff | //tone stuff | ||
//compute the difference of the 2 values (if you're shifting) | |||
int val1 = pat.charAt(p); | int val1 = pat.charAt(p); | ||
int val2 = mov.charAt(p); | int val2 = mov.charAt(p); | ||
int diff = (val1-val2)*100; | int diff = (val1-val2)*100; | ||
//play the tone | |||
tone(8, diff, 10); | tone(8, diff, 10); | ||
//starting shifting (if you are doing it) | |||
String first = mov.charAt(0); | String first = mov.charAt(0); | ||
String second = ""; | String second = ""; | ||
Line 82: | Line 97: | ||
//screen stuff | //screen stuff | ||
//if you're at the end of the screen start again | |||
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;} | ||
//set the cursor | |||
lcd.setCursor(lpos,row); | lcd.setCursor(lpos,row); | ||
Line 94: | Line 111: | ||
a++; | a++; | ||
//and wait | |||
delay(150); | delay(150); | ||
} | } | ||
}else{ | }else{ | ||
//if button is not pressed, just scroll the text | |||
txt="NOT PLAYING"; | txt="NOT PLAYING"; | ||
readtext(); | readtext(); | ||
Line 107: | Line 126: | ||
void readtext(){ | void readtext(){ | ||
//to read an arbitrary text on the screen | |||
for (int lindex = 0; lindex <= lentxt; lindex++) { | for (int lindex = 0; lindex <= lentxt; lindex++) { | ||
Revision as of 15:22, 26 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.
UPDATED : OCT 26 2010 (please play around with it)
//import LED screen library (this is not necessary unless you have a screen)
#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;
//default text
String txt="NOT PLAYING";
int lentxt=txt.length();
//pins for the LED
LiquidCrystal lcd(6, 7, 2, 3, 4, 5);
//shifting patterns, only 1 necessary unless you're doing the shift
String pat = "999999999999";
String mov = "555755757557";
//button vars
const int buttonPin = 9;
int buttonState = 0;
void setup() {
//initializing
pinMode(buttonPin, INPUT);
lcd.begin(numCols,numRows);
}
void loop() {
//read if the button is pressed
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
//if so, get the length of the pattern
int patlen = pat.length();
//loop for every part of the pattern...
for (int p=0; p<patlen; p++) {
//tone stuff
//compute the difference of the 2 values (if you're shifting)
int val1 = pat.charAt(p);
int val2 = mov.charAt(p);
int diff = (val1-val2)*100;
//play the tone
tone(8, diff, 10);
//starting shifting (if you are doing it)
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;
//screen stuff
//if you're at the end of the screen start again
if(a<8){row=0;}else{row=1;}
if(lpos==8){lpos=0;}
//set the cursor
lcd.setCursor(lpos,row);
String newstr = diff;
if(a==16){a=0;lcd.clear();}
lcd.print(newstr.charAt(0), BYTE);
lpos++;
a++;
//and wait
delay(150);
}
}else{
//if button is not pressed, just scroll the text
txt="NOT PLAYING";
readtext();
}
}
void readtext(){
//to read an arbitrary text on the screen
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();
}