编辑Piezo Element脚本~RedBoard

时间:2014-01-09 23:28:15

标签: c++ arduino

我将如何改变这个基本压电元素歌曲剧本中的音符数量。

目标

PS。不介意额外的输出:P还有别的东西!

88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

脚本:

int sensorPin = 0;
const int buzzerPin = 2;
const int sensorValue = analogRead(sensorPin);
const int songLength = 16;
const int numNotes= 8; 
const int tempo = 100;
const int off = LOW;
const int on = HIGH;

const int freq[] = {262, 294, 330, 349, 392, 440, 494, 523};
const int beat[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};

char names[] = {'c','d','e','f','g','a','b','C'};
char notes[] = "cd fda ag cdf dg gf ";



void setup()
{  
  pinMode(buzzerPin, OUTPUT);
  pinMode(13, INPUT);
  pinMode(12, INPUT);
  pinMode(11, INPUT);
  pinMode(10, INPUT);
  pinMode(9, INPUT);
  pinMode(8, INPUT);
  pinMode(6, INPUT);
  pinMode(5, INPUT);
  pinMode(4, INPUT);
  pinMode(0, INPUT);
}


void loop() 
{  
  int i, duration;
  for (i = 0; i < songLength; i++) 
  {
    duration = beat[i] * tempo;                                 
    if (notes[i] == ' ')          
    {
      delay(duration);           
    }
    else                          
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);            
    } 


    delay(tempo/10);      
  }
  while(true){
  }
}
int frequency(char note) 
{
  int i;   


  for (i = 0; i < numNotes; i++)  
  {
    if (names[i] == note)         
    {
      return(freq[i]);     
    }
  }

}

1 个答案:

答案 0 :(得分:0)

使用以上版本的上述草图,您可以简单地添加到beat []和notes []的末尾。请注意,notes []中的数字字符应等于beat []中的元素数量。正如我在

中看到的那样
int sensorPin = 0;
const int buzzerPin = 2;
const int sensorValue = analogRead(sensorPin);
//const int songLength = 16; // used sizeof() function below
//const int numNotes= 8;     // dido
const int tempo = 100;
const int off = LOW;
const int on = HIGH;

char names[] =     {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
const int freq[] = {262, 294, 330, 349, 392, 440, 494, 523};

const int beat[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
char notes[] = "cd fda ag cdf dg gf "; // Note length of notes[] should equal length of beat[]

void setup()
{  
  pinMode(buzzerPin, OUTPUT);
  pinMode(13, INPUT);
  pinMode(12, INPUT);
  pinMode(11, INPUT);
  pinMode(10, INPUT);
  pinMode(9, INPUT);
  pinMode(8, INPUT);
  pinMode(6, INPUT);
  pinMode(5, INPUT);
  pinMode(4, INPUT);
  pinMode(0, INPUT);
}

void loop() 
{  
  int i, duration;
  for (i = 0; i < sizeof(beat); i++) 
  {
    duration = beat[i] * tempo;                                 
    if (notes[i] == ' ')          
    {
      delay(duration);           
    }
    else                          
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);            
    } 


    delay(tempo/10);      
  }
  while(true){
  }
}
int frequency(char note) 
{
  int i;   


  for (i = 0; i < sizeof(notes); i++)  
  {
    if (names[i] == note)         
    {
      return(freq[i]);     
    }
  }

}