Show Posts
Pages: 1 ... 80 81 [82] 83 84 ... 253
1216  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 07, 2014, 12:00:13 pm
I tried to simulate two keypads using two different Init functions and constructors and this is what I got.
Code:
#include<OWKPtest.h>

Display <Print> KP(Serial);
Display <Print> KP2(Serial);

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

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

void setup (){
  Serial.begin(115200);
  KP.Init(KEYS, 4, 3, A1); // (KeyMap, Rows, Columns, Pin)
  KP.Calc(4700, 1000);
  KP2.Init(KEYS_2, 4, 4, A1); // (KeyMap, Rows, Columns, Pin)
  KP2.Calc(4700, 1000); //(Row value, Col value)
  delay(2000);
}
void loop() {
  char Key = KP.Getkey();
  char Key2 = KP2.Getkey();

  if( Key && Key2 )
  {
    Serial.print("KP.");
    Serial.print(Key);
    Serial.print("  KP2.");
    Serial.println(Key2);
  }
}

If the Init and Calc functions are swapped from what they are now, it doesn't work correctly.

Results:
Quote
I cleaned this up.

KP:
1023, 511, 341,
179, 152, 132,
98, 89, 82,
67, 63, 59,
 
KP2:
1023, 511, 341, 255,
179, 152, 132, 117,
98, 89, 82, 76,
67, 63, 59, 56,


KP.1  KP2.1
KP.2  KP2.2
KP.3  KP2.3
KP.3  KP2.A
KP.4  KP2.4
KP.5  KP2.5
KP.6  KP2.6
KP.6  KP2.B
KP.7  KP2.7
KP.8  KP2.8
KP.9  KP2.9
KP.9  KP2.C
KP.*  KP2.*
KP.0  KP2.0
KP.#  KP2.#
KP.#  KP2.D
1217  Using Arduino / Project Guidance / Re: Arduino GPS Speedo project on: February 07, 2014, 11:31:17 am
Just the blink test file, with nothing connected to the arduino and you still the the error? Show me a picture.
1218  Using Arduino / Project Guidance / Re: Arduino GPS Speedo project on: February 07, 2014, 10:21:00 am
The both softwareSerial and regular Serial both use pins 0 and 1, this is the cause of your error. Since you can't change the normal serial pins, you need to change the software serial pins.
1219  Using Arduino / Programming Questions / Re: Arduino Uno Project - Counter does not begin from 0 on: February 07, 2014, 10:14:14 am
Quote
if (mem_a==0) { // CounterA = 0
      mem_a = 1;
      CounterA++; // CounterA = 1
      lcd.setCursor(2,0);
      lcd.print( CounterA,10 ); // Show CounterA -> 1

      lcd.setCursor(10,1);
      lcd.print( (CounterA+CounterB),10 );
    }

You did the exact same thing for CounterB.
However when you power up the arduino or upload the code, do you at anytime see 00 on your lcd?
1220  Using Arduino / Programming Questions / Re: Arduino Uno Project - Counter does not begin from 0 on: February 07, 2014, 09:49:19 am
Which counter are you referring to CounerA or B? And if you are using counters, why are you not declairing them equal to 0?

Quote
.
.
int CounterA = 0, CounterB = 0;
.
.
1221  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 07, 2014, 09:42:42 am
Ha, no the classes. Peter was talking about hold pointers to the constructor, I don't really understand what he means by that.
1222  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 07, 2014, 09:37:41 am
Right after I posted my question, I made all the global variables extern, and it works nice.

How would we make them into one class, I tried myself but failed and deleted the code.

Also, I guess the code could support multiple keypads, though i'm not sure why anyone would need more than one.
1223  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 07, 2014, 09:18:54 am
I fixed the issue, and now i'm seeing the errors for the global variables. What would be the better way to fix the global variables, structs or extern?
1224  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 07, 2014, 09:02:13 am
I restarted the IDE, same issue. So I just need to make a .cpp file ?
1225  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 07, 2014, 08:54:37 am
Code:
#ifndef OWKPtest_h
#define OWKPtest_h

#include <Arduino.h>

