Show Posts
Pages: 1 ... 80 81 [82] 83 84 ... 252
1216  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 06, 2014, 09:32:54 am
Ok, this is what I tried

Code:
//.h file

#ifndef OnewireKeypad_h
#define OnewireKeypad_h

#include <Print.h>
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>

#define NO_KEY '\0'
#define WAITING 0
#define PRESSED 1
#define RELEASED 2
#define HELD 3

#define MAX_KEYS 16

class OnewireKeyPad {

public:
void Init(char KP[], uint8_t Rows, uint8_t Cols, uint8_t Pin);
char Getkey();
void Calc(double R1, double R2, double voltage);
void SetHoldTime(unsigned long setH_Time);
void SetDebounceTime(unsigned long setD_Time);
uint8_t Key_State();
bool Readpin();
void LatchKP();
bool checkLatchedKey(char _key);

private:
double values[MAX_KEYS];
bool latchedKey[MAX_KEYS];
char _Data[MAX_KEYS];
char Out[20];
uint8_t _Rows, _Cols, _Pin;
char lastKey;
};


template < typename T >
class Display : public OnewireKeyPad
{
public:
Display(T & port) : port_( port ),OnewireKeyPad() {}
T &port_;
};

template< >
class Display < LiquidCrystal_I2C > : public OnewireKeyPad
{
    public:
        Display( LiquidCrystal_I2C & lcd ) : port_( lcd ),OnewireKeyPad() {}
LiquidCrystal_I2C & port_;   
};

#endif

Here is the .cpp file
Code:
// OnewireKP.cpp

#include "OneWireKeypad.h"

uint8_t SIZE = 16;
unsigned long time = 0,holdTime, startTime, debounceTime;
bool state, lastState = 0, lastRead = 0;


void OnewireKeyPad :: Init(char KP[], uint8_t Rows, uint8_t Cols, uint8_t Pin)
{
  _Rows = Rows;
  _Cols = Cols;
  _Pin = Pin;
 
  startTime = 0;
  SetDebounceTime(10);
  SetHoldTime(500);
  SIZE = (_Rows * _Cols);
 
  for(byte lidx = 0; lidx < SIZE; lidx++)
  {
    _Data[lidx] = KP[lidx];
    latchedKey[lidx] = 0;
  }
}

char OnewireKeyPad :: Getkey()
{
  int Reading = analogRead(_Pin);
  if(millis() - startTime > debounceTime)
  {
    for(byte i = 0; i < SIZE; i++)
    {
      if(Reading && Reading <= int(values[(SIZE-1)-i]) + 1.9)
        return _Data[(SIZE-1)-i];
    }
    startTime = millis();
  } 
  return NO_KEY;
}

void OnewireKeyPad :: Calc(double R1, double R2, double voltage)
{
  uint8_t i = 0;
  for(double R = 0; R < _Rows; R++)
  {
    for(double C = 0; C < _Cols; C++)
    {
      double V = (voltage * R2)/(R2+ (R1*R) + (R2*C));
      values[i] = V * (1023 / voltage); //204.6 = 5V, 310 = 3V
 
    //if(_rows == 2)
      port_->setLcdCursor(0, R );
        //else
          //port_.setLcdCursor(0, R);
       

      port_.print(int(values[i]));
  port_.print(", ");
      i++;
    }

       port_.println();
  }
}

void OnewireKeyPad :: SetHoldTime(unsigned long setH_Time)
{
  holdTime = setH_Time;
}

void OnewireKeyPad :: SetDebounceTime(unsigned long setD_Time)
{
  debounceTime = setD_Time;
}

uint8_t OnewireKeyPad :: Key_State()
{
  if((state = Readpin()) != lastState)
  {
    lastState = state;
    return (Readpin()? PRESSED : RELEASED);
  }
  //else KP_Hold(500);
  else if(Readpin()){
    time = millis();

    while(Readpin())
    {
      if((millis() - time) > holdTime)
      {
        return HELD;
      }
    }
    lastState = 0;
    return RELEASED;
  }
  return WAITING;

}

bool OnewireKeyPad :: Readpin()
{
  return analogRead(_Pin)? 1 : 0;
}

void OnewireKeyPad :: LatchKP()
{
  char output[20];
  bool PRINT = false;

  char _key = Getkey();
  bool read = _key? 1: 0;

  if(read != lastRead)
  {
    if(read)
    {
      for(int idx = 0; idx < SIZE; idx++)
      {
        if(_key == _Data[idx])
        {
          latchedKey[idx] = !latchedKey[idx];
          //Serial.println(latchedKey[idx]);
          if(latchedKey[idx])
          {
            sprintf(output, "Key %c was Latched",_key);
            PRINT = true;
          }
          else
          {
            sprintf(output, "Key %c was Unlatched",_key);
            PRINT = true;
          }
        }
      }
    }
    lastRead = read;
    if(PRINT)
      port_.println(output);
  }
}

