I am doing a project for myself. I have an arduino nano on one side and a mega 2560 on the other.. I have been trying to pass numerical data from the nano to the mega but encountered some problems. I am sending good data but have trouble on the receiving part since the numbers come up with acsii and when doing the conversion the numbers split up. Here the code for the receiving part. Please note that i need the numerical data for matlab use so by doing Serail.write it won't work. Could someone help please?
Please don't paste an image with your code. Instead, paste the actual code and wrap it with tags.
mikispiki:
when doing the conversion the numbers split up.
So you need to add a "delimiter" to know when you've finished sending numbers. Or always send the same number of digits.
Example:
Your send code could send:
32
then
\0
On the receive side, keep shift your number until the \0 is received. So then keep combining the incoming ascii digits until the delimiter (multiply by 10 each time).
Sorry to bother you.. but in Matlab I can't input ',' .... I need ex int a to be equal to the number I am reciving .. ie int a = 124 or int a = 3456 so that I could input int a into matlab.. but with this method i am using the number are being split up.
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
if( Serial1.available())
{
int ch = Serial1.read();
if (isDigit(ch)) // is this an ascii digit between 0 and 9 ?
{
int blink1 = (ch - '0');
Serial.println(blink1);
// Serial.print(',');
}
}
}
Sorry to bother you.. but in Matlab I can't input ',' .... I need ex int a to be equal to the number I am reciving .. ie int a = 124 or int a = 3456 so that I could input int a into matlab.. but with this method i am using the number are being split up.
It appears you have a Matlab issue that a Matlab forum might help with. With serial communications data is usually sent as eight bit bytes. A single byte usually has a numerical value of 0-255. If you need numerical values greater than 255, then you will need to develop matlab code that can interpet two or more bytes sent to matlab as the numerical value being sent. Fairly easy for arduino to arduino. May be a different story with matlab.
no no.. its an arduino issue ... I have already communicated with matlab without the serial...
The problem is that the values are being displayed one by one.. for example if 456 is sent... I am seeing
4
5
6
not 456..
Bummer ?? I kept trying to do what was suggested but didn't achieve any results. The problem was that for no idea why int wasn't working and when changing to float the code worked perfectly. Here's my attempt. Thanks to all of those who tried to help.
void loop()
{
if( Serial.available())
{
float ch = Serial.read();
if( isDigit(ch) )// is this an ascii digit between 0 and 9?
{
float value = (value * 10) + (ch - '0'); // yes, accumulate the value
}
else if (ch == 10) // is the character the newline character?
{
float blinkRate = value; // set blinkrate to the accumulated value
Serial.println(blinkRate);
value = 0; // reset val to 0 ready for the next sequence of digits
}
}
}
The problem was I wanted a variable which would be equal to the number I am receiving serially s0 that I would input it into Matlab. Doesn't matter which sort of print() I use. I was using it only to confirm that I am receiving good values. The problem I had was that the values were being separated because in only a value at a time can be sent serially.
Y would it be a problem that I am using a float? Its the only way that the above code worked, if I set the variables to int, I only receive rubbish data and to and to be honest I can figure why.
Below is a basic way to send a numeric value thru the serial port. Open the serial monitor, type in the number followed by a comma, then send it to the arduino. The arduino will capture the individual characters, convert them to a number for servo control and also send the number back to the serial monitor.
//zoomkat 3-5-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like 700, or 1500, or 2000,
//or like 30, or 90, or 180,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
//do stuff with the captured readString
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}