Hello, I have a problem. I programmed some code to send text with an LED in long and short flashes. Everything worked, but today i tried to use the Serial.read function to send a string from the serial monitor. However the LED doesn't flash at all right now
The old code, which works:
void StringToByteArray (byte array[], String string)
{
byte stringLength = string.length();
string.getBytes(array, stringLength+1);
}
byte transmit = 12;
byte mask = 1;
byte bitDelay = 5;
void setup()
{
Serial.begin(9600);
pinMode(transmit,OUTPUT);
Serial.println("typ your text here: /n");
String testString = F("test ABC");
byte stringL = testString.length();
byte byteArray [stringL];
StringToByteArray(byteArray, testString);
printArray(byteArray, stringL);
}
void printArray (byte array[], byte strLength)
{
for (byte i = 0; i < strLength; i++){
Serial.println(array[i]);
for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
if (array[i] & mask){ // if bitwise AND resolves to true
digitalWrite(transmit,HIGH); // send 1
delay(8);
}
else{ //if bitwise and resolves to false
digitalWrite(transmit,HIGH); // send 0
delay(4);
}
digitalWrite(transmit, LOW);
delay(bitDelay); //delay
}
delay(10);
}
}
void loop(){}
The new code:
void StringToByteArray (byte array[], String string)
{
byte stringLength = string.length();
string.getBytes(array, stringLength+1);
}
byte transmit = 12;
byte mask = 1;
byte bitDelay = 5;
void setup()
{
Serial.begin(9600);
pinMode(transmit,OUTPUT);
Serial.println("typ your text here: \n");
if(Serial.available() > 0){
String testString = Serial.readString();
byte stringL = testString.length();
byte byteArray [stringL];
StringToByteArray(byteArray, testString);
printArray(byteArray, stringL);
}
}
void printArray (byte array[], byte strLength)
{
for (byte i = 0; i < strLength; i++){
Serial.println(array[i]);
for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
if (array[i] & mask){ // if bitwise AND resolves to true
digitalWrite(transmit,HIGH); // send 1
delay(8);
}
else{ //if bitwise and resolves to false
digitalWrite(transmit,HIGH); // send 0
delay(4);
}
digitalWrite(transmit, LOW);
delay(bitDelay); //delay
}
delay(10);
}
}
void loop(){}
I really don't understand what the problem is, anyone advice?
In the old code, you set the test string to "test ABC".
In the new code, you expect the user to enter the test string very quickly between the execution of the following two statements:
Serial.println("typ your text here: \n");
.......
if(Serial.available() > 0){
You know setup is only run once right (major hint!)
And some extra advice:
Drop the String class. It's big, stupid and slow. Try reading on C strings.
Be carefull with adding leading 0's to a number! With a leading 0 it is treated as a octal number. If you want to make a bit mask and make it more clear, use the binary notation by leading it with 0b.
Also, press ctrl+T sometimes The alignment of the brackets in your code it not very good aka very confusing to read.
Also, define a variable where you need it. the mask variable can be defined inside the loop, like you do with i
If i do it in a loop, the program will keep sending the string, but it just has to be sent one time. And if the other functions, below the serial.read, stay in the setup they too will be executed just one time...
I understand your changes in the code, I have been busy with it for an hour now. The code now works, but just once. If the entered string is sent, it won't send a second string
code:
void StringToByteArray (byte array[], String string)
{
byte stringLength = string.length();
string.getBytes(array, stringLength + 1);
}
byte transmit = 12;
byte mask = 1;
byte bitDelay = 5;
void setup()
{
Serial.begin(9600);
pinMode(transmit, OUTPUT);
Serial.println("type your text here: \n");
}
void loop() {
String testString = "test";
if (Serial.available() > 0) {
String testString = Serial.readString();
byte stringL = testString.length();
byte byteArray [stringL];
StringToByteArray(byteArray, testString);
printArray(byteArray, stringL);
}
}
void printArray (byte array[], byte strLength)
{
for (byte i = 0; i < strLength; i++) {
Serial.println(array[i]);
for (mask = 00000001; mask > 0; mask <<= 1) { //iterate through bit mask
if (array[i] & mask) { // if bitwise AND resolves to true
digitalWrite(transmit, HIGH); // send 1
delay(8);
}
else { //if bitwise and resolves to false
digitalWrite(transmit, HIGH); // send 0
delay(4);
}
digitalWrite(transmit, LOW);
delay(bitDelay); //delay
}
delay(10);
}
}
I know it could be programmed much more efficient, but because it is a school project I want to do it my own way (I had no programming experience before I programmed this)
void loop() {
// String testString = "test";
Serial.println("type your text here: \n");
while(Serial.available() == 0){ } ; // wait for user to enter something
if (Serial.available() > 0) {
String testString = Serial.readString();
byte stringL = testString.length();
byte byteArray [stringL];
StringToByteArray(byteArray, testString);
printArray(byteArray, stringL);
}
}
You have to now solve the problem of how you end the whole thing if the user does not want enter any more text.
You coild also use the "autoformat" option under the "tools" menu to improve the layout of your code.
henkbert:
I know it could be programmed much more efficient, but because it is a school project I want to do it my own way (I had no programming experience before I programmed this)
With that said I'd like to make you aware the you can ask any String object instance to return the address of its internal C/C++ zero-terminated-string by sending in the message '.c_str()'. Using said string would allow for the removal of the 'StringToByteArray' function and instead send the address to a rewritten 'printArray' function.
The problem now is, that the first time you enter a sentence in the serial monitor, it will be send, but if that sentence is sent and you enter another it won't send it. However if the second sentence is entered while the first sentence gets send it will be send to.
@lloyddean I know about that, I discovered that when I had almost finished my code. I doubt about rewriting the code, but chose not to do that. The teacher that will give me my grade doesn't know much about programming, so there is a good chance that a longer code will get me a higher grade haha.
If it is just possible to send a second sentence I am happy