how to send input values from host computer to arduino,such that speficing the number of rotations for a stepper motor etc
1 Answer
The easiest method is to use the serial communication. Use the sample code at http://arduino.cc/en/Tutorial/SwitchCase2, upload that to an Arduino, start the serial monitor (Ctrl+Shift+M), make sure the baud rate is the same as set in the code (9600 in this case), then write the commands and send them to the Arduino.
Edit
Since the serial characters are usually read one by one you need a character that will signal the end of the string. This is usually a newline \n
, but can be any character you choose (and can be sent to Arduino).
Sample code (processString
would be your function to do something with the string):
char buf[64];
byte cnt = 0;
void loop()
{
while (Serial.available()>0) {
byte c = Serial.read();
// Check for terminator character or the size of buffer
if (c == '\n' || cnt >= 63) {
// Add the terminating 0
buf[cnt] = 0;
// Process the full string
processString(buf);
// Prepare to read the next string
cnt = 0;
} else {
// Not terminator - add to string
buf[cnt++] = c;
}
}
}
-
okay.If i want to sent a string the same read() is used? Is there any modifications needed for sending strings? Apr 16, 2015 at 3:40
-