Notifications
Clear all

Keypad control not working

156 Posts
3 Users
24 Likes
9,474 Views
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

Hi Guys, I am struggling with the format of the command to get the following working:

I have a keypad, and a TFT, the system should wait for input, and if it equals a certain number, move a stepper motor to a pre-defined degree.My problem is that I somehow cannot figure the command out, or rather the format of the command.

I have defined the following:

// Variable
String confkey;
int num;

void loop()

{
     IRAval1 = digitalRead(IRSensor1);
     IRAval2 = digitalRead(IRSensor2);
     if ((IRAval1<=0)&&(IRAval2=0))
    {digitalWrite(BY_1, HIGH);
     digitalWrite(BY_2, HIGH);
     digitalWrite(BG_1, HIGH);
     digitalWrite(BG_2, HIGH);
    }
     if ((IRAval1>=1)&&(IRAval2>=1))
    {digitalWrite(BY_1, HIGH);
     digitalWrite(BY_2, LOW);
     digitalWrite(BG_1, HIGH);
     digitalWrite(BG_2, LOW);
    }
     tft.setTextSize(2);
     tft.setTextColor(BLACK);
     tft.setCursor(0, 0);
     tft.fillScreen(WHITE);
     tft.println("-------------------------"); // Static line 1
     tft.println(" To select a Locomotive "); // Static line 2
     tft.println(" Press the loco sched no "); // Static line 3
     tft.println("-------------------------"); // Static line 1
  }
   int GetNumber()
{
   char key = kpd.getKey();
   if (key != NO_KEY)
{
   switch (num)
   if (num=0);
   tft.setTextSize(2);
   tft.setCursor(10, 50);
   Serial.print(num);
   tft.println("-------------------------"); // Static line 1
   tft.println(" Bridge move to "); // Variable line 3a - This will turn the turntable back to 0°
   tft.println(" 0 degrees selected "); // Variable line 3b - This will turn the turntable back to 0°
   tft.println("-------------------------"); // Static line 1
     delay (1000);
   tft.println("-------------------------"); // Static line 1
   tft.println(" Press C To Confirm "); // Pressing C will activate the stepper motor and turn on amber LED's    and turn off green LED's
   tft.println("-------------------------"); // Static line 1
 }
 {
   String getKey();
   char key = kpd.getKey();
   if (key != NO_KEY)
   // break;

***   switch (confkey) ***

   if (confkey='C'); // Confirm selection
   StepMot.step(step0);
   delay(dt);
   digitalWrite(BY_1, HIGH);
   digitalWrite(BY_2, HIGH);
   tft.setTextColor(RED);
   tft.println("-------------------------"); // Static line 1
   tft.println("Standby bridge"); // Flashing RED while stepper motor are turning
   tft.println(" are moving "); // Flashing RED while stepper motor are turning
   tft.println("-------------------------"); // Static line 1
   digitalWrite(BY_1, LOW);
   digitalWrite(BY_2, LOW);
     delay(500);
   digitalWrite(BY_1, HIGH);
   digitalWrite(BY_2, HIGH);
     delay(500);
   digitalWrite(BY_1, LOW);
   digitalWrite(BY_2, LOW);
     delay(500);
   digitalWrite(BY_1, HIGH);
   digitalWrite(BY_2, HIGH);
     delay(500);
   digitalWrite(BY_1, LOW);
   digitalWrite(BY_2, LOW);
     delay (3000);
   tft.setTextColor(BLACK);
   tft.println("-------------------------"); // Static line 1
   tft.println("Bridge turned ");
   tft.println(" to 0 degrees "); // To be displayed after the stepper motor is turned 180°
   tft.println("-------------------------"); // Static line 1
   digitalWrite(BY_1, LOW);
   digitalWrite(BY_2, LOW);

 

ERROR:

switch quantity not an integer (*** problematic line ***)

I have read that "switch" may only be used for integers - BUT - what do you use to switch or move the input to a string?

Regard


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@kobusven95

 

To fix the compile time error change ...

switch (confkey)

to

switch (confkey.asInt())

That will extract the string's integer value for use in the switch statement.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will Great - thanks a mil - however it is switch (confkey.toInt()) 🙂

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@kobusven95 

Damn ! I simply can't function before my morning coffee 🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@kobusven95

More problems I found on a quick scan. I presume some off these are due to this being a work in progress so you can ignore them (except for the last one which will bite you eventually 🙂

 

num - never set

 

confkey - never set

 

if ((IRAval1<=0)&&(IRAval2=0))

should be

if ((IRAval1<=0)&&(IRAval2==0))

the second equals turns it from an assignment to a logical test

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will thanks again.

String confkey;
int num;

I changed that strings as suggested. Should I change the second string as well?

if ((IRAval1>=1)&&(IRAval2>=1))

But I messed something up now bigtime - my screen keep scrolling...

Back to the drawing board...


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@kobusven95 

No, the second test is valid because it's already in the form of a test with a logical result.

The problem with (IRAval2=0)) is that it SETS THE VALUE of IRAval2 TO zero. Adding the second equals changes that operation to a logical value (is IRAval2 equal to zero).

A subtle change but a huge difference. The expressions <,>,<=,>= don't correspond to any normal arithmetic operation and are easily recognized to have logical result values, but the distinction is required in the case of = because it does have a valid meaning in itself (i.e. value assignment).

Anything seems possible when you don't know what you're talking about.


   
kobusven95 reacted
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will Thanks for the explanation


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

May I ask another question please?

I have changed the sketch as follows:

void setup()
{
// Stepper PINS
//Definition of the pins, remember where you need
   Serial.begin(9600);
   StepMot.setSpeed(motSpeed);
   tft.reset();
// IRA
   pinMode(IRSensor1, INPUT);
   pinMode(IRSensor2, INPUT);
// LCD
   uint16_t id = tft.readID();
   tft.begin(id);
   tft.setRotation(1);
   tft.fillScreen(WHITE);
   tft.setTextColor(BLUE, WHITE);
// LED
   pinMode (BY_1, OUTPUT);
   pinMode (BY_2, OUTPUT);
   pinMode (BG_1, OUTPUT);
   pinMode (BG_2, OUTPUT);
// Initialize LCD
   tft.setTextSize(2);
   tft.setTextColor(BLACK);
   tft.setCursor(0, 0);
   tft.fillScreen(WHITE);
   tft.println("-------------------------"); // Static line 1
   tft.println(" To select a Locomotive "); // Static line 2
   tft.println(" Press the loco sched no "); // Static line 3
   tft.println("-------------------------"); // Static line 1
}

However, it seems it now forms an infinite loop, on the screen - the output on the serial monitor shows output from 1 to 8, then back to 1.

Any ideas - I also moved the screen initialize from the loop to the setup, to see if that solves it, but no luck.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@kobusven95 

You can ask as many questions as you want 🙂

It's impossible to answer this particular question though, because it seems that all you've shown us is the setup() and no loop().

It's the loop method that cycles, so the cause is most likely in there somewhere.

Also, your code is unindented and hard for us follow. To post code you should select the code in the Arduino IDE (usually select all) and then choose "copy as HTML" to copy the selected text. Then switch over to the browser and position the cursor about where you want the code. Then press return a couple of times to insert a couple of blank lines. Then click the "{;}" icon above the reply box (that'll put you in an HTML environment, so don't worry, you haven's screwed up when it goes all squirrelly) and position the cursor between a pair of <p></p> (the blank lines you made earlier) and paste the code in between a </p> and <p>.

If you click the Preview button below the reply box, you'll see how it will be displayed on the forum.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will Nice - I see it have a option to copy for FORUM as well.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @kobusven95

@will Nice - I see it have a option to copy for FORUM as well.

Yeah, but I've never seen that work very well 🙁

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@kobusven95 

WOW !!!

It'll take me a while to go through this :):)

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will I tried to break it down into little bits, but it is too integrated...


   
ReplyQuote
Page 1 / 11