Created
May 22, 2017 06:41
-
-
Save bha159/21c796501313f11fd229f8a60cbe1266 to your computer and use it in GitHub Desktop.
Arduino code for a gesture controlled hand(made using servo motor and acclerometer).
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
#include<Servo.h> | |
const int numReadings = 10; | |
int readings[numReadings] = {0}; | |
int index = 0; | |
int total = 0; | |
int average = 0; | |
const int xPin = A0; | |
const int epsilon = 5; | |
long sensorValue = 0; | |
Servo sv; | |
int servoVal = 90,oldServoVal = 90; | |
int DEBUG = 1; | |
int sensorThreshold = 324; | |
int maxRightChange = 68; | |
int maxLeftChange = 52; | |
int change = 0; | |
int toCalibrate = 0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(13,OUTPUT); | |
sv.attach(9); | |
Serial.println("Setting Servo to 90"); | |
sv.write(90); | |
delay(500); | |
Serial.println("Servo set to 90"); | |
for(int thisReading = 0; thisReading < numReadings ;thisReading++) | |
readings[thisReading] = 0; | |
if(toCalibrate) | |
{ | |
calibrate(); | |
delay(10000); | |
} | |
} | |
void loop() | |
{ | |
total -= readings[index]; | |
readings[index] = analogRead(xPin); | |
total += readings[index]; | |
index++; | |
if(index >= numReadings) | |
index = 0; | |
average = total/numReadings; | |
change = (sensorThreshold - average); | |
if(DEBUG) | |
{ | |
Serial.print("Change ="); | |
Serial.println(change); | |
} | |
setServo(change); | |
// delay(1000); | |
} | |
void setServo(int change) | |
{ | |
servoVal = 90; | |
if(abs(change) > epsilon) | |
{ | |
if(change < 0) | |
{ | |
servoVal = 90 - map(-change,0,maxLeftChange,0,90); | |
} | |
else | |
{ | |
servoVal = 90 + map(change,0,maxRightChange,0,90); | |
} | |
} | |
servoVal = constrain(servoVal,0,180); | |
// if(servoVal < 0) | |
// servoVal = 0; | |
// if(servoVal > 180) | |
// servoVal = 180; | |
Serial.print("ServoVal = "); | |
Serial.println(servoVal); | |
//if(abs(oldServoVal - servoVal) > epsilon) | |
moveServo(oldServoVal,servoVal); | |
oldServoVal = servoVal; | |
delay(50); | |
} | |
void moveServo(int from,int to) | |
{ | |
if(to != from) | |
{ | |
if(to > from) | |
{ | |
for(double i = from; i <= to; i += ((double)(to - from))/10) | |
{ | |
if(DEBUG) | |
{ | |
Serial.print("Moving to = "); | |
Serial.println((int)i); | |
} | |
sv.write((int)i); | |
delay(10); | |
} | |
} | |
else | |
{ | |
for(double i = from; i >= to; i += ((double)(to - from))/2) | |
{ | |
if(DEBUG) | |
{ | |
Serial.print("Moving to = "); | |
Serial.println((int)i); | |
} | |
sv.write((int)i); | |
delay(10); | |
} | |
} | |
} | |
} | |
void ledBlink() | |
{ | |
for(int i = 0; i < 3; i++) | |
{ | |
digitalWrite(13,LOW); | |
delay(1000); | |
digitalWrite(13,HIGH); | |
delay(1000); | |
} | |
digitalWrite(13,LOW); | |
} | |
void calibrate() | |
{ | |
Serial.println("Calibration Starting....."); | |
Serial.println("Getting Center Thresholds"); | |
loadDelay(); | |
sensorValue = 0; | |
for(int i = 0; i < 50 ; i++) | |
{ | |
sensorValue += analogRead(xPin); | |
} | |
sensorThreshold = sensorValue/50; | |
change = 0; | |
maxRightChange = 0; | |
maxLeftChange = 0; | |
Serial.println("Getting Left Thresholds"); | |
loadDelay(); | |
for(int i = 0; i < 50 ; i++) | |
{ | |
change = abs(sensorThreshold - analogRead(xPin)); | |
maxLeftChange = max(change,maxLeftChange); | |
} | |
Serial.println("Getting Right Threshold"); | |
loadDelay(); | |
for(int i = 0; i < 50 ; i++) | |
{ | |
change = abs(sensorThreshold - analogRead(xPin)); | |
maxRightChange = max(change,maxRightChange); | |
} | |
Serial.println("Calibration Complete"); | |
Serial.print("sensorThreshold = "); | |
Serial.println(sensorThreshold); | |
Serial.print("maxLeftChange = "); | |
Serial.println(maxLeftChange); | |
Serial.print("maxRightChange = "); | |
Serial.println(maxRightChange); | |
} | |
void loadDelay() | |
{ | |
for(int i = 0; i < 15; i++) | |
{ | |
Serial.print('.'); | |
delay(400); | |
} | |
Serial.println(' '); | |
} | |
//326 62 70 | |
//void calibrate() | |
//{ | |
// Serial.println("Calibration Starting....."); | |
// digitalWrite(13,LOW); | |
// for(int j = 0; j < 3 ; j++) | |
// { | |
// delay(1000); | |
// ledBlink(); | |
// delay(3000); | |
// sensorValue = 0; | |
// for(int i = 0;i < 50;i++) | |
// { | |
// sensorValue += analogRead(xPin); | |
// } | |
// sensorThresholds[j] = sensorValue/50; | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment