Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编辑压电元件脚本~红板_C++_Arduino - Fatal编程技术网

C++ 编辑压电元件脚本~红板

C++ 编辑压电元件脚本~红板,c++,arduino,C++,Arduino,我将如何着手改变这个基本压电元件歌曲脚本中的音符数量 目标 附言:不要介意额外的输出:还有其他的东西 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

我将如何着手改变这个基本压电元件歌曲脚本中的音符数量

目标 附言:不要介意额外的输出:还有其他的东西

88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

脚本:

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]);     
    }
  }

}

使用上述草图的以下版本,您可以简单地添加到节拍[]和音符[]的末尾。注意,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]);     
    }
  }

}