Hello dear friends,
I have a request/question if you lads would be willing to help me out with problem in my code?
So the set up is arduino mega 2560 with LCD, few buttons and KY-040 encoder.
KY-040 part of code is making me problems i used this:
Library:
A high performance Encoder library is now available
And this tutorial
http://forum.arduino.cc/index.php?topic=242356.0
Problem is everytime i run the program it looks like it gets stuck in isr(), and only if i spin encoder is doing normal loop() part of program
My guess is that there is problem in my loops beacause i tested with library example and displaying mills on lcd it worked well, as i am new to this i cannot resolve it on myself and the code is messy too so i'm terribly sorry for that and for any grammar error i made.
//Vključenje knjižnice za LCD in enkoder
#include <LiquidCrystal.h>
#include <Encoder.h>
//Definicija pinov LCDja (glej http://www.instructables.com/id/Connecting-an-LCD-to-the-Arduino/ )
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(20, 21);
// avoid using pins with LEDs attached
//Konstante tipk od leve proti desni
const int gumb1 = 24;
const int gumb2 = 22;
const int gumb3 = 26;
const int gumb4 = 28;
//Konstante dekoderja
const int PinCLK = 21; // Branje CLK signala
const int PinDT = 20; // Branje DT signala
const int PinSW = 48; // Tipka na dekoderju
//Konstanta led luči na gumbu
const int ledPin = 30;
//Konstanta za višino reza max in min
const int min_ = 0;
const int max_ = 800;
//Konstante za dodajanje
const int faktor1 = 1;
const int faktor2 = 10;
const int faktor3 = 100;
//Spremenljivke pozicije
int visina = 0;
int visina_trenutna = 0;
//Spremenljivka faktorja dodajana
int faktor = 1;
//Postavitev vseh gumbov na 0
int gumb1_status = 0;
int gumb2_status = 0;
int gumb3_status = 0;
int gumb4_status = 0;
void setup() {
//Inicializacija LCDja
lcd.begin(16, 2);
lcd.noBlink();
//Definicja vhodov na pullup način (tipka vezana na vhod in gnd) in izhoda
pinMode(ledPin, OUTPUT);
pinMode(gumb1, INPUT_PULLUP);
pinMode(gumb2, INPUT_PULLUP);
pinMode(gumb3, INPUT_PULLUP);
pinMode(gumb4, INPUT_PULLUP);
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT_PULLUP);
Serial.begin (9600);
Serial.println("Start");
}
long oldPosition = -999;
void loop(){
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
//Zapis statusa tipke v spremenljivko predefinirano v 0
gumb1_status = digitalRead(gumb1);
gumb2_status = digitalRead(gumb2);
gumb3_status = digitalRead(gumb3);
gumb4_status = digitalRead(gumb4);
//Zapis osnovnih vrstic na lcd
lcd.setCursor(0, 0);
lcd.print("Pozicija:");
lcd.setCursor(0, 1);
lcd.print("Pomik:");
//Če je stisnjen gumb 1 se menja faktor dodajanja
if (gumb1_status == LOW) {
if (faktor == 1) {
faktor = faktor2;
delay(300);
}
else if(faktor == 10) {
faktor = faktor3;
delay(300);
}
else if (faktor == 100) {
faktor = faktor1;
delay(300);
lcd.clear();
}
}
//Če je pritisnjen gumb 2 potem dodaj višino množeno s faktorjem dodajanja
if (gumb2_status == LOW) {
visina = visina - (1*faktor);
visina_trenutna = visina_trenutna - (1*faktor);
if (visina < 0){
visina = 0;
}
if (visina_trenutna < 0){
visina_trenutna = 0;
}
delay(300);
lcd.clear();
}
//Če je pritisnjen gumb 3 potem odvzemi višino množeno s faktorjem dodajanja
if (gumb3_status == LOW) {
if (visina_trenutna < 800){
if (visina > 800){
visina = 800;
}
if (visina_trenutna > 800){
visina_trenutna = 800;
visina=visina;
}
visina = visina + (1*faktor);
visina_trenutna = visina_trenutna + (1*faktor);
}
delay(300);
lcd.clear();
}
//Če je pritisnjen gumb 4 postavi referencno 0
if (gumb4_status == LOW) {
digitalWrite(ledPin, HIGH);
visina = 0;
delay(30);
lcd.clear();
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
//Zapiši spremenljivke na lcd (trenutna visina zeljena visina in faktor dodajana)
lcd.setCursor(9, 0);
lcd.print(visina);
lcd.setCursor(6, 1);
lcd.print(faktor);
lcd.setCursor(13, 1);
lcd.print(oldPosition);
}
}