Hi agian,
I thought I had a working multiplexing script until I gave it an input that isnt continious... I thought I understood multiplexing but clearly there's a lot more going on with timing and updating the display constantly.
my problem:
the temp sensor is working, it reads temps accurately but I cant figure out how to keep the values displayed while the sensor is reading... when the display is reading the display just goes dark. I tried putting the reading part in an if statement that only runs every 750ms as stated in the datasheet and letting the display update constantly in the main loop. Clearly doesnt work. I need to run the writeDisplay() constantly while once in a while updating the reading value.
How is this done?
my setup:
DS18B20 temp sensor (works)
74HC595N shift reg (works)
4 digit 7 segment display (works)
arduino UNO (works)
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2); //onewire pin selection
DallasTemperature sensors(&oneWire);//dallas temp lib sensor detection
DeviceAddress tempSensor = {0x28, 0xFF, 0xE7, 0xF3, 0x75, 0x18, 0x01, 0x24}; //adress of DS18B20
//shift register interfacing 74HC595N
int DS_pin = 8;
int latch_pin = 9;
int shift_pin = 10;
//reading maniupulation
float reading;
float readingten;
int const numOfDigits = 4;
int d1 = 5; //1000
int d2 = 13; //0100
int d3 = 11; //0010
int d4 = 7; //0001
int digitPins[numOfDigits] = {d1, d2, d3, d4};
int digits[numOfDigits] = {20, 20, 20, 20};
int const numOfDecimals = 1;
int const floatPoint = numOfDigits - 1 - numOfDecimals;
//reading interval
unsigned long lastReadingTime = 0;
const int processingTime = 750; //max as specified in datasheet (depends on resolution)
int const numOfOutputs = 7;
int outputPins[numOfOutputs] = {d1, d2, d3, d4, DS_pin, latch_pin, shift_pin};
// AAAAA
// F B
// F B
// GGGGG
// E C
// E C
// DDDDD DP
//{F, A, B, G, E, D, C, DP} for every number
const byte registers[21] = {
B11101110,//0
B00100010,//1
B01111100,//2
B01110110,//3
B10110010,//4
B11010110,//5
B11011110,//6
B01100010,//7
B11111110,//8
B11110110,//9
B11101111,//0.
B00100011,//1.
B01111101,//2.
B01110111,//3.
B10110011,//4.
B11010111,//5.
B11011111,//6.
B01100011,//7.
B11111111,//8.
B11110111,//9.
B00000000//empty
};
void setup() {
for (int i = 0; i < numOfOutputs; i++) {
pinMode(outputPins[i], OUTPUT);
}
pinMode(DS_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
pinMode(shift_pin, OUTPUT);
Serial.begin(9600);
sensors.setResolution(tempSensor, 12);//set resolution
}
void setDigits() {
readingten = reading * (float)pow(10, numOfDecimals); //987.6 to 9876
digits[0] = (((int)readingten) % 10000) / 1000; //9
digits[1] = (((int)readingten) % 1000) / 100; //8
digits[2] = (((int)readingten) % 100) / 10; //7
digits[3] = (((int)readingten) % 10) / 1; //6
digits[floatPoint] = digits[floatPoint] + 10; //puts decimalpoint on the digit left from the decimals
int i = 0;
while (digits[i] == 0 && i < (numOfDigits - 1)) {
digits[i] = 20; //001.3 to 1.3 removes uneeded zeros, registe[20] is B00000000
i++;
}
}
void writeDisplay() {
for (int j = 0; j < numOfDigits; j++) {
digitalWrite(latch_pin, LOW);// opens register and locks outputs
shiftOut(DS_pin, shift_pin, LSBFIRST, registers[digits[j]]);
digitalWrite(latch_pin, HIGH);//latches from register to outputs
digitalWrite(digitPins[j], LOW); //selects digit
delayMicroseconds(500);
digitalWrite(digitPins[j], HIGH);
}
}
void loop() {
if ( (millis() - lastReadingTime) > processingTime) {
sensors.requestTemperaturesByAddress(tempSensor); //read temps
reading = sensors.getTempC(tempSensor);
lastReadingTime = millis();
}
setDigits();
writeDisplay();
}