Professional Documents
Culture Documents
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
#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
video #2
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
Website
Post Comment
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
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
More Info
About us
Checkout
Contact Us
Jobs Now Hiring
My Account
My Cart
News
Order Tracking
Privacy
Shop
Support
Terms of Service
Subject
Your Message
Human test
Send