uint8_t flag3; //button's flag
const uint16_t p = 300; //delay eliminates button rattling
uint32_t t; //time of button's counter
const uint8_t btm = A1; // button's pin
bool btmStatus;
const uint8_t signPin =13; // indicator of testing start and correct test
float r2 = 100; //resistance of current's shunt in ohms
uint16_t i_test = 12; //test current in mA
uint16_t u_op; //reference voltage in mV
uint8_t pwmVal; //value PWM apropriate reference voltage
const uint8_t pwmPin = 9; //pin numbers
const uint8_t akkum_pin = A4; //controls akkum voltage
const uint8_t u_r2_pin = A5; //controls current's shunt voltage
const uint16_t akkumVmin = 3000; // in mV
const uint16_t tContAkk = 30000; // period of monitoring akkum voltage (ms)
const uint32_t tContr = 30000; // period of monitoring voltage controlling points (ms)
const uint8_t pwmToV = 51; // pwm to one volt
uint16_t volt; // voltage on monitoring pin in mV
bool flag = 1; // flag test's finish
bool flag2 =1; //flag correct test
uint8_t voltTol = 50; // Tolerance of shunt's voltage in mV
const uint8_t stcp = 4; //The pins connected to 74HC595
const uint8_t shcp = 7;
const uint8_t ds = 8;
const uint8_t numbs[] = {
0b11000000, 0b11111001, 0b10100100, 0b10110000,
0b10011001, 0b10010010, 0b10000010, //The codes DEC numbers
0b11111000, 0b10000000, 0b10010000
};
void setup() {
// put your setup code here, to run once:
pinMode(stcp, OUTPUT);
pinMode(shcp, OUTPUT);
pinMode(ds, OUTPUT);
pinMode(pwmPin, OUTPUT);
pinMode(akkum_pin, INPUT);
pinMode(u_r2_pin, INPUT);
pinMode(btm, INPUT_PULLUP);
pinMode(signPin, OUTPUT);
u_op = i_test * r2; //calculate reference voltage
pwmVal = pwmToV * (long)u_op / 1000;
analogWrite(pwmPin,pwmVal);
digitalWrite(signPin, LOW); // indicator of testing start
}
void loop() {
// put your main code here, to run repeatedly:
static uint16_t testDur = 0;
static uint16_t *modeInd = &testDur; //the mode displaing of information on indicator
uint16_t akkum_v;
static uint32_t timer_akkum = millis(); //check akkum voltage
if (millis() - timer_akkum > tContAkk){
akkum_v = readVoltage(akkum_pin);
timer_akkum = millis();
if(akkum_v < akkumVmin){
analogWrite(pwmPin,0);
digitalWrite(signPin, HIGH);
flag = 0;
}
}
static uint16_t dispPeriod = 1000; // pirod of voltage monitoring
static uint32_t timeDisp = 0;
// check shunt voltage value
if(millis() - timeDisp > dispPeriod){
timeDisp = millis();
volt = readVoltage(u_r2_pin);
}
if((volt < (u_op - voltTol)| volt > (u_op + voltTol)) && millis() > 30000){
flag2 = 0;
}
if(!flag2 && flag){
digitalWrite(signPin, (millis() / 500) % 2); // a led blinks if shunt voltage is incorrect
}
//checking button push and change mode displaing information
if(millis() - t > p){
btmStatus = digitalRead(btm);
if(flag3 == 0 && !btmStatus){
t = millis();
flag3 = 1;
}
if(flag3 > 0 && !btmStatus && (millis() - t) > (p + 500)){
modeInd = (modeInd == &testDur) ? &volt: &testDur;
t = millis();
flag3 += 2;
}
else if(flag3 ==1 && btmStatus && millis() - t > p) {
modeInd = (modeInd == &testDur) ? &akkum_v: &testDur;
}
if(flag3 > 0 && btmStatus){
t = millis();
flag3 = 0;
}
}
// calls function displaying of indicator
static uint32_t cTime = 0;
static uint8_t dur1num = 2; // Duration of displaying one digit in dynamic indication
if(millis() - cTime > dur1num){
if(flag) testDur = millis() / 60000;
cTime = millis();
display4_7Ind(*modeInd);
}
}
void display4_7Ind(uint16_t val) //function displaying of indicator
{
// convert voltage value to 4-digit 7-segment display
uint8_t values[4];
values[0] = val / 1000;
values[1] = val % 1000 / 100;
values[2] = val % 100 / 10;
values[3] = val % 10;
static int8_t digit = 3; //setting first digit fou indication
if (--digit < 0) digit = 3;
static uint16_t currNumber;
currNumber = (numbs[values[digit]] << 8) | (1 << digit); //splicing a DEC numbers bit and digit's bit
// trasmiting 16 bits to 74HC595
uint8_t i = 16;
while(i > 0){
i--;
uint8_t tt = bitRead(currNumber, i);
PORTB |= tt;
PORTD |= (1 << 7);
PORTD &= ~(1 << 7);
PORTB &= 0b11111110;
}
PORTD |= (1 << 4);
PORTD &= ~(1 << 4);
}
uint16_t readVoltage(uint8_t pinN)
{
uint16_t av_V = 0;
for(int8_t i = 0; i < 4; ++i){
uint16_t v = analogRead(pinN) * 5000L / 1024;
av_V += v;
}
av_V /= 4;
return av_V;
}