LiquidCrystal_i2c problem with pulses

Hello,
I have a arduino program that genarates pulses for a 3 phase thyristor bridge.
I have an i2c lcd to monitor the firing angleand the program prints to the lcd when a button is pushed.

The problem i am facing is that when the button is pushed and these three commands are executed:

lcd.setCursor(8,1);
lcd.print(A);
lcd.print(" ");

my pulses are keep going crazy. That is happening only when i press the button and while it is pressed
When i delete those 3 commands the pulses are ok while i press the button, so it is not a hardware external or wiring problem.

Is there anyother to print to the lcd without using this library?

Thank you in advance!

Post your complete program. My crystal ball is a little cloudy today

#include <TimerOne.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,20,4); // 0x3F is the i2c address and 20 x 4 is the size of the lcd

const int FREQ = 50; // Frequency
int A = 180; //Firing Angle 0 < A < 359

long D = 1000000*(A)/360/FREQ; //Firing Angl in microsecs (+30 moires)
long degrees_60 = 100000060/360/FREQ; //60 degrees in microsecs
long degrees_30 = 1000000
30/360/FREQ;
long degrees_150 = 1000000150/360/FREQ;
long degrees_330 = 1000000
330/360/FREQ;
long degrees_90 = 100000090/360/FREQ;
long degrees_270 = 1000000
270/360/FREQ;
long degrees_210 = 1000000*210/360/FREQ;

long previousMicros = 0;
long previousMicros2 = 0;
long previousMicros3 = 0;
int k = 0;

void (*fpointer)();

void check_k();
void reset();

void check_A();
void above();
void under_30();
void between_30_90();
void between_90_150();

void setup()
{
//setting each thyristor to a output port
pinMode(2, INPUT_PULLUP);
pinMode(3,OUTPUT); //thyristor 1
pinMode(4,OUTPUT); //thyristor 2
pinMode(5,OUTPUT); //thyristor 3
pinMode(6,OUTPUT); //thyristor 4
pinMode(7,OUTPUT); //thyristor 5
pinMode(8,OUTPUT); //thyristor 6

pinMode(14,INPUT); //button
pinMode(15,INPUT); //button
pinMode(17,INPUT); //button

//setup pwm chop signal
Timer1.initialize(1000000/20000); // 20.000hz
Timer1.pwm(9, 200); // setup pwm on pin 9, 50% duty cycle

//hardware interrupt for reseting time after each period
attachInterrupt(0, reset, RISING); //input signal from ppl pin 2

//start lcd and print a=
lcd.init(); // initialize the lcd
lcd.backlight();

lcd.setCursor(4,0);
lcd.print("Firing Angle");
lcd.setCursor(4,1);
lcd.print("A = ");
lcd.setCursor(8,1);
lcd.print(A);

// check A and set the pointer to the right function
check_A();
}

void loop() // na dokimasw diafores me to an valw while mesa sto loop kai meta vazw button kai lcd
{ while(1){
// check if a button is pressed

if(digitalRead(14) == HIGH || digitalRead(15) == HIGH || digitalRead(17) == HIGH){

if (digitalRead(14) == HIGH && (millis() - previousMicros2)> 500) {
A=A+5;
previousMicros2 = millis();
}
else if (digitalRead(15) == HIGH && (millis() - previousMicros2)> 500) {
A=A-5;
previousMicros2 = millis();
}
else if (digitalRead(17) == HIGH && (millis() - previousMicros2)> 500) {
A=180;
previousMicros2 = millis();
}

if (A>180) A=180;
if (A<0) A=0;

D = 1000000*(A)/360/FREQ;
check_A();

lcd.setCursor(8,1);
lcd.print(A);
lcd.print(" ");
}

fpointer();
//Serial.println(micros() - previousMicros3);
//previousMicros3 = micros();
}
}

void check_A()
{
if(A<=30){
under_30();
fpointer = &under_30;
}
if(A>30 && A<=90){
between_30_90();
fpointer = &between_30_90;
}
if(A>90 && A<150){
between_90_150();
fpointer = &between_90_150;
}
if(A>=150){
above();
fpointer = &above;
}

}

There are some more functions but there is no point to post them...
It is already big code.

Which Arduino board are you running this on ?

I run it on the arduino uno.

UKHeliBob:
Which Arduino board are you running this on ?

It's not necessarily your code, but all code is important. Specially when there is stuff like thyristor in your code that nowhere seems to be controlled in your code.

A wiring diagram will in this case also be needed.

And please edit your post and embed your code between

[code]and [/code]

so it looks like below

your code here

My code is too big to add it there so i will attach it even though i dont think it is nessesery.
I belive it has something to do with the library and those lcd commands.

new_pulses_2.ino (8.82 KB)

Any ideas what is wrong here?

With partial code and no wiring diagram, one can only guess.. But I have two things to nag about: 1) Why does your loop() method contain a while(forever) loop? This makes no sense since the loop() method is an endless loop in its own.. 2) As far as I can tell, you are updating the LCD for each loop when the button is pressed and this is a bad idea. You should not update the LCD that often, since it cannot refresh in such a fast rate. Try to lower the LCD refreshes to at most 5-10 times per second.

I run it on the arduino uno.

1. You are saying that you are using UNO, then what are the meanings of the following instructions when they are referring to non-existing digital pins?

pinMode(14,INPUT); //button
pinMode(15,INPUT); //button
pinMode(17,INPUT); //button

2. attachInterrupt(0, reset, RISING); What is happening here? Can you attach interrupt with DPin-0 (PD0)?

On a '328P board, 14-19 are valid digital pins, can be used in place of A0 to A5.

CrossRoads:
On a '328P board, 14-19 are valid digital pins, can be used in place of A0 to A5.

Very big thanks with +.

GolamMostafa:
2. attachInterrupt(0, reset, RISING); What is happening here? Can you attach interrupt with DPin-0 (PD0)?

The first argument is not a pin number. In this case INT0 (external interrupt 0 for pin 2).

The first argument is not a pin number. In this case INT0 (external interrupt 0 for pin 2).

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); (recommended)
attachInterrupt(interrupt, ISR, mode); (not recommended)

Really, getting old!!

Thanks+

Danois90:
With partial code and no wiring diagram, one can only guess.. But I have two things to nag about: 1) Why does your loop() method contain a while(forever) loop? This makes no sense since the loop() method is an endless loop in its own.. 2) As far as I can tell, you are updating the LCD for each loop when the button is pressed and this is a bad idea. You should not update the LCD that often, since it cannot refresh in such a fast rate. Try to lower the LCD refreshes to at most 5-10 times per second.

For the while in the loop, that was a test to see if the while is faster from the loop. I will propably remove it.
I update the lcd only when the putton is pressed. I have tried to put the lcd print lines of code inside every buttons "if", but i got the same result, at the time the button is pressed and lcd is updating it messing up the other signals on the ports digital outputs 2-8.

Thanks all of you for your answers!

dsoftas:
I update the lcd only when the putton is pressed.

Yes I can see that. But your buttons are not debounced the right way and whenever a button is pressed the code updating the LCD will be repeated whithout any delay. Holding the button for one second may cause the LCD to be updated hundreds, or even thousand times. This is some pseudo-code for what you are doing:

void loop() {

  if (either button down **) {

    if (button1 is down *) handle_button1();
    else if (button2 is down *) handle_button2();
    else if (button3 is down *) handle_button3();

    always_update_lcd_regardless_of_debounce();

  }

}
  • Marks the spots where debounce is actually performed.
    ** Marks the spot where the debouncing should be performed.

You do not provide any wiring, but since your are using I2C: Are those integrated cirquits decoupled with capacitors as is recommended?