In the below code I am trying to make my temperature sensor (and soon a light sensor) to go through a 16 pin shift register (SN74HC595N) to a 4 digit 7 segment led display and the correct temperature value in degrees C (and F later). I know my circuitry and pin values are correct because I made some basic code to check my display. However, I am having issues when controlling individual digits of my display, I can get them all to display the same number or character but not different ones.
This code was based off of another person's arduino project, but currently the below code outputs FFFF on my display.
int latchPin = 8;
int clockPin = 12; //Pin connected to SH_CP
int dataPin = 11; //Pin connected to DS
int digit1Pin = 2;
int digit2Pin = 3;
int digit3Pin = 4;
int digit4Pin = 5;
byte data;
byte dataArray[13];
int tempPin = 0; // the thermistor and 4,7k resistor
int sensorPin = 3; // light sensor pin 0-1023 scale
int tempread = 0;
int temp;
const int MINUS_IDX = 10;
const int CELCIUS_IDX = 11;
const int FARENHEIT_IDX = 12;
void setup(){
pinMode(digit1Pin, OUTPUT);
pinMode(digit2Pin, OUTPUT);
pinMode(digit3Pin, OUTPUT);
pinMode(digit4Pin, OUTPUT);
Serial.begin(9600);
dataArray[0] = B01110111; //119; Corresponding binary to number value is represented
dataArray[1] = B00100100; //36;
dataArray[2] = B01011101; //93;
dataArray[3] = B01101101; //109;
dataArray[4] = B00101110; //46;
dataArray[5] = B01101011; //107;
dataArray[6] = B01111011; //123;
dataArray[7] = B00100101; //37;
dataArray[8] = B01111111; //127;
dataArray[9] = B00101111; //47;
//temperature specific characters
dataArray[CELCIUS_IDX] = B00011011; //83; // C
dataArray[FARENHEIT_IDX] = B01010011; //27; // F
}
void loop(){
int lightLevel = 0;
//LED and photoresistor part
lightLevel = analogRead(sensorPin); //number can range between 0 (0 Volts) and 1023 (5 Volts)
tempread = analogRead(tempPin);
tempread = (analogRead(tempPin));
setTemp(temp, 'C');
// add settemp for f here, mode button toggles which one (if/else statement)
}
void setTemp(int temp, char scale){
//temp must be between -99 and 999 in either scale to fit the display
//put in a check here later
if (scale == 'F'){
setDigit(digit1Pin, FARENHEIT_IDX);
temp = (tempread * 5) * (9/5) + 32.0; // converts degrees Celsius to Fahrenheit.
} else if (scale == 'C'){
setDigit(digit1Pin, CELCIUS_IDX);
temp = tempread * 5; // converts the voltage to degrees Celsius.
}
setDigit(digit2Pin, temp % 10);
temp /= 10;
if (temp >= 1){
setDigit(digit3Pin, temp % 10);
temp /= 10;
if (temp >= 1){
setDigit(digit4Pin, temp % 10);
}
}
}
void setDigit(int digitPin, int value){
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, dataArray[value]);
digitalWrite(latchPin, 1);
digitalWrite(digitPin, HIGH);
delay(1);
digitalWrite(digitPin, LOW);
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
I reached the max character length, so in the below post I will continue with my own code that is giving me a different problem but perhaps an easier fix.