#ifndef cckeypad_h
#define cckeypad_h
#include "Arduino.h"
class cckeypad {
public:
cckeypad(int row[4], int col[4]);
print();
check();
private:
int row1[4];
int col1[4];
int ans[2];
};
#endif
There is no error message, it just stops executing anything after about 100ms. The board itself is fine, I tested it with simple blink example and it works. Do you have any ideas why?
btw. if it wasn't obvious, im still a noob when it comes to programming
One possible issue is that you are calling Arduino library functions (pinMode(), digitalWrite(), Serial.print()) from the object constructor. The Arduino system isn't initialized until AFTER the global constructors. You should move that stuff to a .begin() method to call from setup().
The code is flooding the serial line with messages so that might be causing problems.
Here is the code after cleaning. See if this works longer than the original version. If it works OK you can split it back into cckeypad.h, cckeypad.cpp, and cckeypad.ino.
class cckeypad
{
public:
cckeypad(int row[4], int col[4]);
void begin();
void print();
void check();
private:
int row1[4];
int col1[4];
int ans[2];
};
cckeypad::cckeypad(int row[4], int col[4])
{
for (int i = 0; i != 4; i++)
{
row1[i] = row[i];
col1[i] = col[i];
}
}
void cckeypad::begin()
{
Serial.println("HI");
for (int i = 0; i != 4; i++)
{
pinMode(col1[i], OUTPUT);
Serial.print("S");
pinMode(row1[i], INPUT_PULLUP);
Serial.print("e");
Serial.print("t");
Serial.print("u");
digitalWrite(col1[i], HIGH);
Serial.print("p #");
Serial.println(i);
}
}
void cckeypad::check()
{
for (int c = 0; c != 4; c++)
{
digitalWrite(col1[c], LOW);
for (int r = 0; r != 4; r++)
{
if (digitalRead(row1[r]) == LOW)
{
ans[0] = r + 1;
ans[1] = c + 1;
// Serial.println("check done");
}
else
{
ans[0] = 0;
ans[1] = 0;
// Serial.println("check done, negative");
}
}
digitalWrite(col1[c], HIGH);
}
}
void cckeypad::print()
{
check();
if (ans[0] != 0 || ans[1] != 0)
{
Serial.print(ans[0]);
Serial.println(ans[1]);
}
}
int x[4] = {5, 4, 3, 2};
int y[4] = {6, 7, 8, 9};
cckeypad cckeypad(y, x);
void setup()
{
Serial.begin(115200);
delay(1000);
cckeypad.begin();
}
void loop()
{
cckeypad.print();
delay(500);
}