/* 

  - See Fritzing file: temp_trending2.fzz
    Shows layout for two shift registers and 16 leds

  - See: thermister_test_using_if_else_statement.ino
  - See: Wokwi.com for simulation program
  - See 'thermister_display_if_else.c' in VS_Code / AVR Programs folder for reference.

  Try using ATTiny 85

  Option: Build sketch using C to possibly meet ATTiny13 1k flash size.

  Option: Build sketch with low power (battery) in mind. Have sleep involked after a preset
    amount of time. Use a pushbutton as an interrupt to wake from sleep, stay away for several
    minuts and then go back into sleep mode. Or have two different buttons, one for sleep
    mode and a second for continuous on mode.

  Option: Replace 10k ohm resistor with a potentiometer.
    When resetting to midpoint use the potentiometer to zero the leds to have only yellow lit.
    This way you can manually adjust the value to 500 range.

  !! NOTE !! shiftout uses: shiftOut(dataPin, clockPin, bitOrder, value)
    dataPin: the pin on which to output each bit. Allowed data types: int.
    clockPin: the pin to toggle once the dataPin has been set to the correct value. Allowed data types: int.
    bitOrder: which order to shift out the bits; either MSBFIRST or LSBFIRST.
    value: the data to shift out. Allowed data types: byte.
*/

#include <TinyDebug.h> // Use as you would Serial.begin to debug 

const byte analogIn = A1;
const byte clockPin = 4; // Use pin 12, SPI - MISO
const byte latchPin = 3; // Use pin 8
const byte dataPin = 1; // Use pin 11, SPI - MOSI

const byte numReadings = 20;
int readings[numReadings]; // array of readings from analog input
int readTotal = 0;
int readAverage = 0;
byte index;

int adcValue;
int setValue;


void setup() {
  Debug.begin();
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  //initialize all ADC readings to 0:
  for (index = 0; index < numReadings; index++) {
    readings[index] = 0;
  }

  // read analog input a set number of times and obtain a total of all readings
  for (index = 0; index < numReadings; index++) {
    adcValue = analogRead(analogIn);
    readings[index] = adcValue;
    readTotal = readTotal + readings[index];
    Debug.print("readings["); Debug.print(index); Debug.print("] = "); Debug.println(readings[index]);
    delay(100);
  }

  // calculate the average of all analog input readings
  readAverage = readTotal / numReadings;
  setValue = readAverage;
}

void loop() {

  adcValue = analogRead(analogIn);
  Debug.print("adc = "); Debug.print(adcValue);
  Debug.print(" set = "); Debug.println(setValue);

  // Try changing this code to have two 8 bit numbers instead of 16 bit numbers

  if (adcValue == setValue) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, MSBFIRST, 0b00000000); // this commands dataPin to sync with clockPin, send least significant bit first.
    shiftOut(dataPin, clockPin, MSBFIRST, (0b00000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue + 1) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000010000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000010000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue + 2) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000011000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000011000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue + 3) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000011100000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000011100000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue + 4) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000011110000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000011110000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue + 5) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000011111000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000011111000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue + 6) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000011111100);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000011111100 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue + 7) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000011111110);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000011111110 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue >= setValue + 8) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000011111111);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000011111111 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue - 1) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000000100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000000100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue - 2) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000001100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000001100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue - 3) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000011100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000011100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue - 4) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0000111100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0000111100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue - 5) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0001111100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0001111100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue - 6) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0011111100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0011111100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue == setValue - 7) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b0111111100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b0111111100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }

  else if (adcValue <= setValue - 8) {
    digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, LSBFIRST, 0b1111111100000000);
    shiftOut(dataPin, clockPin, LSBFIRST, (0b1111111100000000 >> 8));
    digitalWrite(latchPin, HIGH);
  }


  delay(200);

}
ATTINY8520PU
74HC595
74HC595