Instead of using Arduino Mega 2560, you should get value from your sensor using Software Serial. Mega is costly plus bigger in size. Software Serial in Arduino is not that difficult, you will find example code in Arduino IDE.
jackthom41:
Instead of using Arduino Mega 2560, you should get value from your sensor using Software Serial. Mega is costly plus bigger in size. Software Serial in Arduino is not that difficult, you will find example code in Arduino IDE.
That depends on baudrate, number of databaits and stopbits and parity. SoftwareSerial does not perform well (to my understanding) at higher baudrates and does only support 8N1.
if (Serial.available() > 0)
{
Serial.println("C1");
while(Serial.available()>0)
{
ch = Serial.read(); //loop till character received
if(ch==0x0A) // if received character is , 0x0A, 10 then process buffer
{
pos = 0; // buffer position reset for next reading
newData=true;
// extract data from serial buffer to 8 bit integer value
// convert data from ASCII to decimal
read1 = ((sbuffer[1]-'0')*100) + ((sbuffer[2]-'0')*10) +(sbuffer[3]-'0');
read2 = ((sbuffer[6]-'0')*100) + ((sbuffer[7]-'0')*10) +(sbuffer[8]-'0');
read3 = ((sbuffer[11]-'0')*100) + ((sbuffer[12]-'0')*10) +(sbuffer[13]-'0');
Serial.println("C3");
}
else
{ //store serial data to buffer
sbuffer[pos] = ch;
pos++;
}
}
}
showNewData();
}
Please read How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum, specifically point #7 how to post code. Next modify reply #5 and add the code tags.
2)
You're using a mega; there is no need to use SoftwareSerial on a hardware serial port (pins 18 and 19 are Serial1).
3)
Do not differentiate variables based on capitalisation (Serial vs serial).
==============
Your code reads data from the serial port that is connected to the PC; do you actually send something? What is your line-ending set to in serial monitor; it needs to be newline or carriagereturn plus newline.
Assigning values to readX should actually be done in showNewData and not in the serial communications part of the code.
sterretje:
1)
Please read How to use this forum - please read - Website and Forum - Arduino Forum, specifically point #7 how to post code. Next modify reply #5 and add the code tags.
2)
You're using a mega; there is no need to use SoftwareSerial on a hardware serial port (pins 18 and 19 are Serial1).
3)
Do not differentiate variables based on capitalisation (Serial vs serial).
==============
Your code reads data from the serial port that is connected to the PC; do you actually send something? What is your line-ending set to in serial monitor; it needs to be newline or carriagereturn plus newline.
Assigning values to readX should actually be done in showNewData and not in the serial communications part of the code.
i'm reading the data & using that data to do something(which i cannot tell),
i didnot understand this line "Assigning values to readX should actually be done in showNewData and not in the serial communications part of the code."
can you please send me the code what exactly u mean?
kammatisai:
i didnot understand this line "Assigning values to readX should actually be done in showNewData and not in the serial communications part of the code."
You should have two (or more) layers. The function of the first layer is solely to read data from the serial port. The function of the second layer is to parse the received data which e.g. includes the conversion from ascii to numbers so it's ready for further processing. There should be a third layer that does the actual processing of the parsed data; if so inclined, you can do that in the second layer.
kammatisai:
i'm reading the data & using that data to do something**(which i cannot tell)**,