一些Arduino代码的问题

时间:2014-06-03 13:31:01

标签: arduino

所以,我正在构建一个我在互联网上看到的Arduino项目(reddit消息通知程序),我下载了代码,但变量有一些奇怪的问题...我不太了解Arduino代码。我似乎无法弄清问题。提前致谢!这是程序代码:

编辑:它没有编译(计算机上的Arduino应用程序没有编译那些不能工作的东西)。它说一些关于int不能成为const char *的东西。或类似的东西。我不记得确切。我很快就能在计算机上发布完整的错误。

#include <String.h>;
#include "pitches.h"


//define our variables
int ledPin = 11;
int x = 0;
int oldbreathe;
int slot;
String incomingWord, incomingByte, breathe, postscore;


//defines the melody played and the duration of each note
/////////////////////////////////////////////////////////
int melody[] = {
NOTE_G4, NOTE_B4, NOTE_D6, NOTE_G5, NOTE_GS4, NOTE_C5, NOTE_DS7, NOTE_GS5, NOTE_AS5, NOTE_D5, NOTE_F6, NOTE_AS5};
int noteDurations[] = {
32, 32, 32, 32,32,32,32,32,32,32,32,16 };
/////////////////////////////////////////////////////////






void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); 
breathe = 49;
slot    = 1;
}


void loop() {

if (breathe == 49){ // if (breathe = 1) then start breathe math function
int outVal = yforx(x);
analogWrite(ledPin, outVal);
delay(6); // modify the pace of breathing
x++;
}
if (breathe == 48){
analogWrite(ledPin, 0); // override the LED to OFF if (breathe = 0)
}

/////////////////////////////
/////////////////////////////

if (Serial.available() > 0) { //If we get a character over the serial line (serial activity)
incomingByte = Serial.read();  

if (incomingByte == 44){  //44 we received a delimiting comma. dump incomingWord to open array slot
  if (slot > 2){
    slot=1;
  }


  if (slot == 1){
    breathe = incomingWord;
    //        Serial.print("new breathe value: ");
    //        Serial.println(breathe);
    if (breathe == 49){playmusic();}
    slot++;


  }
  else 
    if (slot == 2){
    postscore = incomingWord;
    Serial.print("new postscore value: ");
    Serial.println(postscore);
    slot++;
  }


  incomingWord = "";
 }


else if (incomingByte != 13){ //received normal character. add to word
  incomingWord = incomingWord + incomingByte;
}

else 
  if (incomingByte == 13){  //13 is carriage return
  slot = 1;
  incomingWord = "";

}
} 
}


void playmusic() {
for (int thisNote = 0; thisNote < 12; thisNote++) {
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8); 
} 
}



int yforx(int x) {
return (-240*abs(sin(x*0.01)))+255; //sine wav`

1 个答案:

答案 0 :(得分:0)

问题似乎在于你的字符串和字符处理。您已声明int oldbreathe;String incomingWord, incomingByte, breathe, postscore;但您正在处理incomingByte并以int身份呼吸。您可以使用

进行编译
String incomingWord;
int incomingByte, postscore;
int breathe;

而不是breathe = incomingWord;使用

    breathe = incomingWord.charAt(0);

它出现在两个地方。虽然它编译我不确定它是否会起作用,因为我不太明白你期望什么样的输入。代码看起来很复杂,但一次输入一个字节非常棘手。我建议在http://arduino.cc/en/Reference/StringObject阅读有关String类的内容。