/////////////////////////////////////////////////////////////
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <stdio.h>
#include <stdlib.h>
///////////////////////
const int sensorIn = PA7;
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
double maxValue = 0;
const int numReadings = 25;
const int ResetPin = PB11;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int currentaverage = average;
int previousaverage = 0;
float Vo;
/////////////////////
#define GAIN 5.5
#define STEP 10
byte a1[8] = {0b00000,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b00000};
byte a2[8] = {0b00000,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b00000};
byte a3[8] = {0b00000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b00000};
byte z,z0,z1;
int ur,ul,urr,ull;
/////////////////////
const int button = PB11; // GPIO 8 for the button
//const int button = PB3;
const int led = PC13; // GPIO 7 for the LED
int ledflag = 0; // LED status flag
void setup() {
lcd.begin(16, 2);
////////////////////////
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ResetPin , INPUT_PULLDOWN);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
//////////////////////
pinMode(button, INPUT_PULLDOWN); // define button as an
pinMode(led, OUTPUT); // define LED as an output
digitalWrite(led, LOW); // turn output off just in case
///////////////////
lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);
pinMode(PA6,INPUT);// A0 - аналоговый вход
pinMode(PB0,INPUT);// A1 - аналоговый вход
/////////////
}
void loop() {
/////////////////
{
Vo = currentaverage;
}
previousaverage = currentaverage;
currentaverage = average;
//*******************************************************************************
if (abs(currentaverage - previousaverage) < previousaverage / 70 ) // 100 = 1%
//********************************************************************************
{
currentaverage = previousaverage;
}
total = total - readings[readIndex];
readings[readIndex] = AmpsRMS;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
Voltage = getVPP();
VRMS = (Voltage / 2.0) * 0.707; //root 2 is 0.707
// AmpsRMS = (VRMS * 1000)/mVperAmp;
AmpsRMS = (VRMS * 1000);
/////
Serial.print(AmpsRMS);// blue
// Serial.println(" ");
Serial.print("\t");
Serial.print( currentaverage + 8);//red
Serial.print("\t");
Serial.println(average + 10);// green
//******************************************************
if (abs(Vo - currentaverage) > 3) // generator 1% changes = 12
//********************************************************
{
// digitalWrite(PB14, HIGH);
// LED = 1;
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
// digitalWrite(PB14, LOW);
//LED = 0;
digitalWrite(LED_BUILTIN, LOW);
}
/*
lcd.setCursor(0, 1);
lcd.print("V");
lcd.setCursor(1, 1);
lcd.print(AmpsRMS);
lcd.setCursor(11, 1);
//lcd.print(average);
lcd.print(Vo);// red line
*/
/*
lcd.setCursor(0, 0);
lcd.print( currentaverage);// red line
lcd.setCursor(5, 0);
lcd.print((Vo - currentaverage), 2);
*/
//////////////
urr = log(analogRead(PA6))*GAIN;
ull = log(analogRead(PB0))*GAIN;
/////////////
if (digitalRead(button) == HIGH && ledflag == 0) { ledflag = 1; lcd.clear(); delay(200);}
if (digitalRead(button) == HIGH && ledflag == 1) { ledflag = 0; lcd.clear(); delay(200);}
if (ledflag == 1) {
digitalWrite(led, HIGH);
/*
lcd.clear();
// lcd.print(" ");// fill with blanks
lcd.setCursor(0, 0);
lcd.print("hello 1");// and turn on the LED
*/
// urr = log(analogRead(PA6))*GAIN;
// ull = log(analogRead(PB0))*GAIN;
lcd.print(" ");// fill with blanks
lcd.setCursor(0,1);lcd.print("R");
lcd.setCursor(0,0);lcd.print("L");
// lcd.clear();
if(urr<ur){ur=ur-1;delay(STEP);}else{ur = urr;}
for(z=0,z0=0,z1=0;z<=ur;z++,z1++){if(z1>2){z1=0;z0++;}
if(z1==1){lcd.setCursor(z0+1,1);lcd.write((uint8_t)0);lcd.setCursor(z0+2,1);lcd.print(" ");}}
if(z1==3){lcd.setCursor(z0+1,1);lcd.write((uint8_t)1);}
if(z1==2){lcd.setCursor(z0+1,1);lcd.write((uint8_t)2);}
if(ull<ul){ul=ul-1;delay(STEP);}else{ul = ull;}
for(z=0,z0=0,z1=0;z<=ul;z++,z1++){if(z1>2){z1=0;z0++;}
if(z1==1){lcd.setCursor(z0+1,0);lcd.write((uint8_t)0);lcd.setCursor(z0+2,0);lcd.print(" ");}}
if(z1==3){lcd.setCursor(z0+1,0);lcd.write((uint8_t)1);}
if(z1==2){lcd.setCursor(z0+1,0);lcd.write((uint8_t)2);}
/////////////////////
}
else {
lcd.clear();
ledflag = 0;
digitalWrite(led, LOW);
lcd.setCursor(0, 0);
lcd.print( currentaverage);
lcd.setCursor(5, 0);
lcd.print((Vo - currentaverage), 0);
////////////////
}
delay(10);
}
//********************************
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
// while((millis()-start_time) < 1000) //sample for 1 Sec
//***************************************************************
while ((millis() - start_time) < 20) // speed
//****************************************************************
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
maxValue = readValue;
}
if (readValue < minValue)
{
minValue = readValue;
}
}
result = ((maxValue - minValue) * 5.0) / 1024.0; /// обратите внимание что STM32 12 бит вход - это 4095 уровней на 3,3 В, у Вас это для nano 5В 1024 уровня 10 бит
return result;
}