//Sampling at 500 Hz analog input signal
//by Carlos Alquezar
//February 2014

//Based on timer interrupts
//by Amanda Ghassaei
//June 2012

* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.

//For arduino uno or any board with ATMEL 328/168.. diecimila, duemilanove, lilypad, nano, mini…

//timer0 will interrupt at 500Hz


#define THRESHOLD 200 //In order to determine the state of the hand (opened/closed)
#define EMGPIN 3 //Analog pin connected to Muscle Sensor V3 Board
#define LITTLEPIN 3 //Digital pin used by Little servo
#define RINGPIN 5 //Digital pin used by Ring servo
#define MIDDLEPIN 6 //Digital pin used by Middle servo
#define INDEXPIN 9 //Digital pin used by Index servo
#define THUMBPIN 10 //Digital pin used by Thumb servo

//Constants used to open and close the fingers
#define LITTLE 1
#define RING 2
#define MIDDLE 3
#define INDEX 4
#define THUMB 5

Servo servoLittleFinger; // Define servo fingers
Servo servoRingFinger; // Define servo fingers
Servo servoMiddleFinger; // Define servo fingers
Servo servoIndexFinger; // Define servo fingers
Servo servoThumbFinger; // Define servo fingers

int finger;

// Motion routines for handopen and handclose
void openhand(){for(finger = 1; finger < 6; finger++){openFinger(finger);}} void closehand(){for(finger = 1; finger < 6 ; finger++){closeFinger(finger);}} // You have to rewrite properly the functions to open and close the fingers // according of your assembly void openFinger(int finger){ if(finger==LITTLE){servoLittleFinger.write(0);} // Little finger else if(finger==RING){servoRingFinger.write(170);}// Ring finger else if(finger==MIDDLE){servoMiddleFinger.write(170);}// Middle finger else if(finger==INDEX){servoIndexFinger.write(170);}// Index finger else if(finger==THUMB){servoThumbFinger.write(0);}//Thumb finger } void closeFinger(int finger){ if(finger==LITTLE){servoLittleFinger.write(170);} // Little finger else if(finger==RING){servoRingFinger.write(0);}// Ring finger else if(finger==MIDDLE){servoMiddleFinger.write(0);}// Middle finger else if(finger==INDEX){servoIndexFinger.write(0);}// Index finger else if(finger==THUMB){servoThumbFinger.write(170);}//Thumb finger } void setup(){ Serial.begin(115200); //BAUDRATE set to 115200, remember it to set monitor serial properly cli();//stop interrupts //set timer0 interrupt at 500Hz TCCR0A = 0;// set entire TCCR0A register to 0 TCCR0B = 0;// same for TCCR0B TCNT0 = 0;//initialize counter value to 0 // set compare match register for 500hz increments OCR0A = 124;// = (16*10^6) / (500*256) – 1 (must be <256) // turn on CTC mode TCCR0A |= (1 << WGM01); // Set CS12 bit for 256 prescaler TCCR0B |= (1 << CS12); // enable timer compare interrupt TIMSK0 |= (1 << OCIE0A); sei();//allow interrupts servoLittleFinger.attach(LITTLEPIN); // Set Little finger servo to digital pin 3 servoRingFinger.attach(RINGPIN); // Set Ring finger servo to digital pin 5 servoMiddleFinger.attach(MIDDLEPIN); // Set Middle finger servo to digital pin 6 servoIndexFinger.attach(INDEXPIN); // Set Index finger servo to digital pin 9 servoThumbFinger.attach(THUMBPIN); // Set Thumb finger servo to digital pin 10 }//end setup //This function is executed every interrupt ISR(TIMER0_COMPA_vect){//timer0 interrupts at 500Hz int value = analogRead(EMGPIN); //Sampling analog signal if(value>THRESHOLD) //If the value of the sample is greater than THRESHOLD means that the hand has been closed
else //Otherwise the hand is open
Serial.println(value); //You can use serial monitor to set THRESHOLD properly, comparing the values shown when you open and close your hand

void loop() {// Nothing to do here, all is done in the interrupt function

Leave a Reply