My library doesn't work and I don't know why

So I made a library for using a 4x4 switch matrix, however it doesnt work. like it starts executing

cckeypad cckeypad(y,x);

but it stops in the middle of it and doesnt do anything after. Here is the whole code:

#include <cckeypad.h>
int x[4]={5,4,3,2};
int y[4]={6,7,8,9};
cckeypad cckeypad(y,x);
void setup() {
  Serial.begin(9600);
  delay(1000);
}

void loop() { 
    cckeypad.print();
 }


and here is the cckeypad.cpp

#include "Arduino.h"
#include "cckeypad.h"

cckeypad::cckeypad(int row[4], int col[4]){
	Serial.begin(9600);
	delay(100);
	Serial.println("HI");
	for(int i=0;i!=4;i++){
		pinMode(col[i], OUTPUT);
		Serial.print("S");
		pinMode(row[i], INPUT_PULLUP);
		Serial.print("e");
		row1[i]=row[i];
		Serial.print("t");
		col1[i]=col[i];
		Serial.print("u");
		digitalWrite(col1[i], HIGH);
		Serial.print("p #");
		Serial.println(i);
	}
}
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);
	}
}
cckeypad::print() {
	
	check();
	if(ans[0]!=0||ans[1]!=0){
		Serial.println(ans[0]*ans[1]);
		
	}
	
}

and cckeypad.h

#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

Shouldn't these have return types?

I think that that is not the correct way to pass an array to a function if that is what you want to do. See here.

Hi, what do you mean by return types?

A function (except constructor or destructor) should always have a return type, even if it is void.
Or so I thought.

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);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.