#include <TimerOne.h>
// extern const uint16_t ANALOG_OUTPUT_SPEED = 32768; // speed of the analog output in Hz
// extern const uint16_t ANALOG_OUTPUT_SPEED = 48000; // speed of the analog output in Hz
extern const uint16_t ANALOG_OUTPUT_SPEED = 10; // speed of the analog output in Hz
extern const float ANALOG_OUTPUT_MICROS = 1000000 / ANALOG_OUTPUT_SPEED; // speed of the analog output in Hz
///////////////////////////
// Define Pins
///////////////////////////
extern uint16_t analogInPin1 = A0; // attack 0 - 1,023
extern uint16_t analogInPin2 = A1; // attack 0 - 1,023
extern uint8_t oneBITPin1 = 2; // using the LSB for digital noise
extern uint8_t oneBITPin2 = 3; // using the LSB for digital noise
extern uint8_t twoBITandPin = 4; // using the LSB + LSB+1 in an AND configuration for digital noise
extern uint8_t twoBITorPin = 5; // using the LSB + LSB+1 in an OR configuration for digital noise
extern uint16_t digitalInPin1 = 6;
extern uint16_t digitalInPin2 = 7;
void setup() {
Serial.begin(115200);
Serial.println("=====================");
Serial.println(" Rusty Valve ");
Serial.println("=====================");
Serial.println(" NOISE GENERATOR ");
Serial.println("=====================");
// setup routines
setPinModes();
setupTimers();
}
///////////////////////////
// Set Pin Modes
///////////////////////////
void setPinModes() {
pinMode(analogInPin1, INPUT);
pinMode(analogInPin2, INPUT);
pinMode(oneBITPin1, OUTPUT);
pinMode(oneBITPin2, OUTPUT);
pinMode(twoBITandPin, OUTPUT);
pinMode(twoBITorPin, OUTPUT);
pinMode(digitalInPin1, INPUT);
pinMode(digitalInPin2, INPUT);
}
///////////////////////////
// Setup Timers
///////////////////////////
void setupTimers() {
Timer1.initialize(ANALOG_OUTPUT_MICROS);
Timer1.attachInterrupt(getAnalog);
Timer1.start();
}
///////////////////////////
// Timer1 Setup
///////////////////////////
void getAnalog() {
// get the analog reading
uint16_t thisRead1 = analogRead(analogInPin1); // read the analog input pin
uint16_t thisRead2 = analogRead(analogInPin2); // read the analog input pin
uint8_t thisBit1 = bitRead(thisRead1, 0); // read the LSB from analogInPin1
uint8_t thisBit2 = bitRead(thisRead2, 0); // read the LSB from analogInPin2
// Output 1-BIT Noise
digitalWrite(oneBITPin1, thisBit1); // send the LSB to oneBITPin1
digitalWrite(oneBITPin2, thisBit2); // send the LSB to oneBITPin2
Serial.print("thisBit1 = ");
Serial.println(thisBit1);
Serial.print("thisBit2 = ");
Serial.println(thisBit2);
// calc 2-BIT AND Noise
uint8_t twobitANDnoise = thisBit1 & thisBit2; // calc an AND with LSB + LSB+1
digitalWrite(twoBITandPin, twobitANDnoise); // send the AND bit
Serial.print("twobitANDnoise = ");
Serial.println(twobitANDnoise);
// calc 2-BIT OR Noise
uint8_t twobitORnoise = thisBit1 | thisBit2; // calc an OR with LSB + LSB+1
digitalWrite(twoBITorPin, twobitORnoise); // send the OR bit
Serial.print("twobitORnoise = ");
Serial.println(twobitORnoise);
}
void loop() {
///////////////////////////
// receive commands over serial
///////////////////////////
// if (Serial.available()) { // if there is data coming
// String command = Serial.readStringUntil('\n'); // read string until newline character
// if (command == "g") {
// gateON();
// }
// if (command == "h") {
// gateOFF();
// }
// }
}