Hi guys
Having a problem with display flickering using this code:
#include <Wire.h>
#include "Adafruit_MCP23017.h"
// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!
// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)
// Define MCP23017 pins connected to the LED display
// Digits
byte digits[4] = {8,9,10,11};
// Segments A-G and DP.
// DP (pin 2) will always be lit on the 2nd digit.
byte segments[8] = {7,5,3,1,0,6,4,2};
// define numbers, from SEGMENT A-G
byte numbers[10][7] = {
{HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW}, // zero
{LOW,HIGH,HIGH,LOW,LOW,LOW,LOW}, // one
{HIGH,HIGH,LOW,HIGH,HIGH,LOW,HIGH}, // two
{HIGH,HIGH,HIGH,HIGH,LOW,LOW,HIGH}, // three
{LOW,HIGH,HIGH,LOW,LOW,HIGH,HIGH}, // four
{HIGH,LOW,HIGH,HIGH,LOW,HIGH,HIGH}, // five
{HIGH,LOW,HIGH,HIGH,HIGH,HIGH,HIGH}, // six
{HIGH,HIGH,HIGH,LOW,LOW,LOW,LOW}, // seven
{HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH}, // eight
{HIGH,HIGH,HIGH,HIGH,LOW,HIGH,HIGH} // nine
};
// holds the value of the current measured voltage
float voltage = 10.98;
Adafruit_MCP23017 mcp;
void setup() {
mcp.begin(); // use default address 0x20
// Set all required MCP23017 pins to output
// Digits
for (byte digit = 0; digit < 4; digit++) {
mcp.pinMode(digits[digit], OUTPUT);
mcp.digitalWrite(digits[digit], LOW);
}
// Segments
for (byte segment = 0; segment < 8; segment++) {
mcp.pinMode(segments[segment], OUTPUT);
mcp.digitalWrite(segments[segment], LOW);
}
}
void loop() {
displayNumber(voltage);
}
/*
* Light a number in the display
* @params: float number
*/
void displayNumber(float number) {
char outstr[5];
int pos = 0;
int c, n, nbr;
dtostrf(number,5, 2, outstr);
// iterate through all chars in the array
for (c = 0; c < 5; c++) {
if (isDigit(outstr[c])) {
nbr = outstr[c]-'0';
// light the segments that form the number
for (n = 0; n < 7; n++) {
mcp.digitalWrite(segments[n], numbers[nbr][n]);
}
if (pos==1) {
mcp.digitalWrite(segments[7], HIGH);
}
else {
mcp.digitalWrite(segments[7], LOW);
}
// turn on the corresponding digit
mcp.digitalWrite(digits[pos], HIGH);
delay(5);
mcp.digitalWrite(digits[pos], LOW);
pos++;
}
}
}
/*
* Turn off all segments in the display
*/
void turnAllOff() {
for (int n=0; n<7; n++) {
mcp.digitalWrite(segments[n], LOW);
}
}
Basically it sets the necessary segments ON and lights the digit, waits for 5ms, turns off the digit and moves on to the next.
Although the logic looks sound to me I'm experiencing heavy flickering in the display.
Little help on how to overcome this would surely be appreciated
Thanks in advance,
Pedro