Go Down

Topic: Sparkfun Touch Shield: Programming (Read 668 times) previous topic - next topic

jbdelutis

Jan 13, 2013, 02:18 pm Last Edit: Jan 13, 2013, 02:29 pm by jbdelutis Reason: 1
I am a newbie and I got myself a sparkfun Touch Shield I am trying to modify the example code on the sparkfun site: www.sparkfun.com/capsense to allow me to enter a code ex:1234 to turn a pin ex:1 on and then enter the same code and turn it off. PLEASE HELP!!!!!

PaulS

Quote
I am trying to modify the example code on the sparkfun site

Then, your code is no longer what is at that site, is it?

Quote
PLEASE HELP!!!!!

WHAT DO YOU NEED HELP WITH?????? Your hearing aids?

jbdelutis

Quote
PLEASE HELP!!!!!

Quote
WHAT DO YOU NEED HELP WITH?????? Your hearing aids?


Quote
to allow me to enter a code ex:1234 to turn a pin ex:1 on and then enter the same code and turn it off.

PaulS

Quote
to allow me to enter a code ex:1234 to turn a pin ex:1 on and then enter the same code and turn it off.

OK. Your problem is on line 98. Fix that, and it will work. Or not. Where is your code? What happens when you run it? How does that differ from what you want?

jbdelutis

Here is my  code
Code: [Select]
// include the atmel I2C libs
#include "mpr121.h"
#include "i2c.h"


// Match key inputs with electrode numbers
#define ONE 8
#define TWO 5
#define THREE 2
#define FOUR 7
#define FIVE 4
#define SIX 1
#define SEVEN 6
#define EIGHT 3
#define NINE 0

//extras (not used)
#define ELE9 9
#define ELE10 10
#define ELE11 11

//interupt pin
int irqpin = 2;  // D2

void setup()
{
  //make sure the interrupt pin is an input and pulled high
  pinMode(6, OUTPUT);
  pinMode(irqpin, INPUT);
  digitalWrite(irqpin, HIGH);
 
  //configure serial out
  Serial.begin(9600);
 
  //output on ADC4 (PC4, SDA)
  DDRC |= 0b00010011;
  // Pull-ups on I2C Bus
  PORTC = 0b00110000;
  // initalize I2C bus. Wiring lib not used.
  i2cInit();
 
  delay(100);
  // initialize mpr121
  mpr121QuickConfig();
 
  // prints 'Ready...' when you can start hitting numbers
  Serial.println("Ready...");
}

void loop()
{
  uint16_t touchstatus;
 
  touchstatus = mpr121Read(0x01) << 8;
  touchstatus |= mpr121Read(0x00);
 
  if (touchstatus & (1<<ONE))
  if (touchstatus & (1<<TWO))
      digitalWrite(6, LOW);
   
  if (touchstatus & (1<<TWO))
  if (touchstatus & (1<<THREE))
      digitalWrite(6, HIGH);
}

With this you have to press the numbers at the same time

jbdelutis

I would like to be able to press the keys with a delay in between but when I add
Code: [Select]
delay(1000); the led i'm testing with won't turn on at all

jbdelutis

Sorry uploaded the wrong code
HERE IS THE RIGHT CODE
Code: [Select]
// include the atmel I2C libs
#include "mpr121.h"
#include "i2c.h"


// Match key inputs with electrode numbers
#define ONE 8
#define TWO 5
#define THREE 2
#define FOUR 7
#define FIVE 4
#define SIX 1
#define SEVEN 6
#define EIGHT 3
#define NINE 0

//extras (not used)
#define ELE9 9
#define ELE10 10
#define ELE11 11

//interupt pin
int irqpin = 2;  // D2

void setup()
{
  //make sure the interrupt pin is an input and pulled high
  pinMode(6, OUTPUT);
  pinMode(irqpin, INPUT);
  digitalWrite(irqpin, HIGH);
 
  //configure serial out
  Serial.begin(9600);
 
  //output on ADC4 (PC4, SDA)
  DDRC |= 0b00010011;
  // Pull-ups on I2C Bus
  PORTC = 0b00110000;
  // initalize I2C bus. Wiring lib not used.
  i2cInit();
 
  delay(100);
  // initialize mpr121
  mpr121QuickConfig();
 
  // prints 'Ready...' when you can start hitting numbers
  Serial.println("Ready...");
}

void loop()
{
  uint16_t touchstatus;
 
  touchstatus = mpr121Read(0x01) << 8;
  touchstatus |= mpr121Read(0x00);
 
  if (touchstatus & (1<<ONE))
  if (touchstatus & (1<<TWO))
      digitalWrite(6, LOW);
   
  if (touchstatus & (1<<ONE))
  if (touchstatus & (1<<TWO))
      digitalWrite(6, HIGH);
}

PaulS

The sparkfun site has a number of code snippets. One is the getNumber() function. It looks to me like that function should be part of your code.

It also looks, from that code, that
Code: [Select]
  if (touchstatus & (1<<ONE))
returns true if the square labeled 1 was touched.

Why you only test for 2 being touched if one is being touched is rather a mystery.

Read and understand the getNumber() function on the sparkfun site.

jbdelutis

When you hold keys 1 and 2 down the led turns on until you release
The code i'm using is
Code: [Select]
// include the atmel I2C libs
#include "mpr121.h"
#include "i2c.h"


// Match key inputs with electrode numbers
#define ONE 8
#define TWO 5
#define THREE 2
#define FOUR 7
#define FIVE 4
#define SIX 1
#define SEVEN 6
#define EIGHT 3
#define NINE 0

//extras (not used)
#define ELE9 9
#define ELE10 10
#define ELE11 11

//interupt pin
int irqpin = 2;  // D2

void setup()
{
 //make sure the interrupt pin is an input and pulled high
 pinMode(6, OUTPUT);
 pinMode(irqpin, INPUT);
 digitalWrite(irqpin, HIGH);
 
 //configure serial out
 Serial.begin(9600);
 
 //output on ADC4 (PC4, SDA)
 DDRC |= 0b00010011;
 // Pull-ups on I2C Bus
 PORTC = 0b00110000;
 // initalize I2C bus. Wiring lib not used.
 i2cInit();
 
 delay(100);
 // initialize mpr121
 mpr121QuickConfig();
 
 // prints 'Ready...' when you can start hitting numbers
 Serial.println("Ready...");
}

void loop()
{
 uint16_t touchstatus;
 
 touchstatus = mpr121Read(0x01) << 8;
 touchstatus |= mpr121Read(0x00);
 
 if (touchstatus & (1<<ONE))
 if (touchstatus & (1<<TWO))
     digitalWrite(6, HIGH);
   
 if (touchstatus & (1<<ONE))
 if (touchstatus & (1<<TWO))
     digitalWrite(6, LOW);
}

jbdelutis

Could you drop a link to that page I can't seem to find it

PaulS

Quote
When you hold keys 1 and 2 down the led turns on until you release

OK. And, what is it you want to have happen?

PaulS

#11
Jan 13, 2013, 03:03 pm Last Edit: Jan 13, 2013, 03:05 pm by PaulS Reason: 1
Quote
Could you drop a link to that page I can't seem to find it

Its the one in your initial post. Scroll down a bit.

http://www.sparkfun.com/tutorials/288

Edit: That's strange. I clicked on the link in the first post, and it took me to a site. When I copy the URL and paste it here, it's not the same one.

jbdelutis

It's a redirect page. And I want to be able to have a delay between pressing the numbers and the output to stay on after releasing the last button (please use example code 1234)

PaulS

On any given pass through loop, you should be detecting which, if any, "key" is being pressed, if you are trying to use this as a keypad. That's the first but of code you should develop. The code that you have now that detects the simultaneous press of two "key"s is a good starting point. Put that in a function that returns an int. Get rid of the two presses bit. Make the function return a value - -1 for no key, 0 to 9 for the actual key. Look at the getNumber() function for clues.

When you have a function that can tell you which key, if any, is pressed, on any pass through loop, then you need to determine if it is the same key as last time. For this, you need two variables (global) - currKey and prevKey. The currKey variable will be valued by the call to the function you write. The prevKey variable will be set to currKey at the end of loop. Then, you can use:
Code: [Select]
if(currKey != prevKey)
{
}

to see when a change occurs.

Inside that block, you need to make sure that currKey is not no key. If it isn't, then add the key to the array or value you are recording data in. A value is easier.

Code: [Select]
int value = 0;

Code: [Select]
value *= 10;
value += currKey;


Independent of the touch screen code, you would test whether value is 1234 (or whatever "password" you choose. If so, change the LED state.

Independent of that, you apply the LED state to the LED.

In pseudo code:
void loop
   currKey = getKey()
   if currKey not prevKey
     multiply value by 10
     add currKey
   prevKey = currkey

   if value equal password
     state = !state

   digitalWrite state to led pin

Go Up