Difference between revisions of "Sparkfun: Flex Sensor"

From eLinux.org
Jump to: navigation, search
(Inputs and Outputs)
(Sample C Code)
Line 18: Line 18:
  
 
== Sample C Code ==
 
== Sample C Code ==
 +
<pre>
 +
 +
/****************************************************************
 +
* James Popenhagen
 +
* MiniProject02_FlexResistor.c
 +
* 9/23/12
 +
****************************************************************/
 +
#include <errno.h>
 +
#include <string.h>
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <fcntl.h>
 +
#include <poll.h>
 +
#include <signal.h>
 +
#include <unistd.h>
 +
 +
 +
/****************************************************************
 +
* Constants
 +
****************************************************************/
 +
#define MAX_BUF 64
 +
 +
/****************************************************************
 +
* Global Variables
 +
****************************************************************/
 +
int keepgoing = 1;
 +
 +
void signal_handler(int sig)
 +
{
 +
printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
 +
keepgoing = 0;
 +
}
 +
 +
/****************************************************************
 +
* read_ain
 +
****************************************************************/
 +
int read_ain(char* ain){
 +
FILE *fp;
 +
char ainPath[MAX_BUF];
 +
char ainRead[MAX_BUF];
 +
 +
snprintf(ainPath, sizeof ainPath, "/sys/devices/platform/omap/tsc/%s", ain);
 +
 +
if((fp = fopen(ainPath, "r")) == NULL){
 +
printf("Cannot open specified ain pin, %s\n", ain);
 +
return 1;
 +
}
 +
 +
if(fgets(ainRead, MAX_BUF, fp) == NULL){
 +
printf("Cannot read specified ain pin, %s\n", ain);
 +
}
 +
 +
fclose(fp);
 +
return atoi(ainRead);
 +
}
 +
 +
/****************************************************************
 +
* Main
 +
****************************************************************/
 +
int main(int argc, char **argv){
 +
 +
int ainPin;
 +
char ain[MAX_BUF];
 +
float duty_cycle = 0;
 +
float avgDutyCycle = 0;
 +
int avgCount = 1;
 +
 +
if (argc < 2){
 +
printf("Usage: ./MiniProject02 <ainpin>");
 +
exit(-1);
 +
}
 +
 +
signal(SIGINT, signal_handler);
 +
ainPin = atoi(argv[1]);
 +
snprintf(ain, sizeof ain, "ain%d", ainPin);
 +
 +
while (keepgoing) {
 +
usleep(1000);
 +
duty_cycle = read_ain(ain);
 +
duty_cycle = duty_cycle/4095 * 100;
 +
duty_cycle = 30 - duty_cycle;
 +
duty_cycle = duty_cycle*4;
 +
duty_cycle = duty_cycle - 28.6;
 +
if(duty_cycle <= 6) duty_cycle = 0;
 +
avgDutyCycle += duty_cycle;
 +
if (avgCount == 250) {
 +
printf("Calculating.  \r");
 +
fflush(stdout);
 +
}
 +
if (avgCount == 500) {
 +
printf("Calculating . \r");
 +
fflush(stdout);
 +
}
 +
if (avgCount == 750) {
 +
printf("Calculating  .\r");
 +
fflush(stdout);
 +
}
 +
if(avgCount >= 1000){
 +
avgDutyCycle = avgDutyCycle/avgCount;
 +
avgDutyCycle = avgDutyCycle * 100;
 +
printf("                                \r");
 +
printf("Calculating    %d\r",(int)avgDutyCycle);
 +
if(avgDutyCycle >= 9000) printf("Calculating    It's over 9000!!!!\r");
 +
fflush(stdout);
 +
avgDutyCycle = 0;
 +
avgCount = 0;
 +
}
 +
avgCount++;
 +
}
 +
 +
fflush(stdout);
 +
return 0;
 +
}
 +
 +
</pre>
 +
 +
You must specify the ain pin number as the first argument when executing this code after it is compiled.

Revision as of 03:44, 24 September 2012


Overview

The: product. The datasheet.


Inputs and Outputs

As for inputs and outputs, the sensor is basically a resistor and thus should be read from the analog input pins. To work with the analog input, you must put 1k ohm resistor from analog ground to the analog input you choose then connect the sensor from the analog VDD to the analog input you choose. The input voltage can then be read by the processor to determine how far the flex sensor is being bent (flexed).

Bone Usage

Reading and Interpreting Analog Input Data

Sample C Code


/****************************************************************
* James Popenhagen
* MiniProject02_FlexResistor.c
* 9/23/12
****************************************************************/
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>


/****************************************************************
* Constants
****************************************************************/
#define MAX_BUF 64

/****************************************************************
* Global Variables
****************************************************************/
int keepgoing = 1;

void signal_handler(int sig)
{
	printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
	keepgoing = 0;
}
	
/****************************************************************
 * read_ain
 ****************************************************************/
int read_ain(char* ain){
	FILE *fp;
	char ainPath[MAX_BUF];
	char ainRead[MAX_BUF];

	snprintf(ainPath, sizeof ainPath, "/sys/devices/platform/omap/tsc/%s", ain);

	if((fp = fopen(ainPath, "r")) == NULL){
		printf("Cannot open specified ain pin, %s\n", ain);
		return 1;
	}

	if(fgets(ainRead, MAX_BUF, fp) == NULL){
		printf("Cannot read specified ain pin, %s\n", ain);
	}
	
	fclose(fp);
	return atoi(ainRead);	
}

/****************************************************************
 * Main
 ****************************************************************/
int main(int argc, char **argv){

	int ainPin;
	char ain[MAX_BUF];
	float duty_cycle = 0;
	float avgDutyCycle = 0;
	int avgCount = 1;
	
	if (argc < 2){
		printf("Usage: ./MiniProject02 <ainpin>");
		exit(-1);
	}

	signal(SIGINT, signal_handler);
	ainPin = atoi(argv[1]);
	snprintf(ain, sizeof ain, "ain%d", ainPin);

	while (keepgoing) {
		usleep(1000);
		duty_cycle = read_ain(ain);
		duty_cycle = duty_cycle/4095 * 100;
		duty_cycle = 30 - duty_cycle;
		duty_cycle = duty_cycle*4;
		duty_cycle = duty_cycle - 28.6;
		if(duty_cycle <= 6) duty_cycle = 0;
		avgDutyCycle += duty_cycle;
		if (avgCount == 250) {
			printf("Calculating.  \r");
			fflush(stdout);
		}
		if (avgCount == 500) {
			printf("Calculating . \r");
			fflush(stdout);
		}
		if (avgCount == 750) {
			printf("Calculating  .\r");
			fflush(stdout);
		}
		if(avgCount >= 1000){
			avgDutyCycle = avgDutyCycle/avgCount;
			avgDutyCycle = avgDutyCycle * 100;
			printf("                                 \r");
			printf("Calculating    %d\r",(int)avgDutyCycle);
			if(avgDutyCycle >= 9000) printf("Calculating    It's over 9000!!!!\r");
			fflush(stdout);
			avgDutyCycle = 0;
			avgCount = 0;
		}
		avgCount++;
	}
	
	fflush(stdout);
	return 0;
}

You must specify the ain pin number as the first argument when executing this code after it is compiled.