#define NO_KEY '\0'
#define WAITING 0
#define PRESSED 1
#define RELEASED 2
#define HELD 3
template < typename T, typename U > struct IsSameType{ enum { Value = false }; };
template < typename T > struct IsSameType< T, T > { enum { Value = true }; };
#define MAX_KEYS 16

template< typename T >
  class OWKPtest {
    public:
      OWKPtest( T &port ) :  port_( port ) {port_.println("asd"); }
      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);
    protected:
      virtual void SetCursor( int, int ) { /*Do nothing for unknown types*/ }
      T &port_;
    private:
      double values[MAX_KEYS];
      bool latchedKey[MAX_KEYS];
      char _Data[MAX_KEYS];
      char Out[20];
      uint8_t _Rows, _Cols, _Pin;
      char lastKey;
};

struct MissingType{
  void setCursor( int a, int b ){}
  template< typename T > size_t print( const T &t ){}
  template< typename T > size_t println( const T &t ){}
  template< typename T, typename U > size_t print( const T &t, const U &u ){}
  template< typename T, typename U > size_t println( const T &t, const U &u ){}   
};

#ifdef LiquidCrystal_I2C_h
  typedef LiquidCrystal_I2C LCDTYPE;
#else
  typedef MissingType LCDTYPE;
#endif

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

template< >
  class Display < LCDTYPE > : public OWKPtest< LCDTYPE >{
    public:
      using OWKPtest< LCDTYPE >::port_;
      void SetCursor( int a, int b ) { port_.setCursor( a, b ); }
      Display( LCDTYPE & lcd ) : OWKPtest< LCDTYPE >( lcd ) {} 
};

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

template< typename T >
void OWKPtest< T > :: 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;
  }
}

template< typename T >
char OWKPtest< T > :: 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;
}

template< typename T>
void OWKPtest< 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
   
      SetCursor(0, R); 
     
      port_.print(int(values[i]));
    port_.print(", ");
      i++;
    }
    if( IsSameType< T, LCDTYPE >::Value == 0 )
       port_.println( "HI" );
  }
}

template< typename T >
void OWKPtest< T > :: SetHoldTime(unsigned long setH_Time)
{
  holdTime = setH_Time;
}

template< typename T >
void OWKPtest< T > :: SetDebounceTime(unsigned long setD_Time)
{
  debounceTime = setD_Time;
}

template< typename T >
uint8_t OWKPtest< T > :: 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;

}

template< typename T >
bool OWKPtest< T > :: Readpin()
{
  return analogRead(_Pin)? 1 : 0;
}
template< typename T >
void OWKPtest<T> :: 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);
  }
}

template< typename T >
bool OWKPtest< T > :: checkLatchedKey(char _key)
{
  for(int idx = 0; idx < SIZE; idx++)
  {
    if(_key == _Data[idx])
      return latchedKey[idx];   
  }
  return false;
}
#endif
1226  Using Arduino / Programming Questions / Re: Need help referencing two classes in one constructor. on: February 07, 2014, 08:44:22 am
When I made a test .h file, and replaced every OnewireKeyPad with OWKPtest, I ran a test sketch.
Code:
#include<OWKPtest.h>

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);
  KP.Init(KEYS, 4, 3, A0); // (KeyMap, Rows, Columns, Pin)
  KP.Calc(4700, 1000); //(Row value, Col value)
  delay(2000);
}

void loop()
{
  char Key = KP.Getkey();
  if(Key){
    Serial.println(KP.Key_State());
  }
  KP.LatchKey();
}

And got this error.
Quote
test:3: error: expected constructor, destructor, or type conversion before '<' token
test.ino: In function 'void setup()':
test:16: error: 'KP' was not declared in this scope
test.ino: In function 'void loop()':
test:22: error: 'KP' was not declared in this scope

Does this make sense? I don't think its the global variable doing this, otherwise I would see them come up in the error message.
1227  Using Arduino / Programming Questions / Re: Calling All Master Code Writers! on: February 06, 2014, 11:39:12 pm
Why do you want to clear the variable after X time, why not just overwrite it when a new value is in range?

Anyway, you want to look at the example sketch blink without delay to see how things can be done after X time has passed.
1228  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)
}
1229  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.
1230  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
}


Pages: 1 ... 80 81 [82] 83 84 ... 253