Instantly share code, notes, and snippets.

Embed
What would you like to do?
DIY CyberGlove Arduino code
float analog0;
float analog1;
float analog2;
float analog3;
float analog0_close;
float analog1_close;
float analog2_close;
float analog3_close;
float analog0_open;
float analog1_open;
float analog2_open;
float analog3_open;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.println("Calibrating...");
Serial.println("Open hand.");
delay(2000);
analog0 = analogRead(A0);
analog1 = analogRead(A1);
analog2 = analogRead(A2);
analog3 = analogRead(A3);
analog0_open = analog0;
analog1_open = analog1;
analog2_open = analog2;
analog3_open = analog3;
Serial.print("Acquired: ");
Serial.print(analog0_open);
Serial.print(" ");
Serial.print(analog1_open);
Serial.print(" ");
Serial.print(analog2_open);
Serial.print(" ");
Serial.println(analog3_open);
Serial.println("Close hand");
delay(2000);
analog0 = analogRead(A0);
analog1 = analogRead(A1);
analog2 = analogRead(A2);
analog3 = analogRead(A3);
analog0_close = analog0;
analog1_close = analog1;
analog2_close = analog2;
analog3_close = analog3;
Serial.print("Acquired: ");
Serial.print(analog0_close);
Serial.print(" ");
Serial.print(analog1_close);
Serial.print(" ");
Serial.print(analog2_close);
Serial.print(" ");
Serial.println(analog3_close);
Serial.println("Calibration complete.");
Serial.println("Running...");
delay(2000);
}
// the loop routine runs over and over again forever:
void loop() {
analog0 = analogRead(A0);
analog1 = analogRead(A1);
analog2 = analogRead(A2);
analog3 = analogRead(A3);
// print out the value you read:
// Serial.print("Thumb: ");
// Serial.print(analog0);
// Serial.print(" Point: ");
// Serial.print(analog1);
// Serial.print(" Middle: ");
// Serial.print(analog2);
// Serial.print(" Ring: ");
// Serial.println(analog3);
//Scale the values to a range of [0,1]
float scaled_thumb = (analog0 - analog0_open)/(analog0_close - analog0_open);
float scaled_point = (analog1 - analog1_open)/(analog1_close - analog1_open);
float scaled_middle = (analog2 - analog2_open)/(analog2_close - analog2_open);
float scaled_ring = (analog3 - analog3_open)/(analog3_close - analog3_open);
Serial.print("Thumb: ");
Serial.print(scaled_thumb);
Serial.print(" Point: ");
Serial.print(scaled_point);
Serial.print(" Middle: ");
Serial.print(scaled_middle);
Serial.print(" Ring: ");
Serial.println(scaled_ring);
delay(1); // delay in between reads for stability
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment