Display Serial Output on LCD

I have a project that I am working on with the REDBOARD and the EASYDRIVER. I have the code established almost to teh point where I am satisfied that it will work, but then I got an idea to “Improve” it (I know, I know) by adding an LCD screen to display the output.

When I activate the code If I open the Serial Monitor and type in a position command (SET “NUMBER”), the stepper will drive to that position and print out (on the Serial Monitor) the currrent position number.

What I would like to do would be to have the position print to an LCD screen rather than have to be connected to a computer. This would allow me to miniatureize the package and run on battery power so that the unit could be more portable.

My question is how to interface the LCD Screen into the project so that it would display current position number. Would it be best if I used a shield? or can I work with just the REDBOARD and the EASYDRIVER?

Thanks for any suggestions.

The easiest approach is to use a serial LCD, something like this: https://www.sparkfun.com/products/9395

Or for the same price you get an Arduino on the same board: https://www.sparkfun.com/products/10097

I Guess I should also add that the pins I am currently using are PIN 2,3,4 on the POWER side, +5VDC and GND. I think this leaves me enough pins to hook up the LCD, but I am not sure how I would go about that. I also have two grounds but I think that eliminating the on on the Pin Side will be ok. The circuit works without it so I think it is merely redundant. Step and Direction work fine. Here is the way it is setup for now;

Also should add that I am very new to Arduino and although I have burned an awful lot of midnight oil, I am in no way savvy on where a lot of the documentation is for various things, especially how to go about hooking up different components. So if I am being “stupid” please enlighten me (or at least shove me in the general direction!). Thanks

[jremington]

I have the lcd screen (bare bones version). I assume that I have to declare the library in the opening sketch and then tell it (somehow) to port the serial data to the screen. Am I close? I guess I am asking what the code would be to tell the ARDUINO to send the serial data that the keypress returns to the screen.

Ok, I have managed to get the two sketches merged. Individually, they work just fine. Combined, they will compile error free, but now the stepper motor will not function. I have triple and quadruple checked my wiring and that is NOT the problem, so, as I suspect, there is something wonky in the code. Can someone have a peek and see if I have made some un-seeable blunder? ( I can post a schematic of all the wiring for the Easydriver and the LCD if that will help.)

Code begins;

#include <LiquidCrystal.h>;  
  LiquidCrystal lcd (12,11,5,4,3,2);

#define DIR_PIN 8
#define STEP_PIN 9
#define POWER_PIN 10

int CurrentFilter = 0;  //Holds the Current position
int Direction = LOW;	// Starts Clockwise Set HIGH for CCW

volatile long NoOfSteps = 0; //required number of steps to make
volatile long Position = 0; //track motor position

String cmd;	// Place Holder for string from serial.

void setup()  
{
lcd.begin (16,2);
//initialize serial communications
  Serial.begin(9600);
  Serial.flush();
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT); 
  pinMode(POWER_PIN, OUTPUT); 
}
void loop()  
{ if (Serial.available() > 0)		// read in buffer.
  {
   // wait 
    delay(100);
    // clear the screen
    lcd.clear();
    // read all character data
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
//    ********
    cmd=Serial.readStringUntil('#');		// #  "end of line" character.  
    if (cmd=="GETP")	// Show Current Position
    {
      Serial.print(CurrentFilter);
      Serial.println("#");
    }
        else if (cmd=="SETP0") SetFilter(0);  // Set position to correct number.
    else if (cmd=="SETP1") SetFilter(1);
    else if (cmd=="SETP2") SetFilter(2);
    else if (cmd=="SETP3") SetFilter(3);
    else if (cmd=="SETP4") SetFilter(4);
  }
}
void SetFilter(int FilterNum)
{

  if (FilterNum == CurrentFilter)	// Display #0
  {
    Serial.println("0#");
  }
  else if (FilterNum < CurrentFilter) 	// New filter lower than Current, direction LOW (High for CCW)
    Direction = LOW;
    NoOfSteps = Position - (FilterNum * 735);		
    Position= Position - NoOfSteps;
  }
  else		// New filter lower than Current, direction HIGH : (LOW for CW)
  {
    Direction = HIGH;
    NoOfSteps = (FilterNum * 720) - Position;
    EasyDriverStep(Direction, NoOfSteps);
    Position= Position + NoOfSteps;
  }

  CurrentFilter = FilterNum;  
  Serial.println("0#");		// Display #0 to confirm
}

