#include "LedControlMS.h"
#include <Arduino.h>
#define CURRENT_SENSOR A0 // Analog input pin that sensor is attached to
#define VOLTAGE_SENSOR A1 // Analog input pin that sensor is attached to
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn pin 1 of MAX7219
pin 11 is connected to the CLK pin 13 of MAX7219
pin 10 is connected to LOAD pin 12 of MAX7219
We have only a single MAX72XX.*/
LedControl lc=LedControl(12,11,10,1);
byte dp0=0,dp1=0,dp2=0,dp3=0,dp4=0,dp5=0;
unsigned int dv1 = 0, dv3 = 0;
float dv = 0, dv2 = 0;
float amplitude_current; //amplitude current
float effective_value; //effective current
int ones, tens, hundreds,ones1, tens1, hundreds1,thousands1;
byte dp0=0,dp1=0,dp2=0,dp3=0,dp4=0,dp5=0;
void setup()
{
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,15);
/* and clear the display */
lc.clearDisplay(0);
pinMode(CURRENT_SENSOR, INPUT);
pinMode(VOLTAGE_SENSOR, INPUT);
}
void Volts_Reading(void)
{
unsigned int adc1 = analogRead(VOLTAGE_SENSOR);
dv2 = adc1;
if(dv2 >= 0 && dv2 <= 9)
{
dv3 = dv2 * 100;
dp3 = 1;
dp4 = 0;
dp5 = 0;
ones1 = dv3%10;
tens1 = (dv3/10)%10;
hundreds1 = (dv3/100)%10;
lc.setDigit(0,3,(byte)hundreds1,dp3);
lc.setDigit(0,4,(byte)tens1,dp4);
lc.setDigit(0,5,(byte)ones1,dp5);
}
else if(dv2 >= 10 && dv2 <= 99)
{
dv3 = dv2 * 10;
dp3 = 0;
dp4 = 1;
dp5 = 0;
ones1 = dv3%10;
tens1 = (dv3/10)%10;
hundreds1 = (dv3/100)%10;
lc.setDigit(0,3,(byte)hundreds1,dp3);
lc.setDigit(0,4,(byte)tens1,dp4);
lc.setDigit(0,5,(byte)ones1,dp5);
}
else if(dv2 >= 100 && dv2 <= 999)
{
dv3 = dv2 * 1;
dp3 = 0;
dp4 = 0;
dp5 = 0;
ones1 = dv3%10;
tens1 = (dv3/10)%10;
hundreds1 = (dv3/100)%10;
lc.setDigit(0,3,(byte)hundreds1,dp3);
lc.setDigit(0,4,(byte)tens1,dp4);
lc.setDigit(0,5,(byte)ones1,dp5);
}
else if(dv2 > 999)
{
lc.setChar(0,3,'-',false);
lc.setChar(0,4,'-',false);
lc.setChar(0,5,'-',false);
}
}
void Amps_Reading(void)
{
int sensor_max;
sensor_max = analogRead(CURRENT_SENSOR);
amplitude_current=(float)(sensor_max-512)/1024*5/66*1000000; // for 5A mode,you need to modify this with 20 A and 30A mode;
effective_value=(amplitude_current/1.414); // 05A = 185
dv = effective_value/1000; //Divide by 1000
if(dv >= 0 && dv <= 9)
{
dv1 = dv * 100;
dp0 = 1;
dp1 = 0;
dp2 = 0;
ones = dv1%10;
tens = (dv1/10)%10;
hundreds = (dv1/100)%10;
lc.setDigit(0,0,(byte)hundreds,dp0);
lc.setDigit(0,1,(byte)tens,dp1);
lc.setDigit(0,2,(byte)ones,dp2);
}
else if(dv >= 10 && dv <= 99)
{
dv1 = dv * 10;
dp0 = 0;
dp1 = 1;
dp2 = 0;
ones = dv1%10;
tens = (dv1/10)%10;
hundreds = (dv1/100)%10;
lc.setDigit(0,0,(byte)hundreds,dp0);
lc.setDigit(0,1,(byte)tens,dp1);
lc.setDigit(0,2,(byte)ones,dp2);
}
else if(dv >= 100 && dv <= 999)
{
dv1 = dv * 1;
dp0 = 0;
dp1 = 0;
dp2 = 0;
ones = dv1%10;
tens = (dv1/10)%10;
hundreds = (dv1/100)%10;
lc.setDigit(0,0,(byte)hundreds,dp0);
lc.setDigit(0,1,(byte)tens,dp1);
lc.setDigit(0,2,(byte)ones,dp2);
}
else if(dv > 999)
{
lc.setChar(0,0,'-',false);
lc.setChar(0,1,'-',false);
lc.setChar(0,2,'-',false);
}
}
void loop()
{
Volts_Reading();
Amps_Reading();
}