Last active
August 29, 2015 14:08
-
-
Save Robotto/2579bb295227dea9aeca to your computer and use it in GitHub Desktop.
Glove code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int servo_minimum = 0; | |
static int servo_maximum = 180; | |
static unsigned int filter_alpha = 10; | |
#include <Servo.h> | |
Servo finger1, finger2, finger3, finger4, finger5; | |
int servoPin1 = 5; | |
int servoPin2 = 6; | |
int servoPin3 = 9; | |
int servoPin4 = 10; | |
int servoPin5 = 3; | |
int flexPin1 = A0; | |
int flexPin2 = A1; | |
int flexPin3 = A2; | |
int flexPin4 = A3; | |
int flexPin5 = A4; | |
void setup() | |
{ | |
Serial.begin(115200); | |
//Attach the servo objects to their respective pins | |
finger1.attach(servoPin1); | |
finger2.attach(servoPin2); | |
finger3.attach(servoPin3); | |
finger4.attach(servoPin4); | |
finger5.attach(servoPin5); | |
//Set each flex sensor pin to input: this is necessary | |
pinMode(flexPin1, INPUT); | |
pinMode(flexPin2, INPUT); | |
pinMode(flexPin3, INPUT); | |
pinMode(flexPin4, INPUT); | |
pinMode(flexPin5, INPUT); | |
} | |
//initialize the flex variables (uint16_t) with a startup value of 600 | |
unsigned int flex1=600; | |
unsigned int flex2=600; | |
unsigned int flex3=600; | |
unsigned int flex4=600; | |
unsigned int flex5=600; | |
void loop() | |
{ | |
//moving average filters. | |
flex1 = (flex1 * filter_alpha + analogRead(flexPin1)) / (filter_alpha+1); //pinky | |
flex2 = (flex2 * filter_alpha + analogRead(flexPin2)) / (filter_alpha+1); //ring | |
flex3 = (flex3 * filter_alpha + analogRead(flexPin3)) / (filter_alpha+1); //middle | |
flex4 = (flex4 * filter_alpha + analogRead(flexPin4)) / (filter_alpha+1); //index | |
flex5 = (flex5 * filter_alpha + analogRead(flexPin5)) / (filter_alpha+1); //thumb | |
//Graph the pinky :) | |
Serial.print("Flex1 graph: "); | |
for(int i = 0; i<flex1/5;i++) Serial.print('-'); //divide by 5 to fit the terminal window (but reduce resolution) | |
Serial.print("| "); | |
Serial.println(flex1); | |
/* Defines "pos" variables as being proportional to the flex inputs. | |
The 400 to 700 value range seemed adequate for my sensors, but you can change | |
yours accordingly. */ | |
int pos1 = map(flex1, 400, 700, servo_minimum, servo_maximum); | |
int pos2 = map(flex2, 400, 700, servo_minimum, servo_maximum); | |
int pos3 = map(flex3, 400, 700, servo_minimum, servo_maximum); | |
int pos4 = map(flex4, 480, 640, servo_minimum, servo_maximum); | |
int pos5 = map(flex5, 400, 700, servo_minimum, servo_maximum); | |
//Tells servos to move by the amount specified in the "pos" variables | |
finger1.write(pos1); | |
finger2.write(pos2); | |
finger3.write(pos3); | |
finger4.write(pos4); | |
finger5.write(pos5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment