Notifications
Clear all

Road block LCD issue DB1

31 Posts
4 Users
1 Likes
1,556 Views
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2111
 

@robotbuilder 

Instructions for installing a ZIP library are here ... 

https://www.arduino.cc/en/guide/libraries

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
mrclassicman
(@mrclassicman)
Eminent Member
Joined: 1 year ago
Posts: 31
Topic starter  

@will Yes as near as I can tell it is. The LCD shield, as with many other shields, is not named in his article, but the pin assignments and other sketches have worked with this shield. I installed the " <LCD_Key.h"> library using the install library from a zip file. There is an example sketch for this library. I have loaded and executed this example and it does work. For this reason, I figured that I must have succeeded in installing it correctly. This particular library is not available in the normal Arduino repository, which is why Bill included it in the code files at the end of episode. My gut tells me the issue is in the code for the Liquid crystal display though. Other examples for use of this shield are different than what is in this program. If you see nothing glaringly wrong in the code, I can ask no more. I think I will just try and go analog with physical switches. I don't need a screen to tell me what I pressed. lol. Maybe I will revisit the Liquid Crystal thing in the future. I can't thank you enough for your assistance you've given me!

 

Terry 

 


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2111
 
Posted by: @mrclassicman

This particular library is not available in the normal Arduino repository, which is why Bill included it in the code files at the end of episode. My gut tells me the issue is in the code for the Liquid crystal display though. Other examples for use of this shield are different than what is in this program. If you see nothing glaringly wrong in the code, I can ask no more. I think I will just try and go analog with physical switches.

I'm asking politely, one last time, that you add in a print statement and tell us what value localKey has when you press any of the buttons.

