Hi.
First a wanna start be saying that im a newbe at this so sorry if im stuped:)
I got some help whit this code, but the menusystem i try to make my self,as you all can see:)
But now i have run in to a problem i can fix:(
Im gonna try to explane the problem:
When i start the code, the first text on the lcd shows up = working
Then i can hit "A" or "B" on the keypad to get to diffrent menus = working
In menu "A" i can enter numbers and everything works good.
When i press "B" i get in to that menu BUT i cant enter any numbers, if i hit any key the menu goes back to menu"A"???
How can i get menu "B" to work the same way as menu "A"?
Thanks for any help!
#include <AccelStepper.h>
#include <Keypad.h>
#include <LiquidCrystalFast.h>
LiquidCrystalFast lcd(12, 10, 11, 5, 4, 3, 2);
volatile int aPos=0;
volatile int bPos=0;
volatile int currPos=0;
volatile int aCur=0;
volatile int bCur=0;
int val = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {31, 33, 35, 37};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Keypad Library definition
#define backlight_pin A2
AccelStepper stepperA(1, A0, A1);
AccelStepper stepperB(2, A3, A4);
void setup() {
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print( "->PALLINSTELLNING<-" );
lcd.setCursor(0,2);
lcd.print("A = LENGD");
lcd.setCursor(11,2);
lcd.print("B = BREDD");
analogWrite(backlight_pin, 250);
stepperA.setMaxSpeed(1500);
stepperA.setAcceleration(400);
stepperB.setMaxSpeed(1500);
stepperB.setAcceleration(400);
}
void loop(){
pinMode(6, OUTPUT);
analogWrite(6, 140);
char keypressed = keypad.getKey();
if (keypressed != NO_KEY){
switch (keypressed) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
setPos( keypressed );
break;
case 'A':
menylengd();
break;
case 'B':
menybred();
break;
case '*':
deletenumber();
break;
case '#':
calculatedistance();
break;
case 'C':
currPos=0;
break;
case 'D':
currPos=1;
break;
}
}
}
void setPos( char num ) {
int iNum = String(num).toInt();
switch( currPos ) {
case 0:
if( aPos>999) return;
aPos*=10;
aPos+=iNum;
break;
case 1:
if( aPos>999) return;
bPos*=10;
bPos+=iNum;
break;
}
Lcdlengd();
}
void deletenumber() {
switch( currPos ) {
case 0:
aPos/=10;
break;
case 1:
bPos/=10;
break;
}
Lcdlengd();
}
void menylengd() {
switch( currPos ) {
case 0:
break;
}
Lcdlengd();
}
void menybred() {
switch( currPos ) {
case 0:
break;
}
Lcdbredd();
}
void calculatedistance() { // Used to create a full number from entered numbers
if( aPos != aCur ) {
moveStepper( 0 );
}
if( bPos != bCur ) {
moveStepper( 1 );
}
Lcdlengd();
}
void moveStepper( int which ) {
lcd.begin( 20,4 );
lcd.setCursor( 0,1 );
lcd.print( "->FLYTTAR FOTOCELL<-" );
lcd.print(" ");
long calculatedmove;
switch( which ) {
case 0:
calculatedmove = (aPos * 1938UL)/20;
stepperA.runToNewPosition( calculatedmove );
aCur = aPos;
break;
case 1:
calculatedmove = (bPos * 1938UL)/20;
stepperB.runToNewPosition( calculatedmove );
bCur = bPos;
break;
}
Lcdlengd();
}
void Lcdbredd() {
String strA = String(bPos)+ "mm";
String strB = String(bCur)+ "mm";
String strC = "----->BREDD<------";
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print(strC);
lcd.setCursor(0,2);
lcd.print(aPos);
lcd.setCursor(0,2);
lcd.print(strA);
lcd.setCursor(0,1);
lcd.print("ANGE:");
lcd.setCursor(14,2);
lcd.print(strB);
lcd.setCursor(8,1);
lcd.print("AKTUELL POS:");
}
void Lcdlengd() {
String strA = String(aPos)+ "mm";
String strB = String(aCur)+ "mm";
String strC = "----->LENGD<------";
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print(strC);
lcd.setCursor(0,2);
lcd.print(aPos);
lcd.setCursor(0,2);
lcd.print(strA);
lcd.setCursor(0,1);
lcd.print("ANGE:");
lcd.setCursor(14,2);
lcd.print(strB);
lcd.setCursor(8,1);
lcd.print("AKTUELL POS:");
}