Hello, Please don't ignore this, after seeing the code length. Logic is simple
I am working on Arduino Project. Where I have to generate 100kHz from (using Timer 1) at a different duty cycle. The output show's on LCD will be different voltage according to the duty cycle of same frequency. I have written a code but facing few problem in it.
Problem 1st : It switch On the output when press select button and Off it again when again press select button for only 19.5V and for rest it give output but not switch it OFF.
Problem 2nd : I got output for 100kHz in Proteus but Led is not blinking in practical. It's not generating 100kHz from Pin 9 in reality.
Problem 3rd: The menu sometimes skip the two menuitem.
Problem 4th: It display 7.4V ONOFF instead of 7.4V : ON when press select to On and 7.4V : OFF when press again select to off.
There are three button Select, Up, Down for buiding menu.
Here is the code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //RS = 12, E = 11,
int menuitem = 1;
int page = 1;
int PWM = 9;
volatile boolean up = false;
volatile boolean down = false;
volatile boolean select = false;
int downButtonState = 0;
int upButtonState = 0;
int selectButtonState = 0;
int lastDownButtonState = 0;
int lastSelectButtonState = 0;
int lastUpButtonState = 0;
boolean statePWM = true;
long time = 0;
long debounce = 200;
void setup() {
lcd.clear();
lcd.begin(16, 2); //LCD have 16 column and 2 rows
lcd.setCursor(1,0); //LCD cursor set at 1st clomn and 0 rows
pinMode(13, INPUT_PULLUP); //button
pinMode(1, INPUT_PULLUP);
pinMode(0, INPUT_PULLUP);
pinMode(9, OUTPUT);
pinMode(10,OUTPUT);
TCCR1A=0;//reset the register
TCCR1B=0;//reset the register
TCNT1=0;
digitalWrite(9, LOW); //Start as OFF mode
Serial.begin(9600);
}
void loop() {
drawMenu();
downButtonState = digitalRead(1);
selectButtonState = digitalRead(0);
upButtonState = digitalRead(13);
checkIfDownButtonIsPressed();
checkIfUpButtonIsPressed();
checkIfSelectButtonIsPressed();
if (up == 1 ) //UP Button Menuitem Logics
{
up = false;
menuitem--;
if (menuitem==1)
{
menuitem=1;
delay(1000);
}
else if (menuitem==2)
{
menuitem=1;
delay(1000);
}
else if (menuitem==3)
{
menuitem=2;
delay(1000);
}
else if (menuitem==4)
{
menuitem=3;
delay(1000);
}
else if (menuitem==5)
{
menuitem=4;
delay(1000);
} else if (menuitem==6)
{
menuitem=5;
delay(1000);
}
else if (menuitem==7)
{
menuitem=6;
delay(1000);
} else if (menuitem==8)
{
menuitem=7;
delay(1000);
}
else
{
menuitem == 1;
}
}
if (down == 1 ) //DOWN Button Menuitem Logics
{
down = false;
menuitem++;
if (menuitem==1)
{
menuitem=2;
delay(1000);
}
else if (menuitem==2)
{
menuitem=3;
delay(1000);
}
else if (menuitem==3)
{
menuitem=4;
delay(50);
}
else if (menuitem==4)
{
menuitem=5;
delay(50);
}
else if (menuitem==5)
{
menuitem=6;
delay(50);
}
else if (menuitem==6)
{
menuitem=7;
delay(50);
}
else if (menuitem==7)
{
menuitem=8;
delay(50);
}
else if (menuitem==8)
{
menuitem=8;
delay(50);
}
else
{
menuitem == 1;
}
}
if (select == 1)
{
select = false;
if(statePWM)
{
statePWM = false;
turnPWMOn();
}
else
{
statePWM=true;
turnPWMOff();
}
}
}
void checkIfDownButtonIsPressed()
{
if (downButtonState != lastDownButtonState)
{
if (downButtonState == 1)
{
down=true;
}
delay(50);
}
lastDownButtonState = downButtonState;
}
void checkIfUpButtonIsPressed()
{
if (upButtonState != lastUpButtonState)
{
if (upButtonState == 1) {
up=true;
}
delay(50);
}
lastUpButtonState = upButtonState;
}
void checkIfSelectButtonIsPressed()
{
if (selectButtonState != lastSelectButtonState)
{
if (selectButtonState == 1) {
select=true;
}
delay(50);
}
lastSelectButtonState = selectButtonState;
}
void drawMenu()
{
if (page==1)
{
lcd.setCursor(1, 0);
lcd.print("MAIN MENU");
if (menuitem==1 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=511;// 50% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">7.4V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
if (menuitem==2 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=572;// 55.9% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">8.4V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
if (menuitem==3 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=707;// 69.16% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">12V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
if (menuitem==4 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=762;// 74.48% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">14.5V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
if (menuitem==5 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=793;// 77.57%% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">16.5V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
if (menuitem==6 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=806;// 78.8% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">17.5V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
if (menuitem==7 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=818;// 80% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">18.5V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
if (menuitem==8 && selectButtonState==1)
{
TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1
TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
OCR1A=829;// 81.02% duty cycle value
lcd.setCursor(1, 1);
lcd.print(">19.5V : ");
if(statePWM)
{
lcd.print("ON");
}
else
{
lcd.print("OFF");
}
}
}
}
void turnPWMOn()
{
digitalWrite(9, HIGH);
}
void turnPWMOff()
{
digitalWrite(9, LOW);
}