9_arduino_functions - Louisiana Tech University

advertisement
living with the lab
user-defined functions in Arduino sketches
© 2012 David Hall
living with the lab
DISCLAIMER & USAGE
The content of this presentation is for informational purposes only and is intended only for students
attending Louisiana Tech University.
The author of this information does not make any claims as to the validity or accuracy of the information
or methods presented.
Any procedures demonstrated here are potentially dangerous and could result in injury or damage.
Louisiana Tech University and the State of Louisiana, their officers, employees, agents or volunteers, are
not liable or responsible for any injuries, illness, damage or losses which may result from your using the
materials or ideas, or from your performing the experiments or procedures depicted in this presentation.
If you do not agree, then do not view this content.
The copyright label, the Louisiana Tech logo, and the “living with the lab” identifier should not be removed
from this presentation.
You may modify this work for your own purposes as long as attribution is clearly provided.
2
living with the lab
example sketch without user-defined functions
run this program – you may need to change whisker and servo pin numbers
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int whisker1=0;
name your servos
void setup() {
take input from the whiskers on pin 7
pinMode(7,INPUT);
myservo1.attach(2);
myservo2.attach(3);
}
void loop() {
whisker1=digitalRead(7);
if (whisker1==HIGH)
{
myservo1.writeMicroseconds(1300);
myservo2.writeMicroseconds(1700);
delay(2000);
}
else
{
myservo1.writeMicroseconds(1700);
myservo2.writeMicroseconds(1300);
digital pins 2 and 3 will be
used to control the servos
read the state of the whisker on pin 7
(HIGH = pressed and LOW = not pressed)
when whisker 1 is pressed,
back up for 2 seconds
else go forward
}
}
3
living with the lab
move “forward” and “backward” commands into functions
Make these changes. Note that in this case, it may not make sense to use functions. But, if your have
a complex program like you will need for the “navigating the engineering disciplines” challenge, structuring your
program in this way could simplify and shorten your sketch.
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int whisker1=0;
void setup() {
pinMode(7,INPUT);
myservo1.attach(2);
myservo2.attach(3);
}
void loop() {
whisker1=digitalRead(7);
if (whisker1==HIGH)
{
myservo1.writeMicroseconds(1300);
myservo2.writeMicroseconds(1700);
delay(2000);
}
else
{
myservo1.writeMicroseconds(1700);
myservo2.writeMicroseconds(1300);
}
}
SAME UPPER PART OF SKETCH
void loop() {
whisker1=digitalRead(7);
if (whisker1==HIGH)
call “backward” function
{
backward();
delay(2000);
}
else
call “forward” function
{
forward();
}
“void” means the function doesn’t
}
return anything when it is completed
(no numbers are sent back to the loop)
void backward()
{
myservo1.writeMicroseconds(1300);
myservo2.writeMicroseconds(1700);
}
void forward()
{
myservo1.writeMicroseconds(1700);
myservo2.writeMicroseconds(1300);
}
4
living with the lab
more about functions
www.arduino.cc/en/Reference/FunctionDeclaration
5
living with the lab
in-class programming challenge
Modify the program shown earlier to allow your robot to roam as follows:
• robot goes forward by default
• robot backs up and turns right when left whisker is pressed
• robot backs up and goes left when right whisker is pressed
Please use functions for forward, backward, left and right movements. You may test
your robot on the floor to see if it works.
6
Download