// The EasyDriverStep function

void EasyDriverStep(int dir,long steps){
  digitalWrite(POWER_PIN, LOW); // enable power to the EasyDriver
  delayMicroseconds(10); //wait
  digitalWrite(DIR_PIN,dir);	// Set the direction.
  delay(100);	// Wait

// Move the motor
  for(int i=0;i<steps;i++){
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(600);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(600);
  }
 delay(500);
 digitalWrite(POWER_PIN, HIGH);		// Turn off the EasyDriver
}

The code is sending back data over the serial port. Have you looked at that and compared it to how the code processing should flow?

We can’t explain what it is doing if we don’t know what you have tried to send to it.

Will check tonight and report back. At about 4Am I “THINK” I had a breakthrough, but by that time, I may have just been hallucinating :lol:

Please edit your post in the full editor and insert that code between [code ] [/ code] tags. You can use the Code button to place it in the window. It shortens the post to make it easier to read. Also the code comes out better.

Very Good. Thanks for that tip.

When the code is uploaded, I open the Serial Monitor and issue the command “SETP#” (# corresponding to the position I want the Stepper motor to go to). The Serial Monitor will then return a #0 as a verification (I’d like it to return #Position Number, but that can wait until I get everything else sorted out.

The Command “GETP” simply returns the Position number that is in memory (last position gone to).

MY idea was that if I can get the “SETP#” to display on the LCD, I will always know my last position rather than having to type in “GETP”.

The Stepper goes a pre-programmed number of steps to index the position wheel. I have been tossing around the idea of just using a Joystick to actuate the stepper and have it run a preprogrammed number of steps. Of course, I think it’s best to get THIS issue sorted out before going off on a tangent.

So where I am now is that as I said, Individually, the sketches work, but when combined, the LCD will display position number but stepper motor will not move.

I suppose that I should really condense all of this into a neater package for anyone who would like to examine the code for both programs and see if the combining of them contains a problem that I’m just not seeing.

I appreciate the help and advice. Thanks again.

Since you merged the 2 pieces of code, how it worked individually is not important any more. Is your description of how it actually works now, or how you want it to work? I would like to know what it actually does based on your inputs. Described in the exact chronological order.

Ok,

  1. What I want it to do:

(Via the Serial Monitor)

I want to input (via Keyboard on computer) a specific position (i.e. SETP1, SETP3, etc). Inputting this from the keyboard will cause the stepper motor to move a pre-determined number of steps (i.e. 735). When this is done (or as this is done), I want the command to be displayed on the LCD.

  1. What it’s doing:

(Via the Serial Monitor)

When I input “SETP#”, the LCD will display the input command on the screen, but the stepper motor does not move.

  1. What it did before merging;

The individual programs worked as expected. Only when they were merged, did the problem with the stepper arise.

I will upload the individual sketches later this evening when I get back to the office. Maybe you can see something.

Thanks for your help.

hrefab:
2) What it’s doing:

(Via the Serial Monitor)

When I input “SETP#”, the LCD will display the input command on the screen, but the stepper motor does not move.

Well, it won’t respond by moving the motor if cmd is exactly “SETP”. The terminator character # does not get included into the variable cmd.

At first looks, the loop code boils down to this:

If there is any data in the serial read buffer, (it waits 100 miliseconds and clears the lcd screen and…) it’s content and any new data gets dumped to the LCD screen until it is emptied. If any command was sent here it will be ignored. Any partial command would mess up what comes next.

Then it waits for new serial data for 1 second (default timeout) until the # character is received, and puts what is before it into the variable cmd.

