Show Posts
Pages: 1 ... 59 60 [61] 62 63 ... 232
901  Using Arduino / Programming Questions / Re: TFT LCD analog gauge on: February 06, 2014, 11:36:07 pm
I have Hennings old library ITDB02_Graph16, but thankfully he kept his functions all the same.

Note: this will only draw a semicircle, it will not draw a single line. To get a single line to show, you need to remember the position of the last line and draw over it with the background color. You can also use the map function instead of the FOR loop.
Code:
double pi = 3.14159265;
double x = 0; // setting variable to 0 is not needed, it will be written over anyways, it just looks good.
double y = 0;// same as above
int cx = 100; // screen coord X
int cy = 100; // screen coord Y
int rad = 50; // radius 50 pixels

for (double i = 0; i <=  pi; i += .01) {
  x = cx + sin(i + (pi/2)) * rad;
  y = cy + cos(i + (pi/2)) * rad;
  myGLCD.setColor(0,0,0);
  myGLCD.drawLine(100,100,x, y); // (cx, cy, calculated x, calculated y)
}
902  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 06, 2014, 10:04:09 pm
Thanks, and yea I did notice the templates at the bottom. It works great, the only issue I see is to get it to compile, you need to include LiquidCrystal_I2C in the sketch, even if you don't have a I2C Lcd. I'll see if there is anyway around that and if not then I'll just include it in the library folder if anyone wants to use it.

The whole point to the library was to give the option of using a Lcd or the Serial monitor and have the compiler figure out what the code needs to work.

I also made one permanent change, and that was in the Calc function. The keypad works great at 5 Volts, but if you use 3.3 Volts, the analog readings become very unstable and throws everything off. So I decided to keep it at 5 Volts.

Thanks again.
903  Using Arduino / Programming Questions / Re: Calling All Master Code Writers! on: February 06, 2014, 09:50:23 pm
I thought you were familiar with the basics? If your only storing the value when it is within the range of +-z, then you don't need an ELSE statement, unless you do want to do something if it is out of range.

if( x >= (z *-1) && x <= z) 
{
  //store value,  temp = y
}


904  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 06, 2014, 01:31:16 pm
That worked beautifully, I would have never thought of making the function virtual.

Thank you again.

The library is made to work with these keypads HERE
resistors A,B,C = 4700 ohms
resistors D,E,F = 1000 ohms

However, it will calculate the values based on the resistors used, so even if you don't use 4k7 and 1k resistors, it will adjust the code accordingly.

905  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 06, 2014, 12:41:10 pm
That still didn't work.

.cpp
Code:
template< typename T>
void OnewireKeyPad< T > :: 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(IsSameType< T, LiquidCrystal_I2C >::Value)
     port_.setCursor(0, R);

      port_.print(int(values[i]));
  port_.print(", ");
      i++;
    }
    if( IsSameType< T, LiquidCrystal_I2C >::Value == 0 )
       port_.println();
  }
}

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

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

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

void setup (){
    Serial.begin(115200);
  //lcd.init();                      // initialize the lcd
  //lcd.backlight();
  KP.Init(KEYS, 4, 3, A0); // (KeyMap, Rows, Columns, Pin)
  KP.Calc(4700, 1000, 5); //(Row value, Col value, Refference voltage)
  delay(2000);
  //lcd.clear();
}
void loop() {
  char Key = KP.Getkey();
  if(Key){
    //Serial.setCursor(0,0);
    Serial.print(KP.Key_State());
  }
  //lcd.setCursor(0,1);
    KP.LatchKP();
}

Error
Quote
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp: In member function 'void OnewireKeyPad<T>::Calc(double, double, double) [with T = Print]':
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp:166:   instantiated from here
C:\Users\Andrew\Documents\Arduino\libraries\OneWireKeypad\OneWireKeypad.cpp:63: error: 'class Print' has no member named 'setCursor'
906  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 06, 2014, 11:37:52 am
Everything but this below works.
Quote

if(port_ == lcd)   // (T == lcd) // (port_ == LiquidCrystal_I2C) //(T == LiquidCrystal_I2C)
    port_.print(); // lcd
else
   port_.println(); //serial

907  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
908  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.
909  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) { }

910  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
911  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.
912  Using Arduino / Project Guidance / Re: Need help with my keypad on: February 05, 2014, 12:18:32 pm
Try using the keypad library.
913  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?
914  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?
915  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?
Pages: 1 ... 59 60 [61] 62 63 ... 232