Touch sensor for MSP430 launchpad

on Tuesday, November 18, 2014



Circuit diagram:








Coding:


Programming code for code composer studio users:





#include  <msp430g2231.h>
#define Key (P1IN & 0X08)
unsigned int i;

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;    // Stop watchdog timer
  P1DIR |= 0x01;               // Set P1.0 to output direction
  P1OUT &= ~(0x01);
  while(1)
  {
        if(!Key)
        {
            while(!Key);
            P1OUT ^= 0x01;          // Toggle P1.0 using exclusive-OR
        }
  }
}





Programming code for energia software users:





int led = P1_0;
int button = P1_3;
void setup()
{
  pinMode(led,OUTPUT);
  pinMode(button,INPUT);
  digitalWrite(led,LOW);
}

void loop()
{
  int data = digitalRead(button);
 
  if (data == LOW){
  if(digitalRead(led) == HIGH){
  digitalWrite(led,LOW);
 }
if(digitalRead(led) == LOW){

  digitalWrite(led,HIGH);
}
 
  }
 
}