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
Best Answer
toLowerCase()
function converts the object being called on to lower case. It doesn't return anything. So what you probably want to do is: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: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:
You expect
message
to be a String, as you call the length method. Now, the compiler attempts to convert the void inmessage
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: