#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define LED_PIN 13
#define POT_DUTY A0
#define POT_PERIOD A1
#define SDA_PIN A5.2
#define SLC_PIN A4.2
#define BUTTON_DISRUPT 2
int period = 100;
int pWidth = 0;
int nWidth = 0;
int potDutyVal = 0;
int potPeriod = 0;
byte glitchTime = 10;
bool state = HIGH;
//********************************************************************
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_DISRUPT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_DISRUPT), disrupt, CHANGE);
LCD.init();
LCD.backlight();
}
//********************************************************************
void loop() {
potPeriod = analogRead(POT_PERIOD); // 0 to 1023
period = map(potPeriod,0,1023,0,500); //val, fl, fH ,tL,TH nWidth
potDutyVal = analogRead(POT_DUTY); // 0 to 1023
pWidth = map(potDutyVal,0,1023,0,period); //val, fl, fH ,tL,TH nWidth
nWidth = period - pWidth;
String out = String(period) + " " + String(pWidth) + " " + String (nWidth);
String out2 = String(period) + " " + String(1.0 / float(period / 1000.0)) + " " + String(float(pWidth)/float(period) * 100);
serialOut(out);
lcdOut(out2);
pulseOut();
}
void pulseOut(){
digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(pWidth); // wait for a second
digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW
delay(nWidth); // wait for a second
}
void disrupt() {
serialOut("sfgsfdgsdfgsdfg");
digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(glitchTime); // wait for a second
digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW
delay(glitchTime);
}
void serialOut(String out){
Serial.println(out);
}
void lcdOut(String out){
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("PER FRQ +");
LCD.setCursor(0, 1);
LCD.print(out);
}