Skip to content

Instantly share code, notes, and snippets.

@ekenels
Created March 12, 2019 16:31
Show Gist options
  • Select an option

  • Save ekenels/9f9e7514d744b0a9d339430530c0b2c9 to your computer and use it in GitHub Desktop.

Select an option

Save ekenels/9f9e7514d744b0a9d339430530c0b2c9 to your computer and use it in GitHub Desktop.
/*
http://www.robotoid.com/appnotes/arduino-operating-two-servos.html = Servo Motor code used and modified
https://www.instructables.com/id/Flex-Sensor-Glove/ = Flex Sensor code used and modified
https://www.arduino.cc/reference/en/language/functions/math/map/ = Map reference used from.
*/
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
const int flexpin = 9;
void setup ()
{
servoLeft.attach(12); // Set left servo to digital pin 6
servoRight.attach(6); // Set right servo to digital pin 12
Serial.begin(9600);
}
void loop ()
{
int flexposition;
int servopositionLeft;
int servopositionRight;
flexposition = analogRead(flexpin); //Read values from flex sensor
servopositionLeft = map(flexposition, 900, 700, 0, 90); // The Map range for ServoLeft and the values to act upon readings
servopositionRight = map(flexposition, 900, 700, 90, 0); // The Map range for ServoRight and the values to act upon readings
servopositionLeft = constrain(servopositionLeft, 0, 90); //Define Limits for Left Servo (maximun and minimum values of degrees to turn)
servopositionRight = constrain(servopositionRight, 0, 90);//Define Limits for Right Servo (maximun and minimum values of degrees to turn)
servoLeft.write(servopositionLeft); //Left Servo moves from values
servoRight.write(servopositionRight);//Right Servo moves from values
Serial.print("sensor: ");
Serial.print(flexposition);
Serial.print(" servoLeft:");
Serial.println(servopositionLeft);
Serial.print(" servoRight:");
Serial.println(servopositionRight);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment