Arduino Code showing 4 problem - Page 1
Forum section: electronics / general

author: radhika
author_rank: Regular Contributor
timestamp: September 16, 2018, 05:49:49 am
content: Hello, Please don't ignore this, after seeing the code length. Logic is simpleI 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 rowslcd.setCursor(1,0); //LCD cursor set at 1st clomn and 0 rowspinMode(13, INPUT_PULLUP); //buttonpinMode(1, INPUT_PULLUP);pinMode(0, INPUT_PULLUP);pinMode(9, OUTPUT);pinMode(10,OUTPUT);TCCR1A=0;//reset the registerTCCR1B=0;//reset the registerTCNT1=0;digitalWrite(9, LOW); //Start as OFF modeSerial.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=511;// 50% duty cycle valuelcd.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=572;// 55.9% duty cycle valuelcd.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=707;// 69.16% duty cycle valuelcd.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=762;// 74.48% duty cycle valuelcd.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=793;// 77.57%% duty cycle valuelcd.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=806;// 78.8% duty cycle valuelcd.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=818;// 80% duty cycle valuelcd.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 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A=829;// 81.02% duty cycle valuelcd.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);}

author: MK14
author_rank: Super Contributor
timestamp: September 16, 2018, 06:36:04 am
content: "Problem 3rd: The menu sometimes skip the two menuitem."It would probably be better if you used the "Insert Code" forum feature, like this:Code: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);}I'm not sure what your gadget, is trying to do, in overall terms. (You mention 100KHz output, but what is it ?, is it a simple frequency generator ?, why 19.5V, that sounds quite high ?).But assuming you intended to move through the menu, ONE item per button press.Theboldedsections are showing that it would move2 menu itemsat a time.Because you are BOTH decrementing the variable AND setting it to be one less as well.***Bolddid not work, because it is in a forum code block.Repeated here, so you can see the bolded sections: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);}

