Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. /*
  2. SparkFun Inventor's Kit for Arduino
  3. Example sketch 09
  4.  
  5. FLEX SENSOR
  6.  
  7. Use the "flex sensor" to change the position of a servo
  8.  
  9. In the previous sketch, we learned how to command a servo to
  10. mode to different positions. In this sketch, we'll introduce
  11. a new sensor, and use it to control the servo.
  12.  
  13. A flex sensor is a plastic strip with a conductive coating.
  14. When the strip is straight, the coating will be a certain
  15. resistance. When the strip is bent, the particles in the coating
  16. get further apart, increasing the resistance. You can use this
  17. sensor to sense finger movement in gloves, door hinges, stuffed
  18. animals, etc. See http://www.sparkfun.com/tutorials/270 for
  19. more information.
  20.  
  21. Hardware connections:
  22.  
  23. Flex sensor:
  24.  
  25. The flex sensor is the plastic strip with black stripes.
  26. It senses bending away from the striped side.
  27.  
  28. The flex sensor has two pins, and since it's a resistor,
  29. the pins are interchangable.
  30.  
  31. Connect one of the pins to ANALOG IN pin 0 on the Arduino.
  32. Connect the same pin, through a 10K Ohm resistor (brown
  33. black orange) to GND.
  34. Connect the other pin to 5V.
  35.  
  36. Servo:
  37.  
  38. The servo has a cable attached to it with three wires.
  39. Because the cable ends in a socket, you can use jumper wires
  40. to connect between the Arduino and the servo. Just plug the
  41. jumper wires directly into the socket.
  42.  
  43. Connect the RED wire (power) to 5 Volts (5V)
  44. Connect the WHITE wire (signal) to digital pin 9
  45. Connect the BLACK wire (ground) to ground (GND)
  46.  
  47. Note that servos can use a lot of power, which can cause your
  48. Arduino to reset or behave erratically. If you're using large
  49. servos or many of them, it's best to provide them with their
  50. own separate 5V supply. See this Arduino Forum thread for info:
  51. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239464763
  52.  
  53. This sketch was written by SparkFun Electronics,
  54. with lots of help from the Arduino community.
  55. This code is completely free for any use.
  56. Visit http://learn.sparkfun.com/products/2 for SIK information.
  57. Visit http://www.arduino.cc to learn about the Arduino.
  58.  
  59. Version 2.0 6/2012 MDG
  60. */
  61.  
  62.  
  63. // Include the servo library to add servo-control functions:
  64.  
  65. #include <Servo.h>
  66.  
  67. // Create a servo "object", called servo1. Each servo object
  68. // controls one servo (you can have a maximum of 12):
  69.  
  70. Servo servo1;
  71.  
  72. // Define the analog input pin to measure flex sensor position:
  73.  
  74. const int flexpin = 0;
  75.  
  76.  
  77. void setup()
  78. {
  79. // Use the serial monitor window to help debug our sketch:
  80.  
  81. Serial.begin(9600);
  82.  
  83. // Enable control of a servo on pin 9:
  84.  
  85. servo1.attach(9);
  86. }
  87.  
  88.  
  89. void loop()
  90. {
  91. int flexposition; // Input value from the analog pin.
  92. int servoposition; // Output value to the servo.
  93.  
  94. // Read the position of the flex sensor (0 to 1023):
  95.  
  96. flexposition = analogRead(flexpin);
  97.  
  98. // Because the voltage divider circuit only returns a portion
  99. // of the 0-1023 range of analogRead(), we'll map() that range
  100. // to the servo's range of 0 to 180 degrees. The flex sensors
  101. // we use are usually in the 600-900 range:
  102.  
  103. servoposition = map(flexposition, 600, 900, 0, 180);
  104. servoposition = constrain(servoposition, 0, 180);
  105.  
  106. // Now we'll command the servo to move to that position:
  107.  
  108. servo1.write(servoposition);
  109.  
  110. // Because every flex sensor has a slightly different resistance,
  111. // the 600-900 range may not exactly cover the flex sensor's
  112. // output. To help tune our program, we'll use the serial port to
  113. // print out our values to the serial monitor window:
  114.  
  115. Serial.print("sensor: ");
  116. Serial.print(flexposition);
  117. Serial.print(" servo: ");
  118. Serial.println(servoposition);
  119.  
  120. // Note that all of the above lines are "print" except for the
  121. // last line which is "println". This puts everything on the
  122. // same line, then sends a final carriage return to move to
  123. // the next line.
  124.  
  125. // After you upload the sketch, turn on the serial monitor
  126. // (the magnifying-glass icon to the right of the icon bar).
  127. // You'll be able to see the sensor values. Bend the flex sensor
  128. // and note its minimum and maximum values. If you replace the
  129. // 600 and 900 in the map() function above, you'll exactly match
  130. // the flex sensor's range with the servo's range.
  131.  
  132. delay(20); // wait 20ms between servo updates
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement