Author Topic: Arduino Code showing 4 problem  (Read 2160 times)

0 Members and 1 Guest are viewing this topic.

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Arduino Code showing 4 problem
« on: September 16, 2018, 05:49:49 am »
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);
  }
   
 




 
 

Online MK14

  • Super Contributor
  • ***
  • Posts: 4534
  • Country: gb
Re: Arduino Code showing 4 problem
« Reply #1 on: September 16, 2018, 06:36:04 am »
"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: [Select]
if (up == 1 )   //UP Button Menuitem Logics
  {
    up = false;
    [b]menuitem--;[/b]
    if (menuitem==1)
    {
      menuitem=1;   
      delay(1000);
    }
    else if (menuitem==2)
    {
      [b]menuitem=1;[/b]
      delay(1000);
    }
    else if (menuitem==3)
    {
      [b]menuitem=2;[/b]
      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.

The bolded sections are showing that it would move 2 menu items at a time.
Because you are BOTH decrementing the variable AND setting it to be one less as well.

***Bold did 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);
    }



« Last Edit: September 16, 2018, 06:44:26 am by MK14 »
 
The following users thanked this post: radhika

Online MK14

  • Super Contributor
  • ***
  • Posts: 4534
  • Country: gb
Re: Arduino Code showing 4 problem
« Reply #2 on: September 16, 2018, 07:20:19 am »
Similarly, moving the menu in the other direction, seems to have the same possible bug.

menuitem++;
    if (menuitem==1)
    {
      menuitem=2;
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Arduino Code showing 4 problem
« Reply #3 on: September 16, 2018, 09:13:00 am »
Quote
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.
You're not expecting to SEE a 100kHz output on an LED, are you?  It would look continuously ON.
 
The following users thanked this post: MK14, radhika

Online MK14

  • Super Contributor
  • ***
  • Posts: 4534
  • Country: gb
Re: Arduino Code showing 4 problem
« Reply #4 on: September 16, 2018, 09:16:12 am »
You're not expecting to SEE a 100kHz output on an LED, are you?  It would look continuously ON.

Yes, 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.
« Last Edit: September 16, 2018, 09:20:32 am by MK14 »
 

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Arduino Code showing 4 problem
« Reply #5 on: September 16, 2018, 12:23:55 pm »
Thanks,
I will try this code today and will let you know.
 

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Arduino Code showing 4 problem
« Reply #6 on: September 16, 2018, 12:27:03 pm »
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?
 

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Arduino Code showing 4 problem
« Reply #7 on: September 16, 2018, 12:31:03 pm »
You're not expecting to SEE a 100kHz output on an LED, are you?  It would look continuously ON.

Yes, 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.
Sir 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?
 

Online MK14

  • Super Contributor
  • ***
  • Posts: 4534
  • Country: gb
Re: Arduino Code showing 4 problem
« Reply #8 on: September 16, 2018, 12:39:52 pm »
Sir 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?

Sorry, your original description did not seem to mention about a Chopper, and I could not see any schematic.

When you say blinking.
Do you mean you checked it with an oscilloscope, or do you mean you observed an LED ?

As westfw has 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 the oscilloscope.
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Arduino Code showing 4 problem
« Reply #9 on: September 16, 2018, 01:46:00 pm »
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: [Select]
#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; // FIXME


const 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 rows
  lcd.setCursor(1,0);  //LCD cursor set at 1st clomn and 0 rows
  pinMode(UP_BUTTON_PIN, INPUT_PULLUP);  //button
  pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);
  pinMode(SELECT_BUTTON_PIN, INPUT_PULLUP);

  pinMode(PWM_PIN, OUTPUT);
  pinMode(UNKNOWN_OTHER_PIN,OUTPUT);  // FIXME
 
  TCCR1A=0;//reset the register
  TCCR1B=0;//reset the register
  TCNT1=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 bit
    delay(1000);
  }
 
  if (up) {   //UP Button Menuitem Logics
    up = false;
    menuItem--;
    if (menuItem < 1) {
      menuItem = 1;
    }
  }

  if (down) { //DOWN Button Menuitem Logics
    down = false;
    menuItem++;
    if (menuItem > 8) {
      menuItem = 8;
    }
  }

  drawMenu();  // Has a side-effect of configuring the duty cycle, which isn't ideal

  if (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 value
      lcd.print(">7.4V : ");
      break;

    case 2:
      g_dutyCycle = 572; // 55.9% duty cycle value
      lcd.print(">8.4V : ");
      break;

    case 3:
      g_dutyCycle = 707; // 69.16% duty cycle value
      lcd.print(">12V : ");
      break;

    case 4:
      g_dutyCycle = 762; // 74.48% duty cycle value
      lcd.print(">14.5V : ");
      break;

    case 5:
      g_dutyCycle = 793; // 77.57%% duty cycle value
      lcd.print(">16.5V : ");
    break;

    case 6:
      g_dutyCycle = 806; // 78.8% duty cycle value
      lcd.print(">17.5V : ");
      break;

    case 7:
      OCR1A = 818; // 80% duty cycle value
      lcd.print(">18.5V : ");
      break;

    case 8:
      OCR1A = 829; // 81.02% duty cycle value
      lcd.print(">19.5V : ");
      break;

    default:
      g_dutyCycle = 0; // 0% duty cycle value
      lcd.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 1
  TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
  OCR1A = dutyCycle;
 
  digitalWrite(PWM_PIN, on ? HIGH : LOW);
}
 
