2017-08-16 19 views
1

シリアルモニタに文字列/文字を入力する際に​​問題があります。私はプログラミングの初心者ですので、解決方法はわかりません。シリアルモニタに単​​一の文字列/文字を入力する

私には、switch case文のように機能するif/else文があります。私はスイッチのケースから始めましたが、私はそれを使って多くの問題を開発していました。文字/文字(A、S、M、D、P)を入力してそれぞれのケースを指し示していました。私はswitch caseがintのためであることを知っているので、私はあきらめて複数のif/elseステートメントに移りました。

I()は(ボイドループ内で)試みた、

char theOperator = dataInput(); 

if (theOperator == 'A') { 
    // If input is equal to 1, carry out function 'addition()' 
    addition(); 
} 

それは私が '付加' 機能を持っているエラーメッセージを表示します。入力用のコードが使用されているのはここ

は、私が)(Serial.readのような同様のことを試してみましたが、これは私のプログラムの開始をループしないか

char theOperator; 

char dataInput() { 
    while (!Serial.available()) { 
    // Wait for the user to enter value 
    } 
    return Serial.parseInt(); 
} 

、(グローバルに定義された)であります私が値を入力できるようにするか、まったく値を取らないだけです。

私のプログラム(if/elseなどの問題はトップにあります)を理解できるように、私は以下の完全なコードを掲載します。

* EDIT:言及するのを忘れ、私は、

char theOperator = dataInput(); 

    if (theOperator == 1) { 
    // If input is equal to 1, carry out function 'addition()' 
    addition(); 
    } 

をのような何かを行うことができ、これは動作します。入力するときにしかし、私はそれはあなたがSerial.parseInt()でシリアル入力を取っているあなたのchar dataInput()機能ではA、S、M、D、またはPの代わりに、1、2、3、4、5

#define LED_RED 5 
#define LED_ORANGE 6 
#define LED_YELLOW 9 
#define LED_GREEN 10 
#define LED_BLUE 11 

int potValue; 

#define potPin A0 

// Limits the range of the numbers entered to between -99 and 999 
long firstNumber = constrain(firstNumber, -99, 999); 
long secondNumber = constrain(secondNumber, -99, 999); 
long value; 

char theOperator; 

char dataInput() { 
    while (!Serial.available()) { 
    // Wait for the user to enter value 
    } 
    return Serial.parseInt(); 
} 

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

void setup() { 

    Serial.begin(9600); 

    pinMode(LED_RED, OUTPUT); 
    pinMode(LED_ORANGE, OUTPUT); 
    pinMode(LED_YELLOW, OUTPUT); 
    pinMode(LED_GREEN, OUTPUT); 
    pinMode(LED_BLUE, OUTPUT); 

    pinMode(potPin, INPUT); 
} 

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

void loop() { 

    Serial.println ("Enter your chosen operator"); 
    Serial.println ("A for Addition, S for Subtraction, M for Multiplication,"); 
    Serial.println ("D for Division, and P for Potentiometer: "); 

    // Takes an input from the user 
    char theOperator = dataInput(); 

    if (theOperator == 'A') { 
    // If input is equal to 1, carry out function 'addition()' 
    addition(); 
    } 
    else if (theOperator == 'S') { 
    // If input is equal to 2, carry out function 'subtraction()' 
    subtraction(); 
    } 
    else if (theOperator == 'M') { 
    // If input is equal to 3, carry out function 'multiplication()' 
    multiplication(); 
    } 
    else if (theOperator == 'D') { 
    // If input is equal to 4, carry out function 'division()' 
    division(); 
    } 
    else if (theOperator == 'P') { 
    // If input is equal to 5, carry out function 'potentiometer()' 
    potentiometer(); 
    } 
} 

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

void addition() { 

    Serial.println ("A"); 
    Serial.println ("Please enter first number in format nnn: "); 
    firstNumber = dataInput(); // Asks the user to input the first set of numbers 

    /* Message must be in format cnnnnnn 
    therefore first number must be greater than or equal to -99 and less than or equal to 999*/ 
    if (firstNumber >= -99 && firstNumber <= 999) { 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view 
    } 
    else { 
    /* If the data input does not match the format cnnnnnn then this error message will display 
     if the input is invalid the red LED will also turn on */ 
    digitalWrite(LED_RED, HIGH); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 

    Serial.println ("Please enter second number in format nnn: "); 
    secondNumber = dataInput(); // Asks the user to input the second set of numbers 

    /* Message must be in format cnnnnnn 
    therefore second number must be greater than or equal to -99 and less than or equal to 999*/ 
    if (secondNumber >= -99 && secondNumber <= 999) { 
    // The LED will turn off if it was previously on because this is a valid input 
    digitalWrite(LED_RED, LOW); 
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view 
    } 
    else { 
    digitalWrite(LED_RED, HIGH); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 

    /* Give time for the red error LED to stay on 
    for the user to notice he/she has made an invalid input */ 
    delay(500); 
    digitalWrite(LED_RED, LOW); 

    // As this case is for addition, it will add the first and second numbers 
    value = (firstNumber + secondNumber); 

    Serial.print("Value: "); 
    // Prints the value of the two sets of numbers so that the user can see the value of their message 
    Serial.println(value); 

    /////// The 'sign' flag /////// 
    if (value >= 1) { 
    // If value is a positive number (excluding 0) turn orange LED on 
    digitalWrite(LED_ORANGE, HIGH); 
    } 
    else if (value <= -1) { 
    // If value is a negative number, turn orange LED off 
    digitalWrite(LED_ORANGE, LOW); 
    } 
    else if (value == 0) { 
    // If value is equal to 0, set orange LED at half brightness 
    int brightness; 
    // Changes the brightness of the LED so that it is half bright 
    brightness++; 
    brightness %= 128; 
    analogWrite(LED_ORANGE, brightness); 
    } 

    /////// The 'even' flag /////// 
    if ((value % 2) == 0) { 
    // If the modulo is equal to 0, it's an even 
    digitalWrite(LED_BLUE, HIGH); 
    } else { 
    // If the modulo is not equal to 0, it's an odd 
    digitalWrite(LED_BLUE, LOW); 
    } 
} 

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

void subtraction() { 

    Serial.println ("S"); 
    Serial.println ("Please enter the first number in format nnn: "); 
    firstNumber = dataInput(); 

    /* Message must be in format cnnnnnn 
    therefore first number must be greater than or equal to -99 and less than or equal to 999*/ 
    if (firstNumber >= -99 && firstNumber <= 999) { 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view 
    } 
    else { 
    /* If the data input does not match the format cnnnnnn then this error message will display 
     if the input is invalid the red LED will also turn on */ 
    digitalWrite(LED_RED, HIGH); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 

    Serial.println ("Please enter second number in format nnn: "); 
    secondNumber = dataInput(); 

    /* Message must be in format cnnnnnn 
    therefore second number must be greater than or equal to -99 and less than or equal to 999*/ 
    if (secondNumber >= -99 && secondNumber <= 999) { 
    digitalWrite(LED_RED, LOW); 
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view 
    } 
    else { 
    digitalWrite(LED_RED, HIGH); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 

    /* Give time for the red error LED to stay on 
    for the user to notice he/she has made an invalid input */ 
    delay(500); 
    digitalWrite(LED_RED, LOW); 

    // As this case is for subtraction, it will subtract the first and second numbers 
    value = (firstNumber - secondNumber); 
    Serial.print("Value: "); 
    // Prints the value of the two sets of numbers so that the user can see the value of their message 
    Serial.println(value); 

    /////// The 'sign' flag /////// 
    if (value >= 1) { 
    // If value is a positive number (excluding 0) turn orange LED on 
    digitalWrite(LED_ORANGE, HIGH); 
    } 
    else if (value <= -1) { 
    // If value is a negative number, turn orange LED off 
    digitalWrite(LED_ORANGE, LOW); 
    } 
    else if (value == 0) { 
    // If value is equal to 0, set orange LED at half brightness 
    int brightness; 
    // Changes the brightness of the LED so that it is half bright 
    brightness++; 
    brightness %= 128; 
    analogWrite(LED_ORANGE, brightness); 
    } 

    /////// The 'even' flag /////// 
    if ((value % 2) == 0) { 
    // If the modulo is equal to 0, it's an even 
    digitalWrite(LED_BLUE, HIGH); 
    } else { 
    // If the modulo is not equal to 0, it's an odd 
    digitalWrite(LED_BLUE, LOW); 
    } 
} 

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

void multiplication() { 

    Serial.println ("M"); 
    Serial.println ("Please enter the first number in format nnn: "); 
    firstNumber = dataInput(); 

    /* Message must be in format cnnnnnn 
    therefore first number must be greater than or equal to -99 and less than or equal to 999*/ 
    if (firstNumber >= -99 && firstNumber <= 999) { 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view 
    } 
    else { 
    /* If the data input does not match the format cnnnnnn then this error message will display 
     if the input is invalid the red LED will also turn on */ 
    digitalWrite(LED_RED, HIGH); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 

    Serial.println ("Please enter second number in format nnn: "); 
    secondNumber = dataInput(); 

    /* Message must be in format cnnnnnn 
    therefore second number must be greater than or equal to -99 and less than or equal to 999*/ 
    if (secondNumber >= -99 && secondNumber <= 999) { 
    digitalWrite(LED_RED, LOW); 
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view 
    } 
    else { 
    digitalWrite(LED_RED, HIGH); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 

    /* Give time for the red error LED to stay on 
    for the user to notice he/she has made an invalid input */ 
    delay(500); 
    digitalWrite(LED_RED, LOW); 

    // As this case is for multiplication, it will multiply the first and second numbers 
    value = (firstNumber * secondNumber); 
    Serial.print("Value: "); 
    // Prints the value of the two sets of numbers so that the user can see the value of their message 
    Serial.println(value); 

    /////// The 'sign' flag /////// 
    if (value >= 1) { 
    // If value is a positive number (excluding 0) turn orange LED on 
    digitalWrite(LED_ORANGE, HIGH); 
    } 
    else if (value <= -1) { 
    // If value is a negative number, turn orange LED off 
    digitalWrite(LED_ORANGE, LOW); 
    } 
    else if (value == 0) { 
    // If value is equal to 0, set orange LED at half brightness 
    int brightness; 
    // Changes the brightness of the LED so that it is half bright 
    brightness++; 
    brightness %= 128; 
    analogWrite(LED_ORANGE, brightness); 
    } 

    /////// The 'even' flag /////// 
    if ((value % 2) == 0) { 
    // If the modulo is equal to 0, it's an even 
    digitalWrite(LED_BLUE, HIGH); 
    } else { 
    // If the modulo is not equal to 0, it's an odd 
    digitalWrite(LED_BLUE, LOW); 
    } 
} 

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

void division() { 

    Serial.println ("D"); 
    Serial.println ("Please enter the first number in format nnn: "); 
    firstNumber = dataInput(); 

    /* Message must be in format cnnnnnn 
    therefore first number must be greater than or equal to -99 and less than or equal to 999 
    Division by 0 is not allowed */ 
    if (firstNumber >= -99 && firstNumber <= 999 && firstNumber != 0) { 
    Serial.println (firstNumber); // Prints the first set of numbers for the user to view 
    } 
    else if (firstNumber == 0) { 
    /* If the data input does not match the format cnnnnnn or if the input is 
     equal to 0 then this error message will display, and if the input is invalid 
     the red LED will also turn on, or if the input equals to 0 the LED will flash 
     and stay on at half brightness */ 
    blinkError(); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 
    else { 
    digitalWrite(LED_RED, HIGH); 
    } 

    Serial.println ("Please enter second number in format nnn: "); 
    secondNumber = dataInput(); 

    /* Message must be in format cnnnnnn 
    therefore second number must be greater than or equal to -99 and less than or equal to 999 
    Division by 0 is not allowed */ 
    if (secondNumber >= -99 && secondNumber <= 999 && secondNumber != 0) { 
    digitalWrite(LED_RED, LOW); 
    Serial.println (secondNumber); // Prints the second set of numbers for the user to view 
    } 
    else if (secondNumber == 0) { 
    blinkError(); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 
    else { 
    digitalWrite(LED_RED, HIGH); 
    Serial.println ("--------------- ERROR ---------------"); 
    } 

    /* Give time for the red error LED to stay on 
    for the user to notice he/she has made an invalid input */ 
    delay(500); 
    digitalWrite(LED_RED, LOW); 

    // As this case is for division, it will divide the first and second numbers 
    value = (firstNumber/secondNumber); 
    Serial.print("Value: "); 
    // Prints the value of the two sets of numbers so that the user can see the value of their message 
    Serial.println(value); 

    /////// The 'sign' flag /////// 
    if (value >= 1) { 
    // If value is a positive number (excluding 0) turn orange LED on 
    digitalWrite(LED_ORANGE, HIGH); 
    } 
    else if (value <= -1) { 
    // If value is a negative number, turn orange LED off 
    digitalWrite(LED_ORANGE, LOW); 
    } 
    else if (value == 0) { 
    // If value is equal to 0, set orange LED at half brightness 
    int brightness; 
    // Changes the brightness of the LED so that it is half bright 
    brightness++; 
    brightness %= 128; 
    analogWrite(LED_ORANGE, brightness); 
    } 

    /////// The 'even' flag /////// 
    if ((value % 2) == 0) { 
    // If the modulo is equal to 0, it's an even 
    digitalWrite(LED_BLUE, HIGH); 
    } else { 
    // If the modulo is not equal to 0, it's an odd 
    digitalWrite(LED_BLUE, LOW); 
    } 
} 

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

void potentiometer() { 

    Serial.println("P"); 

    do { 
    if (potValue >= 0 && potValue <= 6) { 
     /* If the value on the potentiometer is greater than or equal to 0, 
     or less than or equal to 6, the red LED will turn on */ 

     potValue = analogRead(potPin); // Reads the value on the pin 
     potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31 

     // Converts the DEC value of the potentiometer to BIN 
     Serial.println(potValue, BIN); 
     delay(500); 

     digitalWrite(LED_RED, HIGH); 
     digitalWrite(LED_ORANGE, LOW); 
     digitalWrite(LED_YELLOW, LOW); 
     digitalWrite(LED_GREEN, LOW); 
     digitalWrite(LED_BLUE, LOW); 
    } 
    else if (potValue >= 7 && potValue <= 12) { 
     /* If the value on the potentiometer is greater than or equal to 3, 
     or less than or equal to 12, the orange LED will turn on */ 

     potValue = analogRead(potPin); // Reads the value on the pin 
     potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31 

     Serial.println(potValue, BIN); 
     delay(500); 

     digitalWrite(LED_RED, LOW); 
     digitalWrite(LED_ORANGE, HIGH); 
     digitalWrite(LED_YELLOW, LOW); 
     digitalWrite(LED_GREEN, LOW); 
     digitalWrite(LED_BLUE, LOW); 
    } 
    else if (potValue >= 13 && potValue <= 18) { 
     /* If the value on the potentiometer is greater than or equal to 13, 
     or less than or equal to 18, the yellow LED will turn on */ 

     potValue = analogRead(potPin); // Reads the value on the pin 
     potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31 

     Serial.println(potValue, BIN); 
     delay(500); 

     digitalWrite(LED_RED, LOW); 
     digitalWrite(LED_ORANGE, LOW); 
     digitalWrite(LED_YELLOW, HIGH); 
     digitalWrite(LED_GREEN, LOW); 
     digitalWrite(LED_BLUE, LOW); 
    } 
    else if (potValue >= 19 && potValue <= 24) { 
     /* If the value on the potentiometer is greater than or equal to 19, 
     or less than or equal to 24, the green LED will turn on */ 

     potValue = analogRead(potPin); // Reads the value on the pin 
     potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31 

     Serial.println(potValue, BIN); 
     delay(500); 

     digitalWrite(LED_RED, LOW); 
     digitalWrite(LED_ORANGE, LOW); 
     digitalWrite(LED_YELLOW, LOW); 
     digitalWrite(LED_GREEN, HIGH); 
     digitalWrite(LED_BLUE, LOW); 
    } 
    else if (potValue >= 25 && potValue <= 31) { 
     /*If the value on the potentiometer is greater than or equal to 25, 
     or less than or equal to 31, the blue LED will turn on */ 

     potValue = analogRead(potPin); // Reads the value on the pin 
     potValue = map(potValue, 0, 1023, 0, 31); // Changes the default range from 0 to 1023 to 0 to 31 

     Serial.println(potValue, BIN); 
     delay(500); 

     digitalWrite(LED_RED, LOW); 
     digitalWrite(LED_ORANGE, LOW); 
     digitalWrite(LED_YELLOW, LOW); 
     digitalWrite(LED_GREEN, LOW); 
     digitalWrite(LED_BLUE, HIGH); 
    } 
    } while (true); 
} 

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

void blinkError() { 

    /////// The 'error' flag /////// 

    int brightness = 0; 

    // The for loop will loop 5 times 
    for (int x = 0; x < 5; x++) { 

    digitalWrite(LED_RED, HIGH); 
    delay(100); 
    digitalWrite(LED_RED, LOW); 
    delay(100); 

    // Changes the brightness of the LED so that it is half bright 
    brightness++; 
    brightness %= 128; 
    analogWrite(LED_RED, brightness); 
    } 
} 
+0

入力はどのように入力しますか?どのようなエラーメッセージが表示されますか? – MCG

+0

私はarduinoとarduino IDEを使用しています。シリアルモニタを開くと、A、S、M、D、Pのいずれかを入力するように求められます。現在Aと入力すると、エラーメッセージ-------- ERROR ----- - 私は各機能の中にあり、ユーザーに再度入力を要求します。 1、2、3、4、5のままにしておけば、最初の数字などを入力するようにユーザーに依頼し続けます。文字列入力をしたいのですが、何らかの理由でこれを試しています。 –

答えて

0

を取りたいです'A' parseIntは0を返します。documentationを参照してください。 charを読み取るには、Serial.read()を使用して1文字を取得します。

void loop() { 

    Serial.println ("Enter your chosen operator"); 
    Serial.println ("A for Addition, S for Subtraction, M for Multiplication,"); 
    Serial.println ("D for Division, and P for Potentiometer: "); 

    // Takes an input from the user 
    char theOperator = Serial.read(); 

    switch(theOperator) { 
    case 'A': 
    // If input is equal to 1, carry out function 'addition()' 
     addition(); 
     break; 
    case 'S': 
    // If input is equal to 2, carry out function 'subtraction()' 
     subtraction(); 
     break; 
    case 'M': 
    // If input is equal to 3, carry out function 'multiplication()' 
     multiplication(); 
     break; 
    case 'D': 
    // If input is equal to 4, carry out function 'division()' 
     division(); 
     break; 
    case 'P': 
    // If input is equal to 5, carry out function 'potentiometer()' 
    potentiometer(); 
    break; 
    } 
} 
+0

私はSerial.parseInt()をSerial.read()に変更し、theOperator = dataInput()を残しました。これは、dataInput()をループ内のSerial.read()に変更するか、グローバルに定義されているため、「選択した演算子を入力してください」というループが発生し、入力を許可しないためです。しかし、dataInput()として残しておくと、A、S、M、D、Pを入力できますが、最初の数値を入力すると、1のような奇妙な値が出力され、123は---- -ERROR -----これら2つを加算して98を出力しますか?私は非常に混乱しています。 –

+0

データ入力を変更しないでください。ループ内で読み取られた文字だけを変更してください。 – Grifplex

+0

私は私が何を変えようとしているのかよく分かりません –

関連する問題