bool OnewireKeyPad :: checkLatchedKey(char _key)
{
  for(int idx = 0; idx < SIZE; idx++)
  {
    if(_key == _Data[idx])
      return latchedKey[idx];   
  }
  return false;
}

Sketch:
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWireKeypad.h>

LiquidCrystal_I2C lcd(0x20,20,4);
Display <LiquidCrystal_I2C> KP(lcd);

char KEYS[]= {
  '1','2','3',//'A',
  '4','5','6',//'B',
  '7','8','9',//'C',
  '*','0','#',//'D'
};

void setup (){
    //Serial.begin(115200);

  KP.Init(KEYS, 4, 3, A0); // (KeyMap, Rows, Columns, Pin)
  KP.Calc(4700, 1000, 5); //(Row value, Col value, Refference voltage)
}
void loop()
{
  char Key = KP.Getkey();
  if(Key)
    Serial.println(KP.Key_State());
  KP.LatchKP();
}

Errors:
Quote
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp: In member function 'void OnewireKeyPad::Calc(double, double, double)':
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp:54: error: 'port_' was not declared in this scope
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp:64: error: 'port_' was not declared in this scope
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp: In member function 'void OnewireKeyPad::LatchKP()':
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp:141: error: 'port_' was not declared in this scope
1217  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 05, 2014, 08:15:30 pm
I'll give it a try, maybe not tonight i'm kinda tired, but I'll definitely do it tomorrow.

Thanks.
1218  Using Arduino / Programming Questions / [SOLVED]Need help referencing two classes in one constructor. on: February 05, 2014, 06:13:38 pm
Hey guys, I'm hoping someone can point me in the right direction with a little issue i'm having. I am making a library that uses functions from the Print class, and LiquidCrystal_I2C class, but I can't seem to differentiate functions from the two. The problem is if I initialize my code to use Serial as opposed to Lcd, and comment out the function setCursor(), the sketch works. However and this is another issue i'm trying to work out, if I uncomment the setCursor function and leave it there, I get the error 'class Print' has no member named 'setCursor', which I know it doesn't because it belongs to the LiquidCrystal_I2C library.

My question is what would be the correct way to go about solving this problem, and how can I tell the code what functions to use based on the constructor "Serial / Lcd"?

Code:
class OnewireKeyPad{
Print & port_;
//LiquidCrystal_I2C & port_;
public:
   OnewireKeyPad (Print & port) : port_ (port) { }
            //OnewireKeyPad (LiquidCrystal_I2C & port) : port_ (port) { }

1219  Using Arduino / Programming Questions / Re: Spill over to second line for text over 16 chars | 16x2 LCD Screen on: February 05, 2014, 01:24:46 pm
Related post HERE
1220  Using Arduino / Project Guidance / Re: Need help with my keypad on: February 05, 2014, 12:41:51 pm
Show us a picture of your setup.
1221  Using Arduino / Project Guidance / Re: Need help with my keypad on: February 05, 2014, 12:18:32 pm
Try using the keypad library.
1222  Using Arduino / Project Guidance / Re: My 1st project: TIE Crawler from the Star Wars Expanded Universe on: February 05, 2014, 12:17:37 pm
Do you have a link?
1223  Using Arduino / Project Guidance / Re: Need help with my keypad on: February 05, 2014, 10:49:37 am
What keypad do you have, can you provide a link?
1224  Using Arduino / Project Guidance / Re: My 1st project: TIE Crawler from the Star Wars Expanded Universe on: February 05, 2014, 10:35:17 am
What tracks did you get? I won't know unless I see them. Are they lego treads?
1225  Using Arduino / Project Guidance / Re: My 1st project: TIE Crawler from the Star Wars Expanded Universe on: February 05, 2014, 09:33:54 am
It does but it takes a very long time. A lot longer than steel or iron does.
1226  Using Arduino / Project Guidance / Re: Need help in Codes for Arduino and RADius-S v1.1 on: February 05, 2014, 07:58:42 am
I would have you swap the codes but you already removed the RX pin on one of the modules.
1227  Using Arduino / Project Guidance / Re: My 1st project: TIE Crawler from the Star Wars Expanded Universe on: February 05, 2014, 07:56:23 am
No, air slits should be enough, but you should always test your components first before the final installation.
1228  Using Arduino / Project Guidance / Re: Need help in Codes for Arduino and RADius-S v1.1 on: February 04, 2014, 11:36:17 pm
You need a software serial example and combine it with a normal serial example. Actually the software serial example already has everything done for you.
1229  Using Arduino / Programming Questions / Re: number to character array on: February 04, 2014, 11:33:53 pm
Try dtostrf().
1230  Using Arduino / Programming Questions / Re: If else you else my if else question. If else question on: February 04, 2014, 11:31:07 pm
uS is time, not a distance, so why are you comparing uS to your distance?
if(uS > 100) ??? You may want to convert uS first then compare it.
Pages: 1 ... 80 81 [82] 83 84 ... 252