hey all. I have a quick question with a problem i'm having.
The loop of my code has to run at least 60 times a second. But is adjustable and will run as slow as twice a second in some situations that will be determined by me. I have 2 pots being read on the analog. and i'm trying to show the variables from the pots on my LCD. Here's my problem. without any LCD printing, the code runs 185 times a second. If i try to print 4 lines to the lcd, 2 text, and 2 number values, the fastest my code will run is 22 times a second, which won't work for me. If i remove the 2 text lines and only print the 2 number values it will run 85 times a second... which will work, but i was hoping to have the text to go along with it if possible.
i have basic LCD experience so there's a lot i don't know. I tried to set it all up in the setup, and then update only the numbers, but that doesn't seem to work because the lcd.clear() removes everything and then i'm left with only the numbers.
below is my code, any suggestions would be appreciated. I can work with only numbers, but this is a issue i've had in the past and hope to find a workaround.
//Nano ATmega328P processor
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
// LCD SDA = A4 pin
// LCD SDL = A5 pin
volatile uint32_t count;
int pwmDriver = 9;
int counter = 5;
int onPot = A0;
int gatePot = A1;
long gateTimer; //pot variable count
long pulseCount;
void setup() {
TCCR1A = 0; // Clear the TCCR1A register
TCCR1B = _BV(CS12) | _BV(CS11) | _BV(CS10); // Set T1 (D5) as the timer 1 clock source triggered on the rising edge
TIMSK1 = _BV(TOIE1); // Enable overflow interrupts on timer 1
pinMode(counter, INPUT);
DDRB = B00000010; // pinMode(pwmDriver, OUTPUT);
lcd.init();
lcd.backlight();
int onReading = analogRead(onPot);
int pulseCount = map(onReading, 1023, 0, 1, 20);
int gateReading = analogRead(gatePot);
int gateTimer = map(gateReading, 1023, 0, 1, 500);
lcd.setCursor(0,0);
lcd.print("Pulses... ");
lcd.setCursor(12,0);
lcd.print(pulseCount);
lcd.setCursor(0,1);
lcd.print("Gate On.. ");
lcd.setCursor(12,1);
lcd.print(gateTimer);
delay(3000);
}
void loop() {
PORTB = B00000010; // digitalWrite(pwmDriver, HIGH);
int onReading = analogRead(onPot);
int pulseCount = map(onReading, 1023, 0, 1, 20);
int gateReading = analogRead(gatePot);
int gateTimer = map(gateReading, 1023, 0, 1, 500);
// lcd.setCursor(0,0);
// lcd.print("Pulses... ");
lcd.setCursor(12,0);
lcd.print(pulseCount);
// lcd.setCursor(0,1);
// lcd.print("Gate On.. ");
lcd.setCursor(12,1);
lcd.print(gateTimer);
TCNT1=0;
while (TCNT1 <= pulseCount){
PORTB = B00000000; // digitalWrite(pwmDriver, LOW);
}
PORTB = B00000010; // digitalWrite(pwmDriver, HIGH);
delay(gateTimer);
lcd.clear();
}