You are on page 1of 9

Menu

Home/Tutorials/How to Control Arduino through the Serial Monitor

Posted on October 18, 2011

How to Control Arduino


through the Serial Monitor

Written by
Dan
Posted in
Tutorials
Tagged
arduino, monitor, serial, serial monitor
Comments
13 Comments
When Im developing Arduino code, I often have variables that need to be tweaked

during testing & calibration.


Rather than recompile and upload the code (a time consuming process) I wrote a
few lines to let me open the Serial Monitor and send commands directly.
I hope you nd this useful I use it in all my robots!

#defineDELAY(5)
#defineBAUD(57600)
#defineMAX_BUF(64)

charbuffer[MAX_BUF]
intsofar

chartest_on=0//onlyusedinprocessCommandasanexample.
floatpos_x

floatpos_y

Search Products

floatpos_z

voidjog(floatx,floaty,floatz){
//dosometestsheretovalidatex,y,andz.
pos_x=x
pos_y=y
pos_z=z
}

voidprocessCommand(){
if(!strncmp(buffer,"help",4)){

Serial.println("commands:wheretest(1/0)jog[x(float)]
}elseif(!strncmp(buffer,"where",5)){
//noparameters
Serial.print(pos_x)

Serial.print(",")
Serial.print(pos_y)
Serial.print(",")
Serial.println(pos_z)
}elseif(!strncmp(buffer,"test",4)){
//onewholenumberparameter
char*state=strchr(buffer,'')+1
Serial.println(state)
if(state[0]=='0'){
Serial.println("Endtest")
test_on=0
}else{
Serial.println("Starttest")
test_on=1
}
}elseif(!strncmp(buffer,"jog",3)){
//severaloptionalfloatparameters.

//thencallsamethodtodosomethingwiththoseparameter
floatxx=pos_x
floatyy=pos_y
floatzz=pos_z

char*ptr=buffer
while(ptr&&ptr<buffer+sofar){
ptr=strchr(ptr,'')+1
switch(*ptr){

case'x':case'X':xx=atof(ptr+1)Serial.print('x')S

case'y':case'Y':yy=atof(ptr+1)Serial.print('y')S

case'z':case'Z':zz=atof(ptr+1)Serial.print('z')S
default:ptr=0break
}
}

jog(xx,yy,zz)
}

voidsetup(){
Serial.begin(BAUD)
Serial.println("Init...")
Serial.println("Stretching...")

sofar=0
Serial.println("**AWAKE**")
}

voidloop(){
//listenforserialcommands
while(Serial.available()>0){
buffer[sofar++]=Serial.read()

if(buffer[sofar1]=='')break//incasetherearemulti
}

//ifwehitasemicolon,assumeendofinstruction.
if(sofar>0&&buffer[sofar1]==''){
//whatifmessagefails/garbled?

//echoconfirmation
buffer[sofar]=0
Serial.println(buffer)

//dosomethingwiththecommand
processCommand()

//resetthebuffer
sofar=0

//echocompletion

Serial.println("Done.")
}
}

Share this:

Related

How to use a Ten Pin Seven


Segment display with Arduino
March 22, 2016
In "Tutorials"

Decoding Morse Code with


an Arduino and a Microphone
February 9, 2015
In "News"

Linear Stewart Platform test

Building and testing


micromouse sensors
May 16, 2016
In "Tutorials"

Hypocycloid reduction drives

video #2

Leave a Reply
Your email address will not be published. Required elds are marked *
Comment

Name *

Email *

Website

Je ne suis pas un robot


reCAPTCHA
Condentialit - Conditions

Post Comment

Notify me of follow-up comments by email.


Notify me of new posts by email.

Search

RECENT POSTS

Micromouse maze solving with Processing and the Right Hand Rule
Makelangelo 7.5.0
Micromouse encoders to measure distance
Micromouse motors with continuous servos
Building and testing micromouse sensors

RECENT COMMENTS

Wesley Knapp on Makelangelo 7.5.0


Micromouse encoders to measure distance - Marginally Clever Robots on Micromouse motors
with continuous servos
Micromouse contest update 2 - Marginally Clever Robots on Micromouse contest update 1
Micromouse robot building contest - Vancouver Mini Maker FaireVancouver Mini Maker Faire on
Vancouver Mini Maker Fair 2016 Robot Contest
Micromouse contest update 1 - Marginally Clever Robots on Vancouver Mini Maker Fair 2016
Robot Contest

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notications of new posts by
email.
Join 20 other subscribers

Email Address

Subscribe

Follow Us

Even more options


RSS - Posts
RSS - Comments

More Info
About us
Checkout
Contact Us
Jobs Now Hiring
My Account
My Cart
News
Order Tracking
Privacy
Shop
Support
Terms of Service

Your Name (required)

Your Email (required)

Subject

Your Message

Human test

Send

Marginally Clever Robots 2016


Storefront designed by WooThemes.

You might also like