I was messing around with small basic (the easier intro version of vb) and noticed and extension that allow serial communication! Now i want to send number to the arduino via serial port.
the code for small basic is simple:
LDCommPort.AvailablePorts ()
LDCommPort.OpenPort(3,9600) // set up communication
LDCommPort.TXString(1234) //number to send
Now im stumped on how to program the arduino.
I tried a simple program such as:
int X = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
X = Serial.read();
Serial.println( X);
delay(1000);
}
}
but instead of a an output of "1234" it splits it up as 1 then 2 then 3 then 4. How can I receive this as a single integer?
Save the characters in an array that you keep NULL terminated. When the last character has been received, call atoi() to convert the string to an int.
Of course, you'll see that the tricky part is knowing when the last character has been received.
There are any number of ways to determine that. Since the student failed to do any research before posting, the ways are left as an exercise for the student.
Look for another function that writes bytes, instead of something that clearly says "string" in it. This function is sending a string one character at a time, not numbers.
Look for another function that writes bytes, instead of something that clearly says "string" in it. This function is sending a string one character at a time, not numbers.
There is an option to send bytes at a time. But I actually don't understand the difference
Start the IDE, go to FILE, SKETCHBOOK, then EXAMPLES. Then look in 04.COMMUNICATION for an example called ReadASCIIString.
Start the IDE, go to FILE, SKETCHBOOK, then EXAMPLES. Then look in 04.COMMUNICATION for an example called ReadASCIIString.
There you go.
I just tried it but it doesnt seem to work. I sent 3 values like : 32,255,123, to the serial port but i dont receive a hex value like the program should send.
Start the IDE, go to FILE, SKETCHBOOK, then EXAMPLES. Then look in 04.COMMUNICATION for an example called ReadASCIIString.
There you go.
I just tried it but it doesnt seem to work. I sent 3 values like : 32,255,123, to the serial port but i dont receive a hex value like the program should send.
Computers only know binary. If you want them to convert something and display it as decimal or hexadecimal for your benefit then they will gladly do that. But the information is always sent in binary. Arduino only gets binary from the PC. Arduino has no way of knowing whether you coded that number on the PC side using decimal or hexadecimal or binary or what.
void setup() {
Serial.begin(9600); // set serial speed
}
void loop(){
if (Serial.available() > 0){
int val = Serial.read() ;
Serial.print(val);
}
else{
}
}
Your PC program sent 0x02. Which was stored as an integer on the Arduino. The Arduino then printed the number 2 in ASCII (that's what Serial.print does), which is ASCII 50. Serial.write(val) would send 0x02.
Yes thats the way i do it. Ive never studied programming in my life..till now. All i know is from experiments, i wrote a program that generates codes to drive motors, but i cant send the commands to the arduino via serial, thus this post.
Some simple servo test code you can try using the serial monitor (captures serial character string, parses captured string into two new character strings, converts new strings into numbers, ...). You might modify this code to verify you can make the motors move via serial commands.
// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(3); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see parsed results
Serial.println(servo2);
int n1 = servo1.toInt();
int n2 = servo2.toInt();
Serial.println("the numbers are :");
Serial.println(n1); //print to serial monitor to see number results
Serial.println(n2);
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}
}