adamundefined

Circuit 9

Mar 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. /*
  2. SparkFun Inventor's Kit
  3. Example sketch 09
  4.  
  5. #include <Servo.h>
  6.  
  7. //Servo servo1;
  8.  
  9. const int flexpin = 0;
  10.  
  11.  
  12. void setup()
  13. {
  14.    
  15.   Serial.begin(9600);
  16.  
  17.   //servo1.attach(9);
  18. }
  19.  
  20.  
  21. void loop()
  22. {
  23.   int flexposition;    // Input value from the analog pin.
  24.   int servoposition;   // Output value to the servo.
  25.  
  26.   // Read the position of the flex sensor (0 to 1023):
  27.  
  28.   flexposition = analogRead(flexpin);
  29.  
  30.   // Because the voltage divider circuit only returns a portion
  31.   // of the 0-1023 range of analogRead(), we'll map() that range
  32.   // to the servo's range of 0 to 180 degrees. The flex sensors
  33.   // we use are usually in the 600-900 range:
  34.  
  35.   //servoposition = map(flexposition, 600, 900, 0, 180);
  36.   //servoposition = constrain(servoposition, 0, 180);
  37.  
  38.   // Now we'll command the servo to move to that position:
  39.  
  40.   //servo1.write(servoposition);
  41.  
  42.   // Because every flex sensor has a slightly different resistance,
  43.   // the 600-900 range may not exactly cover the flex sensor's
  44.   // output. To help tune our program, we'll use the serial port to
  45.   // print out our values to the serial monitor window:
  46.  
  47.   Serial.print("sensor: ");
  48.   Serial.print(flexposition);
  49.   //Serial.print("  servo: ");
  50.   //Serial.println(servoposition);
  51.  
  52.   // Note that all of the above lines are "print" except for the
  53.   // last line which is "println". This puts everything on the
  54.   // same line, then sends a final carriage return to move to
  55.   // the next line.
  56.  
  57.   delay(20);  // wait 20ms between servo updates
  58. }
Add Comment
Please, Sign In to add comment