Go Down

Topic: String type is not working! (Read 281 times) previous topic - next topic

DanielKirubakaran

Jul 20, 2019, 08:28 am Last Edit: Jul 20, 2019, 08:30 am by DanielKirubakaran
Hi,
    I am using hifive1 mcu which is an arduino compatible board. I have done many projects in this using arduino ide. I have created a new projects where it contains a "String variable", when i try to print the variable in the serial monitor It is not printing ! i dont know why, previously it was working well.Suddenly i am facing this weird issue.

The same sample code when i run in arduino uno it is working well, when it comes to my Hifive1 it doesn't print the String variable alone all the other println are printing.

Code: [Select]
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("\n\nString charAt() and setCharAt():");
}

void loop() {
  // make a String to report a sensor reading:
  String reportString = "SensorReading: 456";
  Serial.println(reportString);

  // the reading's most significant digit is at position 15 in the reportString:
  char mostSignificantDigit = reportString.charAt(15);

  String message = "Most significant digit of the sensor reading is: ";
  Serial.println(message + mostSignificantDigit);

  // add blank space:
  Serial.println();

  // you can also set the character of a String. Change the : to a = character
  reportString.setCharAt(13, '=');
  Serial.println(reportString);

  // do nothing while true:
  while (true);
}









O/P

String charAt() and setCharAt():






Kindly help me out.
Thank you.

Robin2

O/P

String charAt() and setCharAt():
It is doing exactly what you are telling it to do - look carefully at your code.

As you have only posted a snippet I have no idea what you are hoping it would do. Post a complete program that illustrates the problem.


Separately, and not directly related to your problem, it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DanielKirubakaran

#2
Jul 20, 2019, 09:04 am Last Edit: Jul 20, 2019, 09:05 am by DanielKirubakaran
Actually i have given the complete code. Scroll down in the code box to see the loop().
The issue is it is not printing the Serial.println()'s which is in the loop().

wildbill

That board seems to have plenty of RAM, so I don't think that use of Strings (bad idea though it is) can be the issue.

That it works on an Arduino (it certainly does on my Uno) suggests that the problem lies with the hifive1 or more likely, the toolchain you're using to program it.

Robin2

Actually i have given the complete code.
When I did not see any global variables defined before setup() I had assumed (incorrectly) that it was only a snippet - apologies.

Your program seems to work fine on my Uno.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

johnwasser

I suspect there is a problem in the run-time code provided for the Freedom E310 processor of your HiFive1 board.  I'd try putting the code into setup() and leaving loop() empty in case there is a problem caused by the while(1);
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

DanielKirubakaran

Still not working when leaving the loop() empty.
It only prints the "String charAt() and setCharAt():"

Also tried to print,

char *hello="hello";
Serial.println(hello);

it prints the char*. But why not String type?
is reinstalling arduino ide a good choice?

DanielKirubakaran

These are the tests that i have done in the morning in different systems.

I)Windows10(currently i am using)
Arduino version 1.8.9

output:

String charAt() and setCharAt():

II)Linux(ubuntu 18.04)
Arduino Version 1.8.8

output:
        String charAt() and setCharAt():
        SensorReading: 456
        Most significant digit of the sensor reading is: 4
        SensorReading= 456

III)Windows7 (colleague's system)
Arduino Version 1.8.8

output:

String charAt() and setCharAt():

I guess, if i reinstall the ide in my system again , the same issue will occur.,

Robin2

I was using Linux Mint when I tried your program on my Uno (Reply #4)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DanielKirubakaran

I was using Linux Mint when I tried your program on my Uno (Reply #4)

...R
It is working for me also in my UNO board
But not working in Hifive1 board , especially those string data types

Robin2

It is working for me also in my UNO board
But not working in Hifive1 board , especially those string data types
Did you have to load a special "core" to use the Hifive1 board, and if so, who wrote it? Maybe that is where you should be seeking an answer?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DanielKirubakaran

Yes , that is "Sifive".
I have put this query in their forum as well. Assumed that the people who worked with the board which i am using may also there in this forum as the board is an Arduino compatible one.

Robin2

Yes , that is "Sifive".
I'm confused. You have called it "hifive1" in your earlier Posts.

A link to the website would be a useful.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DanielKirubakaran

#13
Jul 22, 2019, 12:39 pm Last Edit: Jul 22, 2019, 12:41 pm by DanielKirubakaran
It is Sifive's hifive1.As you asked about the authors i told about Sifive's team.
please check it out here.

https://www.sifive.com/boards/hifive1

Thanks for your patience.

Robin2

Thanks.

My guess is that they have not properly implemented the interface with the Arduino IDE.

Have you tried modifying your code so it does not use the String class, instead uses cstrings - char arrays terminated with 0.


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up