serial string donot clear properley

Hi ,

I am using arduino micro board compiler version 1.6.3 , I am reading a string like as follows

// sample code

if(Serial.available())
{

char inChar = Serial.read();
inputstring += inChar;

if(inChar == '*') // * end of string
{

newstring = inputstring;
inputstring = " ";
newstring = " ";
}

}
Even i tried to clear inputstring it is not clearing. for first time after uplloading it is working well. Afterwards it is not clearing. but the same code worked in other system successfully. i reinstalled my arduino in my system, but sam issue continues,

i think there is something problem with library file. can anybody help me to get rid of this problem??

Thanks in Advancxe

Arul

arulchozhan:
Hi ,

I am using arduino micro board compiler version 1.6.3 , I am reading a string like as follows

// sample code

if(Serial.available())
{

char inChar = Serial.read();
inputstring += inChar;

if(inChar == '*') // * end of string
{

newstring = inputstring;
inputstring = " ";
newstring = " ";
}

}



Even i tried to clear inputstring it is not clearing. for first time after uplloading it is working well. Afterwards it is not clearing. but the same code worked in other system successfully. i reinstalled my arduino in my system, but sam issue continues,


i think there is something problem with library file. can anybody help me to get rid of this problem??


Thanks in Advancxe

Arul

It works fine for me

void setup(){
Serial.begin(9600);
String S;
char a ='*';
S += a;
Serial.print("the Content of S |");
Serial.print(S);
Serial.print("| length=");
Serial.println(S.length(),DEC);
S = "";
Serial.print("the Content of S |");
Serial.print(S);
Serial.print("| length=");
Serial.println(S.length(),DEC);
}

void loop(){}

Here is what the output is

the Content of S |*| length=1
the Content of S || length=0

Chuck.

hi sir, thans to reply
in that code if am using hyper terminal window instad of arduino serial window am get the correct output(that means the string is cleared evry time). what is problem in arduino serial plese help me

what is problem in arduino serial plese help me

The problem is in the code that you did not post.

arulchozhan:
hi sir, thans to reply
in that code if am using hyper terminal window instad of arduino serial window am get the correct output(that means the string is cleared evry time). what is problem in arduino serial plese help me

To enable us to help you:

  • post the actual code that fails
  • post an explanation of what you expected your code to do
  • Post what your code actually did, a screen capture.

Just saying it don't work, does not tell us what did not work. Your original post stated that a String Object would not clear. I interpreted that to mean you could not change the content of a String Object to a NULL string. That is what my code/output example answered.

The better you make your problem explanation, the better we can explain our solution for your problem.

Chuck.

#include <Wire.h>
#include <ivafruit_ADS1015.h>
#include <Adafruit_ADS1015.h>
#include <SoftwareSerial.h>
#include<string.h>
#include<stdio.h>
Adafruit_ADS1115 ads1115 (0x49);
ivafruit_ADS2224 ads2224 (0x48);
String inputString = "";
String newstring="";
#define disk1 0X10//Address of dac chip
#define relayadd 0X41 //Address of dac chip
int integerValue=0;
unsigned int outValue=0xff; //intial off state for relay
//////////
const int buttonPin0 =A0;
const int buttonPin1 =A1; // the number of the pushbutton pin
int buttonState0, buttonState1 =HIGH;
const int EN =9;
const int a0 =10;
const int a1 =11;
////////////////
void dac(int deviceaddress, unsigned int eeaddress,int data )
{

Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress));
Wire.write((int)(data >> 4));
Wire.write((int)(data << 4));
Wire.endTransmission();
}
void relay(int address, unsigned int count,int out )
{

Wire.beginTransmission(address);
Wire.write((int)(count));
Wire.write((int)( out ));
Wire.endTransmission();
}

void setup(void)
{
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(EN, OUTPUT);
pinMode(a0, OUTPUT);
pinMode(a1, OUTPUT);
Serial.begin(9600);
Wire.begin();
String inputString = "";
ads1115.begin();
ads2224.begin();
Serial.println("WELCOME");
//ads1115.setGain(GAIN_SIXTEEN);
}

