Hi, I'm kinda new to working with actual programming and micro controller stuff. i'm currently working for my power meter project and find something interesting.
at first, i did my power meter project program in a straight method, put every task in my loop function, except for the initialization stuff put on the setup function.
then, after some time with some C/C++ books and repository, i want to try my program by scheduling some function call in my loop, and let the function do the job.
in first method, everything goes well (i think). and with the second one, i somehow write a sketch which couldn't even run the setup function.
i tried to check if my setup run on my arduino by put a Serial.print command on it (used to do it for debugging). but nothing goes on in my terminal.
i believe that it's not a hardware issues since i can upload my first sketch to my board and everything goes well.
so, i wonder if creating a function can somehow interrupting the arduino library from working or modifying how it works, does it matter somehow if i declare my function before or after the setup or loop function?
here's the sketches of my bugged program. the code compiled fine and returned no error messages, but return nothing on both the terminal and my LCD. it'll be a great help if anyone could point out what's wrong with my code.
#include <Key.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
//--------------------------------------------------------------------------------------------------LCD Pin setup
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//--------------------------------------------------------------------------------------------------Keypad setup
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {7, 6, 13, A5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
float Vrms, Irms, Watt, VA, Saldo = 0, MidPointV, MidPointI, freq2, PF;
int freq1;
char IOMOD = ('0');
char key;
class interval {
private:
unsigned long T, t, Interval;
public:
interval(int i) {
starttimer(i);
}
void starttimer(int j) {
Interval = j * 1000;
t = millis();
}
bool gettimerstate() {
T = millis();
if ((T - t) >= Interval) {
return true;
}
else {
return false;
}
}
};
class calc {
private:
float record, sumrecord, returnvalue;
int i, n;
void sumrec(float y) {
sumrecord += y;
}
void clearrec() {
record = 0;
sumrecord = 0;
returnvalue = 0;
n = 0;
}
public:
float getval() {
returnvalue = sqrt(sumrecord / n);
if (i = 2) {
return returnvalue;
}
if (i = 1) {
return (sumrecord / n);
}
void clearrec();
}
calc(int ii) {
const int i = ii;
}
void rec(float x) {
n++;
if (i = 1) {
record = x;
}
if (i = 2) {
record = x * x;
}
sumrec(record);
}
};
void setup() {
Serial.begin(9600);
while (!Serial);
int a = 1024, b = 1024, c = 0, d = 0;
lcd.begin(16, 2);
Serial.println("Lcd Init");
lcd.print("Calibrating");
lcd.setCursor(0, 1);
lcd.print("Please Wait");
do {
a = min(a, analogRead(1));
b = min(b, analogRead(0));
c = max(c, analogRead(1));
d = max(d, analogRead(0));
} while (millis < 5000);
MidPointI = (a + c) / 2;
MidPointV = (b + d) / 2;
Serial.println("get midpoint");
lcd.clear();
lcd.print("calibrating done");
lcd.setCursor(0, 1);
lcd.print("Starting");
Serial.println("setup done");
}
calc current(2);
calc voltage(2);
calc wattage(1);
interval mains(1);
interval FreqCalc(1);
void loop() {
Serial.println("Loop??");
sense(analogRead(0), analogRead(1));
key = keypad.getKey();
if (key != NO_KEY) {
InOut(key, IOMOD);
}
if (mains.gettimerstate() == true) {
Vrms = voltage.getval();
Irms = current.getval();
Watt = wattage.getval();
VA = Vrms * Irms;
PF = VA / Watt;
Display(1, IOMOD);
mains.starttimer(1);
}
}
void sense(int v, int i) {
float Iread, Vread, W;
Iread = ((((i - MidPointI) * (2.5 / MidPointI)) * 1000.0) / 220);
Vread = (((((v - MidPointV) * 5 * 110 * 220) / (2 * MidPointV)) / 10) / 9);
W = (Iread * Vread);
current.rec(Iread);
voltage.rec(Vread);
wattage.rec(W);
freqcalc(Vread, 1);
}
void freqcalc(float x, int a) {
float X[3];
float maxsqx = 0.0;
float tolerance, freq2buff;
int zerocount = 0;
int freq1buff;
unsigned long zerotime[2];
maxsqx = max(maxsqx, x);
tolerance = (sq(maxsqx * a / 100));
for (int i = 3; i > 1; i--) {
X[i - 1] = X[i - 2];
}
X[0] = x;
if (sq(X[1]) < tolerance) {
if ((X[0] < 0) && (sq(X[0]) > tolerance) && (X[2] > 0) && (sq(X[2]) > tolerance)) {
zerocount++;
zerotime[1] = zerotime[0];
zerotime[0] = millis();
}
}
freq2buff = 1000 / (zerotime[1] - zerotime[0]);
if (FreqCalc.gettimerstate() == true) {
freq1 = zerocount;
freq2 = freq2buff;
zerocount = 0;
FreqCalc.starttimer(1);
}
}
void InOut(char in, char imod) {
int n;
unsigned long Sald;
switch (imod) {
case ('#'):
if (in = '#') {
Saldo += Sald;
IOMOD = ('0');
Sald = 0;
}
else if (in = '*') {
Sald = Sald / 10;
lcd.setCursor(n - 1, 1);
lcd.print(" ");
n--;
}
else {
Sald += (in - ('0'));
n++;
}
lcd.setCursor(n, 1);
break;
default:
IOMOD = in;
break;
}
}
void Display(int i, char dmod) {
int line = i;
if (line = 0) {
lcd.clear();
switch (dmod) {
case ('0'):
lcd.print("Balance Left");
lcd.setCursor(0, 1);
break;
case ('1'):
lcd.print("Voltage");
lcd.setCursor(15, 1);
lcd.print("V");
lcd.setCursor(0, 1);
break;
case ('2'):
lcd.print("Current");
lcd.setCursor(15, 1);
lcd.print("A");
lcd.setCursor(0, 1);
break;
case ('3'):
lcd.print("Real Power (P)");
lcd.setCursor(15, 1);
lcd.print("W");
lcd.setCursor(0, 1);
break;
case ('4'):
lcd.print("ApparentPower(S)");
lcd.setCursor(14, 1);
lcd.print("VA");
lcd.setCursor(0, 1);
break;
case ('5'):
lcd.print("Frequency (n)");
lcd.setCursor(14, 1);
lcd.print("Hz");
lcd.setCursor(0, 1);
break;
case ('6'):
lcd.print("Frequency (t)");
lcd.setCursor(14, 1);
lcd.print("Hz");
lcd.setCursor(0, 1);
break;
case ('7'):
lcd.print("Power Factor");
lcd.setCursor(0, 1);
break;
case ('*'):
lcd.print("gerdi.akbar");
lcd.setCursor(0, 1);
lcd.print("@student.upi.edu");
break;
case ('#'):
lcd.print("Input Balance");
lcd.setCursor(0, 1);
break;
default:
lcd.print("No output mode");
break;
}
}
else if (line = 1) {
switch (dmod) {
case ('0'):
lcd.print(Saldo);
break;
case ('1'):
lcd.print(Vrms);
break;
case ('2'):
lcd.print(Irms);
break;
case ('3'):
lcd.print(Watt);
break;
case ('4'):
lcd.print(VA);
break;
case ('5'):
lcd.print(freq1);
break;
case ('6'):
lcd.print(freq2);
break;
case ('7'):
lcd.print(PF);
break;
case ('#'):
lcd.print(key);
break;
default:
//do nothing
break;
}
}
}
any guidance will be appreciated.
regards