If cmd is exactly “GETP” then it prints the CurrentFilter variable, followed by a #, (if not the following…)

If cmd is subsequently exactly “SETP0” is executes SetFilter(0), (if not the following…)

If cmd is subsequently exactly “SETP1” is executes SetFilter(1), (if not the following…)

etc. until SetFilter(4)

The SetFilter code itself looks ok. (except for the second else comment line; it implies filter is lower than Current but it’s actually higher at this else condition) It calls the following EasyDriverStep function depending on the filter selected.

The EasyDriverStep code looks ok too. You probably didn’t change that code and it has run before.

So that means the stepper isn’t turning because the conditions of the if-statements in loop() never matched what you expect cmd to be. You assume it is exactly “GETP” or “SETP#” or whatever number #. But somehow that cmd string is filled with something else unexpected. There may be more characters in it, there may be wrong characters in between (unlikely but potentially posible) or there may be less in it. The simple way to find that out and prove it is by sending back what is in the variable cmd after that is determined.

The while loop dumping the buffer to lcd seems inapropriate. You loose whatever serial commands are sent to it. I don’t understand the need for the delay(100) either, the readStringUntil waits long enough. Remove the while loop. Once you detected the # character and the string cmd is filled, print that to the LCD (and serial port for testing). That should clear up the mists of what is going on exactly.

p.s. You still have color tags in the code window. Those color changes do not work in the code window. If you must use that, then make use of quote tags.

Fixed

Well, it won’t respond by moving the motor if cmd is exactly “SETP”. The terminator character # does not get included into the variable cmd.

Just to be clear, I am NOT entering “SETP#”, I simply used that as a placeholder. I see where that could be confusing. The command I enter is “SETP1” or “SETP2”… etc. these commands SHOULD cause the stepper to actuate and go to the pre-determined number of steps.

Fair enough, but if you never send a “#” character behind it then it never will be considered a valid command, and cmd never gets filled. Then that is your problem.

Valen:
Fair enough, but if you never send a “#” character behind it then it never will be considered a valid command, and cmd never gets filled. Then that is your problem.

I’m a bit confused here with the above reply. When I load the sketch, if I type “SETP1” (No Quotes) into the command line in the Serial Monitor, the Stepper motor DOES run the prescribed number of steps, ONLY if I do NOT have the LCD connected. (IN other words, when I only load the stepper sketch.

If I load the LCD Sketch (single) then the Commands entered on the Serial Monitor are echo’d to the LCD screen.

It is when I try to merge the two sketches that I get the problem with the stepper NOT working when I enter the Command on the Serial Monitor.(Type “SETP1” (No Quotes) and press ENTER).

The goal is to get the command to print to the LCD and have the stepper motor move accordingly

What I am trying to get sorted out is where in the code and WHAT in the code do I need to do to accomplish this.

If you keep reverting back to the individual sketches then you will keep being confused. You should focus on the merged sketch, as that is the goal. Like I said, get the contents of the variable cmd back to the pc or print it on the LCD. That has the answer of why it is not working.

In the merged sketch the variable DOES print to the LCD. It’s the Stepper that doesn’t work. I’ll have to try to debug this a line at a time. Speaking of which, is there a utility that will single step through a sketch? I’m not so experienced with sketches that I can just pull the answer out of a hat. I’m trying to learn and understand, so I have a lot of reading and research to do. Thank you for the advice.

No, the variable cmd does not print to lcd! No where in the code do I see lcd.print(cmd).

What does happen is that the serial buffer gets emptied by printing character by character to the lcd in the while loop. And this may contain the command that you sent to it. But then it can’t go into the variable cmd anymore. So whatever these if-statements in loop() are basing their judgements on it’s not the command you sent. And so it does not move any motors.

I’m sorry, I don’t know of such plugins or mods for the Arduino IDE. It’s not very sophisticated as a development environment. Maybe AVR Studio can do step-run analysis, but I don’t know if it can compile Arduino code as is without problems.