void loop(){
unsigned int address = 0X30; // this for relay address
int16_t adc0;
int16_t outputValue;
int16_t iadc0, iadc1;
float ivolt0, ivolt1,volt0;
long int volt1;
unsigned int cont = 0X03;// this relay control select bit
buttonState0 = digitalRead(buttonPin0);
buttonState1 = digitalRead(buttonPin1);

if (buttonState0 == LOW) {
//DO SOME
}
if (buttonState1 ==LOW) {
//DO SOME
}
if(Serial.available()>0) {
char inChar = Serial.read(); // char
inputString += inChar;
if (inChar == '*') {
if(inputString[0]=='Y')
{
outValue=0xf0;

}
if(inputString[0]=='N')
{
outValue=0xff;

}

//Serial.println( inputString);
if(inputString[0]=='I')
{
int i;
int len=inputString.length();
for(i=0;i<len;i++)
{
if (isDigit(inputString*))*

  • {*
    _ char m=inputString*;_
    _
    newstring += m;_
    _
    }_
    _
    }_
    _
    integerValue= newstring.toInt();*_
    _ integerValue= (integerValue)*40; //to convert the user value into arround 4000 range_

* }*
* if(inputString[0]=='L')*
* {*
* int k=26;*
* Serial.println('k');*
* }*
* if(inputString=="S1")*
* {*
* digitalWrite(EN,HIGH);*
* digitalWrite(a0,LOW);*
* digitalWrite(a1,LOW);*
* }*
* else if(inputString=="S2")*
* {*
* digitalWrite(EN,HIGH);*
* digitalWrite(a0,HIGH);*
* digitalWrite(a1,LOW);*
* }*
* else if(inputString=="S3")*
* {*
* digitalWrite(EN,HIGH);*
* digitalWrite(a0,LOW);*
* digitalWrite(a1,HIGH);*
* }*
* else if(inputString=="S4")*
* {*
* digitalWrite(EN,HIGH);*
* digitalWrite(a0,HIGH);*
* digitalWrite(a1,HIGH);*
* }*
* else*
* {*
* digitalWrite(EN,LOW);*
* digitalWrite(a0,LOW);*
* digitalWrite(a1,LOW);*
* } inputString = "";*
* newstring=" " ;*
* }*
* Serial.println(integerValue);*
* }*
* Serial.println(integerValue);*
* dac(disk1, address, integerValue);*
* relay(relayadd, cont, outValue);*
* adc0 = ads1115.readADC_SingleEnded(0);
iadc0 = ads2224.readADC_SingleEnded1(0);
_ volt0 = adc00.000188;
* volt0 = adc0/4926.470588;*
* ivolt0 = iadc0/4926.470588;*
//volt1=volt0*100;_

* //Serial.print(adc0);*
* //Serial.print(",");*
* //Serial.print("RCV");*
* //Serial.println(volt0,3);*
* //Serial.println(ivolt0,3);*
* //delay(1000);*
*} *
///////////////////////////////////////////
THIS IS MY TOTAL CODE.
the output is given heare
the intialy print 0 continuesly.
if i give I10*(enter the send tab) that time print 400 is work prefect
after i give any value not change it that means continusley print same 400. when i again dump the code it print intial value 0 and i entered some value in send tab that vale printed.but i give second time the value do not printed this is my problem can u help me

THIS IS MY TOTAL CODE.

AND IT IS CRAP. AND IT IS NOT POSTED CORRECTLY.

There are sticky posts at the top of the forum that describe how to post code here, using code tags (generated by the </> icon on the left end of the first row).

Before you post that mess again, though, use Tools + Auto Format to do some proper indenting.

             }      inputString = "";
                     newstring=" " ;

Find ONE other example where ANY statement follows the } of another statement on the same line. THAT IS WRONG!

Setting inputString, some place, to an empty string, and setting newstring (what happened to the camelCase naming convention?) to a space are NOT doing the same thing.

thank u r commands. i was done the correction but still am get same problem. can u give more idea to sole the issue

i was done the correction but still am get same problem.

I changed my code. I'm not going to show it to you. It still doesn't work. Can you help?

Did I paraphrase you correctly?

If so, no.