/*

  ############################################
  ||                                        ||
  ||                 Title:                 ||
  ||          Integer Serial Read           ||
  ||           Display, and Tone            ||
  ||                Feedback                ||
  ||                                        ||
  ||                 Name:                  ||
  ||                 GC1CEO                 ||
  ||                                        ||
  ||            Date: 9/20/2024             ||
  ||                                        ||
  ||              Description:              ||
  || A custom function sends out a pattern  ||
  ||of high and low tones to indicate 0s and||
  ||1s any time an integer from 0 to 255 is ||
  ||          inputted via serial.          ||
  ||                                        ||
  ||  A button also exists to repeat back   ||
  ||      the number's tones.      ||
  ||                                        ||
  ############################################

*/


#include "pitches.h"
#include <toneAC.h>

//Pin connected to ST_CP (Storage Clock / Latch) of 74HC595
int latchPin = 8;
//Pin connected to SH_CP (Shift Clock) of 74HC595
int clockPin = 12;
////Pin connected to DS / SER (Data/Serial Input) of 74HC595
int dataPin = 11;
////Pin connected to inverted MR / SRCLR (Master Reset / Serial Register Clear) of 74HC595
int resetPin = 6;
////Pin connected to inverted OE (Output Enable) of 74HC595
int oePin = 7;

// Pin used for the button
int toneButton = 2;

// Whatever the button has been pressed
bool buttonPressed = false;



// The number incoming to toneOut/shiftOut as well as the previous one
int incomingNum = 0;
int incomingNum_last = 0;

// the inputted number as a string and its converted binary value as a string
String inputNum;
String inputBin;

// The current and previous states of the button

int buttonState = LOW;
int lastButtonState = LOW;


// previous time since last button press
unsigned long lastDebounceTime = 0;

// delay (in ms) before button press can be registered again
unsigned long debounceDelay = 50;

// the notes for the high note to indicate a 1 and low note to indicate a 0 as set by pitches.h
int highNote = NOTE_C4;
int lowNote = NOTE_C2;

void setup() {

  Serial.begin(9600);

  //set pins to output so you can control the shift register and setting button and pot pins to INPUT
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(resetPin, OUTPUT);
  pinMode(oePin, OUTPUT);


  // Setting reset to HIGH to stop reset and setting output enable to LOW to always enable output
  digitalWrite(resetPin, HIGH);
  digitalWrite(oePin, LOW);

  // Cleaning up serial monitor and announcing program has initalized and going to loop
  for (int a = 0; a < 25; a++) {
    Serial.println("");
  }
  Serial.println("Please enter a number (from 0 to 255).");
}
void loop() {
  int reading = digitalRead(toneButton);

  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if (Serial.available() > 0) {
    inputNum = Serial.readString();
    incomingNum = inputNum.toInt();
    inputBin = String(incomingNum, BIN);
    
  }

  if (millis() - lastDebounceTime > debounceDelay) {

    if (digitalRead(toneButton) == HIGH) {
      buttonPressed = true;
      toneOut(MSBFIRST, incomingNum_last, 500);
      buttonPressed = false;
      
    }

    if (reading != buttonState) {
      buttonState = reading;
    }

    digitalWrite(latchPin, LOW);
    if (incomingNum != incomingNum_last) {
      shiftOut(dataPin, clockPin, MSBFIRST, incomingNum);
      toneOut(MSBFIRST, incomingNum, 500);
      digitalWrite(latchPin, HIGH);
      incomingNum_last = incomingNum;
    }
  }
 
}

void toneOut(uint8_t bitOrder, uint8_t val, int delayTime)
{
  if (bitOrder == LSBFIRST) {
    for (int i = 0; i < 8; i++)  {

      Serial.print(bitRead(val, i));
      delay(delayTime);
      if (bitRead(val, i) == 0) {
        toneAC(lowNote, 10, 500, false);
        delay(delayTime);
        toneAC(0);
      } else {
        toneAC(highNote, 10, 250, false);
        delay(delayTime);
        toneAC(0);
      }


    }
  } else {
    for (int i = 7; i >= 0; i--)  {

      Serial.print(bitRead(val, i));
      delay(delayTime);
      if (bitRead(val, i) == 0) {
        toneAC(lowNote, 10, 500, false );
        delay(delayTime);
        toneAC(0);
      } else {
        toneAC(highNote, 10, 250, false);
        delay(delayTime);
        toneAC(0);
      }

    }
  }


Serial.println();
}



$abcdeabcde151015202530354045505560fghijfghij
74HC595