#include <TimerOne.h>
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 oneBITPin = 2; // using the LSB for digital noise
extern uint8_t oneBITSelectorPin = 3; // 0V = analogInPin1, 5V = analogInPin2
extern uint8_t twoBITPin = 4; // using the LSB of both inputs in a logic calculation for digital noise
extern uint8_t twoBITSelectorPin = A2; // 0V = AND, 2.5V = OR, 5V = XOR
void setup() {
Serial.begin(115200);
Serial.println("=====================");
Serial.println(" Rusty Valve ");
Serial.println("=====================");
Serial.println(" NOISE GENERATOR V2 ");
Serial.println("=====================");
// setup routines
setPinModes();
setupTimers();
}
///////////////////////////
// Set Pin Modes
///////////////////////////
void setPinModes() {
pinMode(analogInPin1, INPUT);
pinMode(analogInPin2, INPUT);
pinMode(oneBITPin, OUTPUT);
pinMode(oneBITSelectorPin, INPUT);
pinMode(twoBITPin, OUTPUT);
pinMode(twoBITSelectorPin, 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);
uint16_t thisRead2 = analogRead(analogInPin2);
uint8_t thisBit1 = bitRead(thisRead1, 0); // read just the LSB from analogInPin1
uint8_t thisBit2 = bitRead(thisRead2, 0); // read just the LSB from analogInPin2
///////////////////////////
// Output 1-BIT Noise
///////////////////////////
uint8_t thisRead3 = digitalRead(oneBITSelectorPin);
if (thisRead3) {
digitalWrite(oneBITPin, thisBit2); // send the LSB to oneBITPin2
Serial.print("thisBit2 = ");
Serial.println(thisBit2);
} else {
digitalWrite(oneBITPin, thisBit1); // send the LSB to oneBITPin1
Serial.print("thisBit1 = ");
Serial.println(thisBit1);
}
///////////////////////////
// Output 2-BIT Noise
///////////////////////////
uint16_t thisRead4 = analogRead(twoBITSelectorPin);
uint16_t twoBITselection = map(thisRead4, 0, 1023, 0, 2);
// calc 2-BIT AND Noise
if (twoBITselection == 0) {
uint8_t twobitANDnoise = thisBit1 & thisBit2; // calc an AND with LSB + LSB+1
digitalWrite(twoBITPin, twobitANDnoise); // send the AND bit
Serial.print("twobitANDnoise = ");
Serial.println(twobitANDnoise);
}
// calc 2-BIT OR Noise
if (twoBITselection == 1) {
uint8_t twobitORnoise = thisBit1 | thisBit2; // calc an OR with LSB + LSB+1
digitalWrite(twoBITPin, twobitORnoise); // send the OR bit
Serial.print("twobitORnoise = ");
Serial.println(twobitORnoise);
}
// calc 2-BIT XOR Noise
if (twoBITselection == 2) {
uint8_t twobitXORnoise = thisBit1 ^ thisBit2; // calc an OR with LSB + LSB+1
digitalWrite(twoBITPin, twobitXORnoise); // send the XOR bit
Serial.print("twobitXORnoise = ");
Serial.println(twobitXORnoise);
}
}
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();
// }
// }
}