Sign up ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free.

I am building a type of telegraph with my Arduino and instead of using Morse code I am taking each character and giving it a value of 1-26 (a-z), 27 for a space and 28 for unknown. Then I convert that number to binary. But I keep getting errors. Could someone please help me? Thanks!

int button_0 = 13;
int button_1 = 12;

int out_0 = 11;
int out_1 = 10;

int in_0 = 9;
int in_1 = 8;

int buzzer = 7;

void setup() {

  pinMode(button_0, INPUT);
  pinMode(button_1, INPUT);

  pinMode(out_0, OUTPUT);
  pinMode(out_1, OUTPUT);

  pinMode(in_0, OUTPUT);
  pinMode(in_1, OUTPUT);

  pinMode(buzzer, OUTPUT);

  Serial.begin(9600);

}


void loop() {

  Serial.println("Message to binary: ");
  String input = "Hello World";
  String message = input.toLowerCase();
  String binaryNumber = "";

  for(int i = 0; i <= message.length(); i++) {

    char l = message.charAt(i);
    int letter = 0;

    if (l == 'a') {

      letter = 1;

    } 
    else if (l == 'b') {

      letter = 2;

    } 
    else if (l == 'c') {

      letter = 3;

    } 
    else if (l == 'd') {

      letter = 4;

    } 
    else if (l == 'e') {

      letter = 5;

    } 
    else if (l == 'f') {

      letter = 6;

    } 
    else if (l == 'g') {

      letter = 7;

    } 
    else if (l == 'h') {

      letter = 8;

    } 
    else if (l == 'i') {

      letter = 9;

    } 
    else if (l == 'j') {

      letter = 10;

    } 
    else if (l == 'k') {

      letter = 11;

    } 
    else if (l == 'l') {

      letter = 12;

    } 
    else if (l == 'o') {

      letter = 13;

    } 
    else if (l == 'm') {

      letter = 14;

    } 
    else if (l == 'n') {

      letter = 15;

    } 
    else if (l == 'p') {

      letter = 16;

    } 
    else if (l == 'q') {

      letter = 17;

    } 
    else if (l == 'r') {

      letter = 18;

    } 
    else if (l == 's') {

      letter = 19;

    } 
    else if (l == 't') {

      letter = 20;

    } 
    else if (l == 'u') {

      letter = 21;

    } 
    else if (l == 'v') {

      letter = 22;

    } 
    else if (l == 'w') {

      letter = 23;

    }

    else if (l == 'x') {

      letter = 24;

    } 
    else if (l == 'y') {

      letter = 25;

    } 
    else if (l == 'z') {

      letter = 26;

    } 
    else if (l == ' ') {

      letter = 27;

    } 
    else {

      letter = 28;

    }

    binaryNumber = binaryNumber + String(letter, BIN) + " ";

  }

  Serial.println("Message to binary:\n"+binaryNumber);

}

Here is my error:

binary_telegraph.cpp: In function ‘void loop()’:
binary_telegraph.cpp:37:38: error: conversion from ‘void’ to non-scalar type ‘String’ requested
share|improve this question
1  
What kind of errors are you getting? –  angelatlarge Mar 31 '13 at 17:36
1  
Yo, that code is not very neat. To get a=1, b=2, etc., use math: arduino.cc/en/Reference/Char. So, if (l>96 && l<123) { letter = l-96 } (see the ASCII table). By the way, why do you use an int for the variable letter? As you won't get above 28, a char would be enough too. –  Camil Staps Mar 31 '13 at 17:41
    
@Camil Staps - Thanks for the recommendation! –  user1978786 Mar 31 '13 at 17:55
    
That's okay. Good luck with your project! –  Camil Staps Mar 31 '13 at 17:55

1 Answer 1

up vote 1 down vote accepted

toLowerCase() function converts the object being called on to lower case. It doesn't return anything. So what you probably want to do is:

String input = "Hello World";
String message = input;
message.toLowerCase();

This explains the error: you put a void, a nothing, in the variable message. This is not an unreasonable error to make, according to the documentation:

Get a lower-case version of a String. As of 1.0, toLowerCase() modifies the string in place rather than returning a new one.

So the toLowerCase() function used to return a lower-cased version of the string. Now it just modifies the string being called on, and not return anything.

Now, at line 37:

for(int i = 0; i <= message.length(); i++) {

You expect message to be a String, as you call the length method. Now, the compiler attempts to convert the void in message to a String, but that's not possible. Therefore, you get an error.

P.S. As a general rule, here's my personal process for debugging these sorts of errors:

  • Look at the line where the error occurs
  • Check the documentation for all the functions used on that line (in this case here)
  • If that doesn't solve it, double-check my assumptions about the language syntax based on the error message.
share|improve this answer
    
It works!!! Thank you! –  user1978786 Mar 31 '13 at 17:54
    
@Camil Staps: Oh, right, the moralistic, patronizing bit should probably be attributed to me, rather than the whole community :) –  angelatlarge Mar 31 '13 at 17:54
    
@angelatlarge hey, you get all the credits! No, seriously: these are good rules, but as this is a CW, you cannot just say 'my' ;) –  Camil Staps Mar 31 '13 at 17:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.