The following users thanked this post: MK14, radhika

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Arduino Code showing 4 problem
« Reply #10 on: September 16, 2018, 03:07:11 pm »
Sir 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?

Sorry, your original description did not seem to mention about a Chopper, and I could not see any schematic.

When you say blinking.
Do you mean you checked it with an oscilloscope, or do you mean you observed an LED ?

As westfw has 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 the oscilloscope.

It's absolutely Okay :) And yes i just observed the LED but not checked on Oscilloscope
 
The following users thanked this post: MK14

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Arduino Code showing 4 problem
« Reply #11 on: September 16, 2018, 04:31:29 pm »
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: [Select]
#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; // FIXME


const 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 rows
  lcd.setCursor(1,0);  //LCD cursor set at 1st clomn and 0 rows
  pinMode(UP_BUTTON_PIN, INPUT_PULLUP);  //button
  pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);
  pinMode(SELECT_BUTTON_PIN, INPUT_PULLUP);

  pinMode(PWM_PIN, OUTPUT);
  pinMode(UNKNOWN_OTHER_PIN,OUTPUT);  // FIXME
 
  TCCR1A=0;//reset the register
  TCCR1B=0;//reset the register
  TCNT1=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 bit
    delay(1000);
  }
 
  if (up) {   //UP Button Menuitem Logics
    up = false;
    menuItem--;
    if (menuItem < 1) {
      menuItem = 1;
    }
  }

  if (down) { //DOWN Button Menuitem Logics
    down = false;
    menuItem++;
    if (menuItem > 8) {
      menuItem = 8;
    }
  }

  drawMenu();  // Has a side-effect of configuring the duty cycle, which isn't ideal

  if (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 value
      lcd.print(">7.4V : ");
      break;

    case 2:
      g_dutyCycle = 572; // 55.9% duty cycle value
      lcd.print(">8.4V : ");
      break;

    case 3:
      g_dutyCycle = 707; // 69.16% duty cycle value
      lcd.print(">12V : ");
      break;

    case 4:
      g_dutyCycle = 762; // 74.48% duty cycle value
      lcd.print(">14.5V : ");
      break;

    case 5:
      g_dutyCycle = 793; // 77.57%% duty cycle value
      lcd.print(">16.5V : ");
    break;

    case 6:
      g_dutyCycle = 806; // 78.8% duty cycle value
      lcd.print(">17.5V : ");
      break;

    case 7:
      OCR1A = 818; // 80% duty cycle value
      lcd.print(">18.5V : ");
      break;

    case 8:
      OCR1A = 829; // 81.02% duty cycle value
      lcd.print(">19.5V : ");
      break;

    default:
      g_dutyCycle = 0; // 0% duty cycle value
      lcd.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 1
  TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1
  OCR1A = dutyCycle;
 
  digitalWrite(PWM_PIN, on ? HIGH : LOW);
}


Hello Sir,
Thank you so much. It solve much of my code problem. It solve teh skipping of menuitem and ON OFF problem.  :clap:
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?
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Arduino Code showing 4 problem
« Reply #12 on: September 16, 2018, 04:39:02 pm »
I bet there are leftover characters on the LCD

Change 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.
 
The following users thanked this post: MK14, radhika

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Arduino Code showing 4 problem
« Reply #13 on: September 16, 2018, 05:32:00 pm »
Thank YOu so much. It working well now. Tomorrow I will go to lab and check it on Oscilloscope.
Thanks for the help :) ;D
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Arduino Code showing 4 problem
« Reply #14 on: September 16, 2018, 05:40:18 pm »
No problem. Happy to help!
 
The following users thanked this post: radhika


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf