#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);// pins RS,E,D4,D5,D6,D7
float frequency=1;
float pulsewidth=50;// default 1s (1 Hz) pe, pulse width 50%
unsigned long prMillis = 0;
float hightime;
float lowtime;
bool flag = false;
bool ledState = HIGH;
int lcdupdate;
String mystring1;
const byte PinButs [] = { 9, 10, 11, 12 };
const byte PinOut = 8;
// -----------------------------------------------------------------------------
// monitor multiple buttons, return index of pressed buttor or NoBut
#define Nbut sizeof(PinButs)
byte butState [Nbut];
enum { Finc, Fdec, Pinc, Pdec }; // button indices
const int NoBut = -1;
// -------------------------------------
int
chkButtons ()
{
for (unsigned n = 0; n < Nbut; n++) {
byte but = digitalRead (PinButs [n]);
if (butState [n] != but) {
butState [n] = but;
delay (10); // debounce
if (LOW == but)
return n;
}
}
return NoBut;
}
//void setup(){
// Serial.begin(9600);
// prMillis = millis();
// flag = true;
// lcd.begin(16, 2);// LCD 16X2
// pinMode(8, OUTPUT);
// pinMode(9,INPUT);
// pinMode(10,INPUT);
// pinMode(11,INPUT);
// pinMode(12,INPUT);
// pwm();
// lcdcontrol();
//}
void setup ()
{
//Serial.begin (9600);
Serial.begin(9600);
prMillis = millis();
flag = true;
lcd.begin (16, 2);// LCD 16X2
for (unsigned n = 0; n < Nbut; n++) {
pinMode (PinButs [n], INPUT_PULLUP);
butState [n] = digitalRead (PinButs [n]);
}
// digitalWrite (PinOut, Off);
pinMode (PinOut, OUTPUT);
pwm();
lcdcontrol();
}
void loop ()
{
// msec = millis ();
pwm ();
// monitor button presses
switch (chkButtons ()) {
case Fdec:
if (0 < frequency)
frequency -= 0.1;
lcdupdate = 1;
break;
case Finc:
frequency += 0.1;
lcdupdate = 1;
break;
case Pdec:
if (0 < pulsewidth)
pulsewidth--;
lcdupdate = 1;
break;
case Pinc:
if (100 > pulsewidth)
pulsewidth++;
lcdupdate = 1;
break;
}
if(lcdupdate==1){ //if button is pressed, run pwm function and then update lcd. if pwm isn't run than the high and low time doesn't correctly update on the lcd, but I'm not sure why...
pwm();
lcdcontrol();
lcdupdate = 0;
}
}
void pwm(){
hightime = (1/frequency)*1000*pulsewidth/100; //convert Hz to ms to get the period and then multiply it by the pulse width as a percentage to get "high" time
lowtime = ((1/frequency)*1000)-hightime; // period - "high" time = "low" time.
if (flag == true){
if (millis() - prMillis >= hightime)//pwm high state is over
{
ledState = !ledState;
digitalWrite(8, ledState);
flag = false;
prMillis = millis();
Serial.print("hightime=");
Serial.print(hightime);
Serial.print("lowtime=");
Serial.print(lowtime);
}
}
else{
if (millis() - prMillis >= lowtime)//pwm low state is over
{
ledState = !ledState;
digitalWrite(8, ledState);
flag = true;
prMillis = millis();
}
}
}
void lcdcontrol(){
lcd.setCursor(0,0);
lcd.print(" "); //clear the display
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("F=");
lcd.print(frequency);
lcd.print("Hz");
lcd.setCursor(11,0);
lcd.print(pulsewidth, 0);
lcd.print(" %");
lcd.setCursor(0,1);
lcd.print("H=");
lcd.print(hightime, 0);
lcd.print("ms");
lcd.setCursor(8,1);
lcd.print("L=");
lcd.print(lowtime, 0);
lcd.print("ms");
}