Stuck in the mud
Well, here’s part of my documentation on this emotive bleep project. I am *trying* to write code that has a vocabulary of bleeps with a few “phrases”…
I tried working out logically… that I need an array of phrases, each of which is an array of sequences. Beyond that, I am stuck, and after hours of circling the drain, I was wondering if one of you guys reading might be able to give me a few hints 🙂
———————————————————————————————————————————
//emotive bleeps
//static values
int speakerPin = 9;
int length = 10; // the number of notes
int tempo = 500;
//tone variables
String phrases[] = {“hi”,”bye”,”yes”,”no”,”alert”,”dying”, “hot” };
char notes[] = “qrstuvwxyz”; // a space represents a rest
int beats[] = {1,1,1,1,1,1,1,1,1,1};
//func to play each tone, regardless of sequence
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playPhrase(String phrase){
//get input from keyboard (processing sketch) and interpret that as a “phrase” string
String phrase = ‘bye’;
switch(phrase) {
//each phrase has the same 10 notes, but I am just going to have each phrase have different values for the wavelengths
case’hi’:
char names[] = {‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’ };
int tones[] = { 0, 600, 1800, 1000, 400, 1000, 0, 0, 500, 3000}; //wavelengths
case’bye’:
char names[] = {‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’ };
int tones[] = { 400, 800, 800, 0, 0, 0, 0, 0, 2500, 3000}; //wavelengths
default”:
char names[] = {‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’ };
int tones[] = { 400, 600, 800, 1000, 1200, 1550, 1750, 2000, 2500, 3000}; //wavelengths
break;
}
}
/*
void playNote (char note, int duration){
char names[] = {‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’ };
int tones[] = { 400, 600, 800, 1000, 1200, 1550, 1750, 2000, 2500, 3000}; //wavelengths
}*/
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ‘ ‘) {
delay(beats[i] * tempo); // rest
} else {
playPhrase(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
——————————————————————————————————————————————-
andywallace 6:05 am on February 3, 2011 Permalink |
I hope this isn’t too late to be helpful, but I’m looking at your code now. There are a few things I noticed right off the bat:
-you shouldn’t use “tone” as a variable name because it’s already reserved by arduino. That was the problem with the demo code we got last week. Name it toneValue or anything else that isn’t “tone”
-in your void loop you make a call to playPhrase(notes[i], beats[i] * tempo), but playPhrase only wants a string. I think this was supposed to be a call to playTone, which takes two ints.
I’m going to look it over a bit and see if I can find anything. Hooray insomnia!
andywallace 6:07 am on February 3, 2011 Permalink |
Also, the time zone on this blog is way off. It’s only 1:00 where I live.
breegeek 6:10 am on February 3, 2011 Permalink |
thank you kindly!
andywallace 6:26 am on February 3, 2011 Permalink |
Looked at it bit more. I’m not really sure what names[] does or exactly how your code works, so I’m not sure how helpful I can be from a distance. It’s worth noting that the arrays you made, names and tones, are only available within the function playPhrase. they are not global variables. You would need to declare them up at the top for them to be accessible to other functions.
Also, the example code we got the other day was pretty funky. It’s actually really easy to use a piezo as long as you now the frequency of the sound you want to make. Arduino as a pretty easy tone function: http://arduino.cc/en/Reference/Tone
You just set the tone to what you want and then delay for as long as you want it to last. After that you can use noTone to silence it. The code I posted uses it that way if it’s helpful.
I’m sure there are plenty of people in the class who are more knowledgable about sound than I am, but I’ll probably be in or around the 10th floor lab tomorrow from 3 to 4 or so. I have a fairly informal meeting with somebody from the games workshop, but I’d be happy to take a look at you code in person if you think it would be helpful.