I'm new here, so I'm sorry if ths is the wrong area for my topic. I started a new project using a Arduino Mega 2560 but now I'm at a problem I don't now how to deal with.
I use a MaxBotix MB1033 ultrasonic sensor to measure distances. This sensor has a analog-, a pulse- and a serial output. The serial is the most accurate and so I want to use this one. I set up a 2nd serial port on my Arduino by using
Serial1.begin(9600);
and read the Output of the sensor by using
Serial1.read();
I thought this wold be enough to run the sensor, but it wasn't so. I need a serial port with 9600baud, 8 bits, 1 stop bit and no parity. Can anybody here tell me what configuration the Serial1 has or how to change this?
So I tried it using Serial1.availible and it runs! It shows me the right data. My next problem is how to convert the ascii date out of the serial input into normal integers, so as if I use Serial.write but into a integer variable. Have I to write a function to convert ascii or is this still availible?
My next problem is how to convert the ascii date out of the serial input into normal integers, so as if I use Serial.write but into a integer variable. Have I to write a function to convert ascii or is this still availible?
The atoi() function comes to mind. We'd need to see how you are currently handling the serial input.
VerirrtesSchaf:
So I tried it using Serial1.availible and it runs! It shows me the right data. My next problem is how to convert the ascii date out of the serial input into normal integers, so as if I use Serial.write but into a integer variable.
If the values exceed 255 you will need to batch them up into multiple bytes. This might help:
Well I wrote me a own function to convert the ASCII data to integer, because I only need 12 characters. But now I have another serious problem. I tried now one day to get my serial data so how I want. The sensor I read shall be part of a system that monitors temperatures and a fill level. So I get the data of the sensors etc in different time differneces, so I want to ask the level sensor also e.g. only all 10 seconds of its data. The whole system runs only this sensor not.
How can I get the 5 Bytes it sends to the Arduino in one step before the program gets to the next step? The data should also be calculated in a special way in this time, but when I take a for loop around the serial part I get the data tree times.
digital Pin 21 is only used to activate/deactivate sensor.
for(i=0; i<6; i++) {
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
if(inByte==13) { digitalWrite(21, LOW); }
}
}
How fast do you think it will run through this loop if there are less than 6 bytes to read? Why is the loop iteration count set to 6? If it is because that is the number of bytes you need to wait for and read, you need to add the wait for part.
while(Serial1.available() == 0) { // Do nothing } at the top of the for loop, and get rid of the if statement.