Hi there.
I’m having a problem with a code and a triple 7 segment display.
I am building a soldering station that displays the temperature via a triple 7 segment display.
The problem is that I am using the PID library in the same time and the display is flickering.
I tried using the TimerOne for the display, but it doesn’t have any effect. I also tried to change the PID interval to 10mS, but that didn.t help either.
#include <PID_v1.h>
byte const digits[] = {
B00111111,B00000110,B01011011,B01001111,B01100110,B01101101,B01111101,B00000111,B01111111,B01101111};
int digit_common_pins[]={A3,A4,A5}; //A3 A4 A5
int max_digits =3;
int current_digit=max_digits-1;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Define the aggressive and conservative Tuning Parameters
double aggKp=4, aggKi=0.2, aggKd=1;
double consKp=1, consKi=0.05, consKd=0.25;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
void setup()
{
DDRD = B11111111; // sets Arduino pins 0 to 7 as outputs
for (int y=0;y<max_digits;y++)
{
pinMode(digit_common_pins[y],OUTPUT);
}
myPID.SetMode(AUTOMATIC);
pinMode(11, OUTPUT);
}
void loop()
{
Input = analogRead(0);
Input = map(Input, 0, 510, 25, 350);
double Setpoint = analogRead(1);
Setpoint = map(Setpoint, 0, 1023, 150, 350);
double gap = abs(Setpoint-Input); //distance away from setpoint
if(gap<10)
{ //we're close to setpoint, use conservative tuning parameters
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
analogWrite(11, Output);
show(Input);
}
void show(int value) {
int digits_array[]={};
boolean empty_most_significant = true;
for (int z=max_digits-1;z>=0;z--)
{
digits_array[z] = value / pow(10,z);
if(digits_array[z] != 0 ) empty_most_significant = false;
value = value - digits_array[z] * pow(10,z);
if(z==current_digit)
{
if(!empty_most_significant || z==0){
PORTD = ~digits[digits_array[z]];
}
else
{
PORTD = B11111111;
}
digitalWrite(digit_common_pins[z], HIGH);
}else{
digitalWrite(digit_common_pins[z], LOW);
}
}
current_digit--;
if(current_digit < 0)
{
current_digit= max_digits;
}
}