【问题标题】:Arduino beginner problemsArduino初学者问题
【发布时间】:2012-07-22 23:35:05
【问题描述】:

我对 Arduino 非常陌生,并且正在尝试示例。我在使用以下代码时遇到了很多问题:

#include <ctype.h>
#include <Arduino.h>
#include "Telegraph.h"
//#include <Telegraph.h>

char* LETTERS[]  =  {
  ".-", "-...", "-.-.", "-..", ".",      // A-E
  "..-.", "--.", "....", "..", ".---",   // F-J
  "-.-", ".-..", "--", "-.", "---",      // K-O
  ".--.", "--.-", ".-.", "...", "-",     // P-T
  "..-", "...-", ".--", "-..-", "-.--",  // U-Y
  "--.."                                 // Z
};

char* DIGITS[]  =  {
  "-----", ".----", "..---", "...--",     //0-3
  "....-", ".....", "-....", "--...",     //4-7
  "---..", "----."                        //8-9
};

Telegraph::Telegraph(const int outputPin, const int ditLength){
  _outputPin  =  outputPin;
  _ditLength  =  ditLength;
  _dahLength  =  dahLength;
  pinMode(_outputPin, OUTPUT);
}

void Telegraph::outputCode(const char* code){
  for(int i=0; i<strlen(code); i++){
    if( code[i]  ==  '.' )
      dit();
    else
      dah();
  }
}

void Telegraph::dit(){
  Serial.print(".");
  outputSymbol(_ditLength);
}

void Telegraph::dah(){
  Serial.print("-");
  outputSymbol(_dahLength);
}

void Telegraph::outputSymbol(const int length){
  digitalWrite(_outputPin, HIGH);
  delay(length);
  digitalWrite(_outputPin, LOW);
  delay(length);
}

void Telegraph::sendMessage(const char* message){
  for(int i=0;  i  <  strlen(message); i++){
    const char currentChar  =  toupper(message[i]);
    if( isalpha(currentChar) ){
      outputCode(LETTERS[currentChar  -  'A']);
      delay(_dahLength);
    }else if(isdigit(currentChar) ){
      outputCode(DIGITS[currentChar  -  '0']);
      delay(_dahLength);
    }else if(currentChar  =  ' '){
      Serial.print(" ");
      delay(_ditLength * 7);
    }
  }
  Serial.println();
}

我收到以下错误:

Telegraph.cpp:6:15: error: two or more data types in declaration of ‘LETTERS’
Telegraph.cpp: In constructor ‘Telegraph::Telegraph(int, int)’:
Telegraph.cpp:24:18: error: ‘dahLength’ was not declared in this scope
Telegraph.cpp: In member function ‘void Telegraph::sendMessage(const char*)’:
Telegraph.cpp:58:18: error: ‘LETTERS’ was not declared in this scope
Telegraph.cpp:63:30: error: assignment of read-only variable ‘currentChar’

对不起,如果这个问题过于笼统,但正如我所提到的,我很新,并不完全理解代码。

【问题讨论】:

    标签: compiler-errors arduino


    【解决方案1】:

    Telegraph.cpp:6:15: 错误:“LETTERS”声明中有两种或多种数据类型

    符号LETTERS 已经在其他地方定义了。也许在Telegraph.h?你必须寻找那个。

    Telegraph.cpp: In constructor ‘Telegraph::Telegraph(int, int)’:
    Telegraph.cpp:24:18: error: ‘dahLength’ was not declared in this scope
    

    相关代码:

      _dahLength  =  dahLength;
    

    是的,dahLength 没有定义。也许您想将此作为另一个参数添加到构造函数中?

    Telegraph.cpp: In member function ‘void Telegraph::sendMessage(const char*)’:
    Telegraph.cpp:58:18: error: ‘LETTERS’ was not declared in this scope
    

    有关该问题,请参见上文。解决第一个问题,忽略这个问题这么久。

    Telegraph.cpp:63:30: error: assignment of read-only variable ‘currentChar’
    

    这是由于

    }else if(currentChar  =  ' '){
    

    我确定您想使用 == 而不是 =

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 2020-11-23
      • 2015-03-16
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多