author: MK14
author_rank: Super Contributor
timestamp: September 16, 2018, 07:20:19 am
content: Similarly, moving the menu in the other direction, seems to have the same possible bug.menuitem++;if (menuitem==1){menuitem=2;

author: westfw
author_rank: Super Contributor
timestamp: September 16, 2018, 09:13:00 am
content: QuoteYou're not expecting to SEE a 100kHz output on an LED, are you? It would look continuously ON.

author: MK14
author_rank: Super Contributor
timestamp: September 16, 2018, 09:16:12 am
content: Quote from: westfw on September 16, 2018, 09:13:00 amYes, you can see it! (changes in intensity level, NOT flashing on/off, obviously)I think the intention, is that it is for changing the BRIGHTNESS via PWM of the LEDs.But the description is not too clear, so I could easily be wrong.

author: radhika
author_rank: Regular Contributor
timestamp: September 16, 2018, 12:23:55 pm
content: Thanks,I will try this code today and will let you know.

author: radhika
author_rank: Regular Contributor
timestamp: September 16, 2018, 12:27:03 pm
content: Yes, Absolutely Sir. It's continuously ON. It's not blinking. But, why? What's the reason behind it?Also, as in the code i have changed the value of ORC1A to get 100kHz at different duty cycle. Is it the correct one?

author: radhika
author_rank: Regular Contributor
timestamp: September 16, 2018, 12:31:03 pm
content: Quote from: MK14 on September 16, 2018, 09:16:12 amSir the intention is not varing the brightness. But deriving Chopper using 100kHz at different duty cycle using PWM from Arduino.May I know why it's not blinking?

author: MK14
author_rank: Super Contributor
timestamp: September 16, 2018, 12:39:52 pm
content: Quote from: radhika on September 16, 2018, 12:31:03 pmSorry, your original description did not seem to mention about a Chopper, and I could not see any schematic.When you sayblinking.Do you mean you checked it with anoscilloscope, or do you mean you observed an LED ?Aswestfwhas correctly pointed out (SORRY, I misunderstood, what you were trying to do, I thought it was brightness control or similar), you can't see 100KHz.Hence why I am asking about theoscilloscope.

author: sokoloff
author_rank: Super Contributor
timestamp: September 16, 2018, 01:46:00 pm
content: As other posters above have said, 100kHz flashing is over 1500x faster than an old CRT TV drew its picture, so you will never be able to see such flashing with the naked eye. Your code is going to make the LED very (very) subtly less bright at lower settings and slightly brighter at higher settings.If you intended for it to be flashing for a user, you need to slow the thing WAY down (perhaps to a period of a second or more, not 1/100,000th of a second).Your code was also fairly hard to follow, with lots of copy/pasting.I haven't tested the following code, but here's the way I'd have re-wrtiten it:Code:#include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //RS = 12, E = 11,int menuItem = 1;int page = 1;const int PWM_PIN = 9;const int UNKNOWN_OTHER_PIN = 10; // FIXMEconst int UP_BUTTON_PIN = 13;const int DOWN_BUTTON_PIN = 1;const int SELECT_BUTTON_PIN = 0;int g_dutyCycle = 0;volatile boolean up = false;volatile boolean down = false;volatile boolean select = false;boolean g_statePWM = true;long time = 0;long debounce = 200;void setup() {lcd.clear();lcd.begin(16, 2); //LCD have 16 column and 2 rowslcd.setCursor(1,0); //LCD cursor set at 1st clomn and 0 rowspinMode(UP_BUTTON_PIN, INPUT_PULLUP); //buttonpinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);pinMode(SELECT_BUTTON_PIN, INPUT_PULLUP);pinMode(PWM_PIN, OUTPUT);pinMode(UNKNOWN_OTHER_PIN,OUTPUT); // FIXMETCCR1A=0;//reset the registerTCCR1B=0;//reset the registerTCNT1=0;setPWM(g_statePWM, g_dutyCycle);Serial.begin(9600);}void loop() {checkIfDownButtonIsPressed(digitalRead(DOWN_BUTTON_PIN));checkIfUpButtonIsPressed(digitalRead(UP_BUTTON_PIN));checkIfSelectButtonIsPressed(digitalRead(SELECT_BUTTON_PIN));if (up || down) { // Slow the up/down menus a bitdelay(1000);}if (up) { //UP Button Menuitem Logicsup = false;menuItem--;if (menuItem < 1) {menuItem = 1;}}if (down) { //DOWN Button Menuitem Logicsdown = false;menuItem++;if (menuItem > 8) {menuItem = 8;}}drawMenu(); // Has a side-effect of configuring the duty cycle, which isn't idealif (select) {select = false;g_statePWM = !g_statePWM;setPWM(g_statePWM, g_dutyCycle);}}void checkIfDownButtonIsPressed(int newState){static int lastDownButtonState = 0;checkButton(down, lastDownButtonState, newState);}void checkIfUpButtonIsPressed(int newState){static int lastUpButtonState = 0;checkButton(up, lastUpButtonState, newState);}void checkIfSelectButtonIsPressed(int newState){static int lastSelectButtonState = 0;checkButton(select, lastSelectButtonState, newState);}void checkButton(volatile boolean &output, int &oldState, int newState) {if (oldState != newState) {if (newState == 1) {output = true;}delay(50);}oldState = newState;}void drawMenu(){if (page == 1) {lcd.setCursor(1, 0);lcd.print("MAIN MENU");lcd.setCursor(1, 1);switch (menuItem) {case 1:g_dutyCycle = 511; // 50% duty cycle valuelcd.print(">7.4V : ");break;case 2:g_dutyCycle = 572; // 55.9% duty cycle valuelcd.print(">8.4V : ");break;case 3:g_dutyCycle = 707; // 69.16% duty cycle valuelcd.print(">12V : ");break;case 4:g_dutyCycle = 762; // 74.48% duty cycle valuelcd.print(">14.5V : ");break;case 5:g_dutyCycle = 793; // 77.57%% duty cycle valuelcd.print(">16.5V : ");break;case 6:g_dutyCycle = 806; // 78.8% duty cycle valuelcd.print(">17.5V : ");break;case 7:OCR1A = 818; // 80% duty cycle valuelcd.print(">18.5V : ");break;case 8:OCR1A = 829; // 81.02% duty cycle valuelcd.print(">19.5V : ");break;default:g_dutyCycle = 0; // 0% duty cycle valuelcd.print("CODE_ERROR : ");break;}if (g_statePWM) {lcd.print("ON");} else {lcd.print("OFF");}}}void setPWM(boolean on, int dutyCycle){TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1 //also WGM11, WGM10 are 1TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1OCR1A = dutyCycle;digitalWrite(PWM_PIN, on ? HIGH : LOW);}

author: radhika
author_rank: Regular Contributor
timestamp: September 16, 2018, 03:07:11 pm
content: Quote from: MK14 on September 16, 2018, 12:39:52 pmIt's absolutely OkayAnd yes i just observed the LED but not checked on Oscilloscope

author: radhika
author_rank: Regular Contributor
timestamp: September 16, 2018, 04:31:29 pm
content: Quote from: sokoloff on September 16, 2018, 01:46:00 pmHello Sir,Thank you so much. It solve much of my code problem. It solve teh skipping of menuitem and ON OFF problem.But, It displaying 7.4V : ONOFF 8.4V ONOFF instead of 7.4V: ON (when ON) and 7.4V :OFF (when OFF) but why? How to solve this?

author: sokoloff
author_rank: Super Contributor
timestamp: September 16, 2018, 04:39:02 pm
content: I bet there are leftover characters on the LCDChange the LCD print ON and OFF to add some spaces:lcd.print(“ON “);lcd.print(“OFF “);Sorry this is not full code, but I’m only on my phone at the moment. I’m sure you can apply this.

author: radhika
author_rank: Regular Contributor
timestamp: September 16, 2018, 05:32:00 pm
content: Thank YOu so much. It working well now. Tomorrow I will go to lab and check it on Oscilloscope.Thanks for the help

author: sokoloff
author_rank: Super Contributor
timestamp: September 16, 2018, 05:40:18 pm
content: No problem. Happy to help!