I suggest you add some Serial.print statements to check on the execution. You haven't said anything to suggest that you'd done so. Please add "Serial.begin(9600);" (or whatever baud rate you're using) for your setup() method and add 'Serial.print("localKey = "); Serial.println(localKey);' to the sketch immediately following the assignment of localKey.

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 3 years ago
Posts: 1549
 

@will 

Of course, I forgot, I remember doing this before but the instructions were given as to how to do it rather presuming I knew how.

 


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2111
 

@robotbuilder 

We're all here to help each other 🙂

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 3 years ago
Posts: 1549
 

@mrclassicman 

 If you see nothing glaringly wrong in the code, I can ask no more.

If it is Bill's code then it must work if it worked for him.  If you are using different hardware then remember hardware and coding must match. The code can look and compile without it working if the hardware is different or is wired incorrectly.

 

 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 3 years ago
Posts: 1549
 

@will 

It is possible that @mrclassicman doesn't know how to implement your suggestion.

I don't know if this will work or not.

I used ************ to highlight where the changes were made.

 

/*
  PWM Motor Test - High Frequency
  pwm_motortest_hi.ino
  Uses LCD Keypad module
  Runs at higher PWM frequency
  Modified from Cytron example code
  setPwmFrequency from macegr of the Arduino forum
  
  DroneBot Workshop 2019
   https://dronebotworkshop.com 

   Modified by robotBuilder to print to Arduino IDE Monitor 
  
*/

// Include libraries for LCD Display
#include <LiquidCrystal.h>
#include <LCD_Key.h>

// Define keycodes
#define None     0
#define Select   1
#define Left     2
#define Up       3
#define Down     4
#define Right    5

// Pin for analog keypad
#define diy_pwm  A2
// PWM output pin
#define pwm      3
// Motor direction pin
#define dir      2

// Define LCD display connections
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// Setup LCD Keypad object
LCD_Key keypad;

// Variable to represent PWM value
int pwm_value = 0; 

// Variable to represent Keypad value
int localKey;       

void setup(){
 
  Serial.begin(9600);   // add this *********************
  
  // Setup LCD	
  lcd.begin(16, 2);
  lcd.clear();
  
  // Set the PWM Frequency	
  setPwmFrequency(pwm, 8);
  
  // Define Pins
  pinMode(pwm,OUTPUT);
  pinMode(dir,OUTPUT);

}

void loop()
{
  while(1)
  {
   // Scan keypad to determine which button was pressed
   localKey = keypad.getKey();    
   
   // Toggle motor direction if LEFT button pressed
   if(localKey==Left){      
     digitalWrite(dir,!digitalRead(dir));
     delay(200);}

   // Increase motor speed if UP button pressed
   if(localKey==Up){      
     pwm_value++;
     delay(200);
   //  lcd.clear();}
   
   // Decrease motor speed if DOWN button pressed
   else if(localKey==Down){   
     pwm_value--;
     delay(200);
   //  lcd.clear();}
   
   // Ensure PWM value ranges from 0 to 255
   if(pwm_value>255)      
     pwm_value= 255; 
   else if(pwm_value<0)
     pwm_value= 0;
   
   // Send PWM to output pin
   analogWrite(pwm,pwm_value);

   // print to IDE Monitor instead of LCD  **********
   Serial.print("PWM:");
   Serial.println(pwm_value);
   Serial.print("DIR:");
   Serial.println(digitalRead(dir));
   //
   
   // Display results on LCD
  // lcd.setCursor(0,0);      
  // lcd.print("PWM:");
  // lcd.print(pwm_value);
  // lcd.setCursor(0,1);
  // lcd.print("DIR:");
  // lcd.print(digitalRead(dir));
  }
}

void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x07; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}


 

 


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2111
 
Posted by: @robotbuilder

@will 

It is possible that @mrclassicman doesn't know how to implement your suggestion

I don't know if this will work or not.

I wanted to see what the keypad returns as the value localKey. I suspect that this value is not being set correctly and, if that's the case, then nothing else will work as expected (which seems to be the case now).

If the correct key codes ARE being returned, then the keypad shield may be presumed to be OK.

Apparently my instructions (repeated twice) were not sufficiently clear, so I'll stop pursuing this topic.

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
mrclassicman
(@mrclassicman)
Eminent Member
Joined: 1 year ago
Posts: 31
Topic starter  

@will I have implemented your suggestion Serial.begin(9600),Serial.print("localKey=");Serial.println(localKey);

The printout continues with "0" until I press "right" key which returns the number 5. None of the other switches produce any value or change.

It took me a while to figure out exactly, even with your instructions, where to put these lines.

 

Terry  


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2111
 

@mrclassicman 

I'm sorry I couldn't make the instructions clear enough, my bad.

As I thought, the keys are not being processed correctly, so the rest of the sketch is pretty much out of luck.

This is probably because of a mismatch between the shield Bill used in the original and the one you are using. The shields vary in the LCD used and in the way that the buttons are interpreted and converted to a numeric value.

Since you seem to feel that the LCD will not be required in the future, you may be able to get rid of the shield and use the serial monitor to test and debug the rest of the sketch.

You will need to wire up real buttons to replace those on the shield. The easiest way to do that is to wire one end of the button to an Arduino pin and the other end to Arduino GND. Then declare the pin value like

#define leftPin  5

and in your setup() method, use the command

pinMode(leftPin,INPUT_PULLUP);

This will tell the Arduino to add a 10K internal pullup resistor to this pin and so it will always show as HIGH because it's tied internally to 5V. When you press the button, it will then be tied directly to GND which will bring the value to LOW.

So, when you read the button with digitalRead(leftPin):

if the value is HIGH, then the button is not pressed

if the value is LOW, then the button is pressed

Is that clear ?

 

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
mrclassicman
(@mrclassicman)
Eminent Member
Joined: 1 year ago
Posts: 31
Topic starter  

I only get the "right" key value when in the serial monitor, not on the LCD screen. I am sorry I did not include this in original response. 


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2111
 

@mrclassicman 

The "right" key (5) is never used in the sketch, so the fact that it doesn't change the LCD output isn't astonishing 🙂

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
mrclassicman
(@mrclassicman)
Eminent Member
Joined: 1 year ago
Posts: 31
Topic starter  

@will That is awesome, thank you so much. So many manufacturers build these things it is very difficult to address this in the code. I buy my stuff locally when possible as brick and mortar stores are important imo. Doing the switches directly to the Arduino should be a breeze. Just as a little bit of extra information that I had discovered was the values the keys returned with the grabkey example. 

My values:                 

Select=638

Left=407

Down=256

Up=99

Right=0

These were not the values Bill reported were returned on an earlier project with the same shield

His Values:

Select=741

Left=504

Down=329

Up=144

Right=0

I don't know if this is significant unless I was attempting to assign variable ranges for each of the keys.

Thanks you for your patience, I can be a handful for certain!

Terry

 


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2111
 

@mrclassicman 

As I understand it, the keypads usually use a set of resistors between the buttons. So, when each button is pressed, you can tell by the voltage how much resistance is present and thereby deduce the button which would have that much resistance.

Because each board is different, the resistor net depends on the manufacturer and isn't guaranteed to yield the same results.

I've never used one, so I don't know what the grabkey value represents, but it does fit nicely into the theory if you look at the grabkey value as a proportional voltage on a 0-1023 scale (like the Arduino's analog pin input values). The differences between your values and Bill's values illustrate the different resistors used by the shield makers.

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
mrclassicman
(@mrclassicman)
Eminent Member
Joined: 1 year ago
Posts: 31
Topic starter  

Thank you both Will and Robotbuilder! Your time, patience, assistance, and insights. I did not think about using the serial monitor in the diagnostics for this. The Serial monitor is a very helpful tool! I need to put in a delay next time so everything doesn't just flash by! lol. Again, Thanks Big Time for all your help and Will a super big shout out for the additional instruction for the switches. We can put this issue to bed now.

 

Terry


   
ReplyQuote
Page 2 / 3