Hello everybody, thank you to take the time to read this.
I try to read different AC voltage (5 different inputs) and control some relays.
to do so, I'm using 5 ZMPT101.
I've modified the library and everything is working fine if I names all class with specific names.
But, if I try to instance this class with a pointer to be able to scan the voltage in a loop (as you can do with an array), I'm stuck at the declaration of the class and it doesn't compile.
my code:
//InOut.h
//the target is to control Input and output
#ifndef PIN_IN || ndef PIN_OUT
#include <Arduino.h>
//#include "ZMPT101B.h"// lib AC voltage reader
//start
#define ADC_SCALE 1023.0
#define VREF 1.7
#define DEFAULT_FREQUENCY 50
//end
class InOut{
private:
byte pinin;
byte pinout;
bool state;
float u;
const int Mv0=20;
//start
float offset=0;
int zero = 512;
float sensitivity =0.0018;
float lastvalue=0;
uint8_t pin;
//end
public:
float szp=0; //set zero point
InOut(byte pinin, byte pinout);
void init();
int GetVolt();
void ON();
void OFF();
bool Status();
//start
void defzmpt(uint8_t _pin);
float calibrate();
void chgPin(uint8_t _pin);
void setZeroPoint(int _zero);
void setSensitivity(float sens);
float getVoltageDC();
float getVoltageAC(uint16_t frequency = 50);
//end
};
#endif
then InOut.cpp
#include "InOut.h"
InOut::InOut(byte pinin,byte pinout){
this->pinin = pinin;
this->pinout = pinout;
state=LOW;
// init();
}
void InOut::init(){
pinMode(pinout,OUTPUT);
pinMode(pinin,INPUT);
OFF();
// sensitivity = 0.00180;//added 27/11-0.019 origine a 0.001 trop sensible
}
void InOut::ON(){
digitalWrite(pinout, LOW);
}
void InOut::OFF(){
digitalWrite(pinout, HIGH);
}
bool InOut::Status(){
state=digitalRead(pinout);
return state;
}
int InOut::GetVolt(){
u=getVoltageAC();
u=u-10;//security offset
if(u < Mv0) {
u=0;
}
if(u<lastvalue-2 || u>lastvalue+2) lastvalue=u;
return int(lastvalue);
}
//start
void InOut::defzmpt(uint8_t _pinin) {
pinin = _pinin;
}
float InOut::calibrate() {
uint16_t acc = 0;
for (int i = 0; i < 10; i++) {
acc += analogRead(pin);
}
zero = acc / 10;
return zero;
}
void InOut::setZeroPoint(int _zero) {
zero = _zero;
}
void InOut::setSensitivity(float sens) {
sensitivity = sens;
}
float InOut::getVoltageDC() {
int16_t acc = 0;
for (int i = 0; i < 10; i++) {
acc += analogRead(pin) - zero;
}
float V = (float)acc / 10.0 / ADC_SCALE * VREF / sensitivity;
return V;
}
void InOut::chgPin(uint8_t _pin) {
pin = _pin;
sensitivity = 0.019;
}
float InOut::getVoltageAC(uint16_t frequency) {
uint32_t period = 1000000 / frequency;
uint32_t t_start = micros();
uint32_t Vsum = 0, measurements_count = 0;
int32_t Vnow;
while (micros() - t_start < period) {
Vnow = analogRead(pinin) - zero;
Vsum += Vnow*Vnow;
measurements_count++;
}
float Vrms = sqrt(Vsum / measurements_count) / ADC_SCALE * VREF / sensitivity;
return Vrms;
}
//end
and the .ino
//test des librairies inout
#include "InOut.h"
#define INP0 A0
#define INP1 A1
#define INP2 A2
#define INP3 A3
#define INP4 A4
#define INP5 A5
#define OUT0 30
#define OUT1 28
#define OUT2 26
#define OUT3 24
#define LINK 22
InOut Inout[6] ((INP0,OUT0),(INP1,OUT1),(INP2,OUT2),(NP3,OUT3),(INP4,0),(INP5,0));
/* this is working but not practicle
InOut InO1(INP0,OUT0);
InOut InO2(INP1,OUT1);
InOut InO3(INP2,OUT2);
InOut InO4(INP3,OUT3);
*/
int tempM = 10000;
int temp=0;
int n=0;
void setup(){
Serial.begin(9600);
float cal=0;
for(n=0;n=5,n++) {
Inout[n].init();
cal=Inout[n].calibrate();
Serial.println(cal);
}
/*
InO1.init();
InO2.init();
InO3.init();
InO4.init();
cal=InO1.calibrate();
Serial.println(cal);
cal=InO2.calibrate();
Serial.println(cal);
cal=InO3.calibrate();
Serial.println(cal);
cal=InO4.calibrate();
Serial.println(cal);
*/
}
void loop(){
int u;
for(n=0; n==3){
u=Inout[n].GetVolt();
if(u!=prev[n]){
Serial.println(String("A") + n + ": " + u);
prev[n]=u;
}
if(u>=200 && status==false && Inout.Status()==HIGH ){
Inout[n].ON();
status=true;
}
else {
if (Inout[n].Status()==LOW) Inout.OFF();
}
delay(500);
}
// reset status
for(n=0; n==3;n++){
if (Inout[n].Status()=LOW){
status=true;
break;
}
status=false;
}
//display bus
for (n=4; n==5;n++){
u=Inout[n].GetVolt;
Serial.println(String("B") + n + ": " + u);
delay(500);
}
}
I spent few hours looking on Google but can't find a way out, if someone can send me to the right direction.
Thank you for your help.