Created
July 19, 2013 17:33
-
-
Save thecodemaiden/6040927 to your computer and use it in GitHub Desktop.
Adapting the code from http://www.instructables.com/id/Turn-a-pencil-drawing-into-a-capacitive-sensor-for/ for MSP430.
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
// Pin for the LED | |
int LEDPin = GREEN_LED; | |
int speakerPin = P1_1; | |
// Pin to connect to your drawing | |
int button1N = 4; | |
int button2N = 5; | |
// This is how high the sensor needs to read in order | |
// to trigger a touch. You'll find this number | |
// by trial and error, or you could take readings at | |
// the start of the program to dynamically calculate this. | |
int touchedCutoff = 60; | |
void setup(){ | |
Serial.begin(9600); | |
// Set up the LED | |
pinMode(LEDPin, OUTPUT); | |
digitalWrite(LEDPin, LOW); | |
} | |
void loop(){ | |
// If the capacitive sensor reads above a certain threshold, | |
// turn on the LED | |
if (readCapacitivePin(button1N) > touchedCutoff) { | |
digitalWrite(LEDPin, HIGH); | |
} | |
else { | |
digitalWrite(LEDPin, LOW); | |
} | |
// Every 500 ms, print the value of the capacitive sensor | |
if ( (millis() % 500) == 0){ | |
Serial.print("Capacitive Sensor on Pin P1_4 reads: "); | |
Serial.println(readCapacitivePin(button1N)); | |
} | |
} | |
// readCapacitivePin | |
// Input: x in P1.x pin name | |
// Output: A number, from 0 to 17 expressing | |
// how much capacitance is on the pin | |
// When you touch the pin, or whatever you have | |
// attached to it, the number will get higher | |
// In order for this to work now, | |
// The pin should have a 1+Megaohm resistor pulling | |
// it up to +5v. | |
uint8_t readCapacitivePin(int pinToMeasure){ | |
// This is how you declare a variable which | |
// will hold the PORT, PIN, and DDR registers | |
// on an AVR | |
volatile uint8_t* port; | |
volatile uint8_t* ddr; | |
volatile uint8_t* pin; | |
// Here we translate the input pin number from | |
// Arduino pin number to the AVR PORT, PIN, DDR, | |
// and which bit of those registers we care about. | |
byte bitmask; | |
port = (uint8_t*) &P1OUT; | |
ddr = (uint8_t*) &P1DIR; | |
bitmask = 1 << pinToMeasure; | |
pin = (uint8_t*) &P1IN; | |
// Discharge the pin first by setting it low and output | |
*port &= ~(bitmask); | |
*ddr |= bitmask; | |
delay(1); | |
// Make the pin an input WITHOUT the internal pull-up on | |
*ddr &= ~(bitmask); | |
// Now see how long the pin to get pulled up | |
int cycles = 16000; | |
for(int i = 0; i < cycles; i++){ | |
if (*pin & bitmask){ | |
cycles = i; | |
break; | |
} | |
} | |
// Discharge the pin again by setting it low and output | |
// It's important to leave the pins low if you want to | |
// be able to touch more than 1 sensor at a time - if | |
// the sensor is left pulled high, when you touch | |
// two sensors, your body will transfer the charge between | |
// sensors. | |
*port &= ~(bitmask); | |
*ddr |= bitmask; | |
return cycles; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment