Download presentation
Presentation is loading. Please wait.
Published byMeryl Bryant Modified over 9 years ago
1
user-defined functions in Arduino sketches living with the lab © 2012 David Hall
2
living with the lab 2 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. DISCLAIMER & USAGE
3
example sketch without user-defined functions #include 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); } when whisker 1 is pressed, back up for 2 seconds else go forward digital pins 2 and 3 will be used to control the servos take input from the whiskers on pin 7 read the state of the whisker on pin 7 (HIGH = pressed and LOW = not pressed) name your servos run this program – you may need to change whisker and servo pin numbers 3 living with the lab
4
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 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) { backward(); delay(2000); } else { forward(); } void backward() { myservo1.writeMicroseconds(1300); myservo2.writeMicroseconds(1700); } void forward() { myservo1.writeMicroseconds(1700); myservo2.writeMicroseconds(1300); } call “backward” function call “forward” function “void” means the function doesn’t return anything when it is completed (no numbers are sent back to the loop) 4 living with the lab
5
more about functions www.arduino.cc/en/Reference/FunctionDeclaration 5 living with the lab
6
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 living with the lab
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.