Go Down

Topic: Stepper Controller with Keypad and LCD (Read 2 times) previous topic - next topic

Metalfan1185

ok, I fixed the comparison =='s as mentioned previously (Thanks for that) and added some comments to the top as to what is left.

Here is the code as it stands now (still workin on it lol)

Code: [Select]
//Josh Nolan
//Stepper Controller
//Started 2/10/11

//Things left:
// 1)find a fix that will convert the numeric keypad entried to integers so they can be used
//   by the subroutines and strings so they can be displayed on the LCD as they are entered.
//
// 2)fix the motor start part.  Maybe have something like "Vals ok, Press enter to start!"
//   
// 3)look into how this thing is going to act after the motor runs finish.  can I hold the
//   steps per full revolution value and reset the others for reentry? or reset the whole
//   thing?
//
// 4)figure out the pin assignments and build it up.  If I can use an 328 I will, if not Ill
//   hook it up to the Mega.



#include <Stepper.h>
#include <LCD4Bit.h>
#include <Keypad.h>

LCD4Bit lcd = LCD4Bit(2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','<'},
  {'C','0','.','E'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);   //can i change 'stepsPerRevolution' after init from main loop or setup?
                                                    //If I did it from setup would I have to reset somehow?   
                                                   
int stepsToGo = 0;         // number of steps the motor has/will take
int stepsToGoOK = 0;
int totalSteps = 0;        //Total number of steps, later used as 'stepsPerRevolution'
int totalStepsOK = 0;
int stepSpeed = 0;         // holds stepper speed entry from keypad
int stepSpeedOK = 0;
int entrydone = 0;         //indicator to tell me that an entry is completed
int toggleReverse = 0;
int runCount = 0;
String entry = "";         //combined keypad characters to form a int for values

void setup(){
  lcd.init();
  lcd.clear();
  Serial.begin(9600);   //for debugging, first time attempting using serial in this manner
}

void loop(){
  while (totalStepsOK < 1) {
    EnterSteps();}
  while (stepSpeedOK < 1) {
    EnterSpeed();}
  while (stepsToGoOK < 1) {
    StepsToGo();}
  while (runCount == 0);
    RunCounter();}
   
  if (runCount > 0) {
  lcd.clear();
  lcd.cursorTo(1, 0);
  lcd.printIn("Step Initialized");
  lcd.cursorTo(2, 0);
  delay(1000);
  while (runCount > 0) {
     lcd.cursorTo(2, 0);
     lcd.printIn(runcount);
     lc
            motorStart();
            runCount--;
         }
        }
char key = cusomKeypad.getKey();
  if (key != NO_KEY){
     switch (key) {
            case 'E':  //would like to later use + and - as increment/decrement
            lcd.clear();
            lcd.cursorTo(1, 0);
            lcd.printIn("-Ready to Run-")
            delay(1000);
            lcd.cursorTo(2, 0);
            lcd.printIn("(ENTER to Start)")
            break;
            case 'C':
           
            break;
     }
  }
}


void GetEntry(){  //universal entry getter to be called on for values
entrydone = 0;
  if (entrydone == 0){
    char key = cusomKeypad.getKey();
       if (key != NO_KEY){
          switch (key) {
            case '+':  //would like to later use + and - as increment/decrement
            break;
            case '-':
               if (totalStepsOK == 1) {
                  if (stepSpeedOK == 1) {
                      if (toggleReverse == 0){
                         toggleReverse = 1;
                         }
                      if (toggleReverse == 1){
                         toggleReverse = 0;
                      }
                  }
               }
            break;
            case '<':
            break;
            case 'C':
               lcd.clear();
               lcd.cursorTo(1, 0);
               entry = "000";
               lcd.printIn("Entry Cleared");
            break;
            case '.':
               lcd.clear();
               lcd.cursorTo(1, 0);
               lcd.printIn("Invalid Keypress");
               delay(1000);
            break;
            case 'E':
               if (entry == "000"){
                  lcd.clear();
                  lcd.cursorTo(1, 0);
                  lcd.printIn("Invalid Entry");
                  delay(1000);
                  //possible break needed here?
               }
               else {
                  entrydone++;
                  Serial.print("Entry Completed");
               }
            break;
           
    }
  //lcd.printIn(" " + key);
  Serial.println(key);
  entry = String(entry + key);
   }
  }
}
  void EnterSteps(){ //To get the Total number of steps per revolution of motor under test
  int x;
  if (totalSteps != 0){
  lcd.clear();
  lcd.cursorTo(1, 0);
  lcd.printIn("Enter Steps Per");
  lcd.cursorTo(2, 0);
  lcd.printIn("Revolution:");
  lcd.cursorTo(2, 12);
  GetEntry();
  if (entrydone == 1){
     //int x = atoi(entry);
     totalSteps = x;
  Serial.print("EnterSteps ok");
  }
}
}
  void EnterSpeed(){ //To get motor Speed (Range - 0 to ???) range has to be filtered to usable values
  int y;
  lcd.clear();
  lcd.cursorTo(1, 0);
  lcd.printIn("Enter Stepper");
  lcd.cursorTo(2, 0);
  lcd.printIn("Speed:");
  lcd.cursorTo(2, 12);
  GetEntry();
  if (entrydone == 1){
     //int y = atoi(entry);
     stepSpeed = y;
   Serial.println("EnterSpeed ok");
  }
}
  void StepsToGo(){
  lcd.clear();
  lcd.cursorTo(1, 0);
  lcd.printIn("Enter Steps to Go");
  lcd.cursorTo(2, 0);
  lcd.printIn("(- for rev)");
  lcd.cursorTo(2, 12);
  GetEntry();
  }
 
  void RunCounter(){
  lcd.clear();
  lcd.cursorTo(1, 0);
  lcd.printIn("Enter Number of");
  lcd.cursorTo(2, 0);
  lcd.printIn("Motor Runs:");
  lcd.cursorTo(2, 12);
  GetEntry();
  if (entry > 0){
     entry = runCount;
  }
  else{
     lcd.clear();
     lcd.cursorTo(1, 0);
     lcd.printIn("Need + Run Count");
  }
  }
 
  void motorStart() {  //I want to move-it move-it! lol
  if (toggleReverse = 1){
     myStepper.step(-stepsToGo);
  }
  else{
     myStepper.step(stepsToGo);
  }
}
 
 
 



Metalfan1185

#6
Feb 20, 2011, 05:09 am Last Edit: Feb 20, 2011, 05:31 am by Metalfan1185 Reason: 1
Update:

lots fixed from last post.  Also here's a screenshot of an error I get while trying to send some text and a string to the LCD with atoi():



Code: [Select]
//Josh Nolan
//Stepper Controller
//Started 2/10/11

//Things left:
// 1)find a fix that will convert the numeric keypad entried to integers so they can be used
//   by the subroutines and strings so they can be displayed on the LCD as they are entered.
//
// 2)fix the motor start part.  Maybe have something like "Vals ok, Press enter to start!"
//  
// 3)look into how this thing is going to act after the motor runs finish.  can I hold the
//   steps per full revolution value and reset the others for reentry? or reset the whole
//   thing?
//
// 4)figure out the pin assignments and build it up.  If I can use an 328 I will, if not Ill
//   hook it up to the Mega.



#include <Stepper.h>
#include <LCD4Bit.h>
#include <Keypad.h>

LCD4Bit lcd = LCD4Bit(2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
 {'1','2','3','+'},
 {'4','5','6','-'},
 {'7','8','9','<'},
 {'C','0','.','E'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);   //can i change 'stepsPerRevolution' after init from main loop or setup?
                                                   //If I did it from setup would I have to reset somehow?  
                                                   
int stepsToGo = 0;         // number of steps the motor has/will take
int stepsToGoOK = 0;
int totalSteps = 0;        //Total number of steps, later used as 'stepsPerRevolution'
int totalStepsOK = 0;
int stepSpeed = 0;         // holds stepper speed entry from keypad
int stepSpeedOK = 0;
int entrydone = 0;         //indicator to tell me that an entry is completed
int toggleReverse = 0;
int runCount = 0;
int motorFinished = 0;
String entry = "";         //combined keypad characters to form a int for values

void setup(){
 lcd.init();
 lcd.clear();
 Serial.begin(9600);   //for debugging, first time attempting using serial in this manner
}

void loop(){
 while (totalStepsOK < 1) {    //run each routine until a suitable value is obtained, then on to the next
   EnterSteps();}
 while (stepSpeedOK < 1) {
   EnterSpeed();}
 while (stepsToGoOK < 1) {
   StepsToGo();}
 while (runCount == 0);{
   RunCounter();}
   
 if (totalStepsOK == 1){
    if (stepSpeedOK == 1){
       if (stepsToGoOK == 1){
          if (runCount != 0) {
             if (motorFinished == 0) {
                //Motor is ready to run
                lcd.clear();
                lcd.cursorTo(1, 0);
                lcd.printIn("Values Ready!");
                delay(1000);
                lcd.clear();
                lcd.cursorTo(1, 0);
                lcd.printIn("ENTER To Start");
                lcd.cursorTo(2, 0);
                lcd.printIn("CLEAR To Reset");
                char key = cusomKeypad.getKey();
                   if (key != NO_KEY){
                      switch (key) {
                         case 'E':  //would like to later use + and - as increment/decrement
                         while (runCount > 0){
                            lcd.clear();
                            lcd.cursorTo(1, 0);
                            lcd.printIn("Stepper Running!");
                            lcd.cursorTo(2, 0);
                            lcd.printIn("Runs To Go:  " + atoi(runCount));
                            motorStart();
                            delay(1000);
                            runCount--;
                            if (runCount = 0) {
                               motorFinished = 1;
                            }
                         }
                         break;
                         case 'C':
                            lcd.clear();
                            lcd.cursorTo(1, 0);
                            lcd.printIn("Values Cleared!");
                            stepsToGo = 0;
                            stepsToGoOK = 0;
                            totalSteps = 0;
                            totalStepsOK = 0;
                            stepSpeed = 0;
                            stepSpeedOK = 0;
                            entrydone = 0;
                            toggleReverse = 0;
                            runCount = 0;
                            motorFinished = 0;
                            entry = "";
                         break;
      }
     }
    }
   }
  }
 }
}
}

void GetEntry(){  //universal entry getter to be called on for values
entrydone = 0;
 if (entrydone == 0){
   char key = cusomKeypad.getKey();
      if (key != NO_KEY){
         switch (key) {
           case '+':  //would like to later use + and - as increment/decrement
           break;
           case '-':
              if (totalStepsOK == 1) {
                 if (stepSpeedOK == 1) {
                     if (toggleReverse == 0){
                        toggleReverse = 1;
                        }
                     if (toggleReverse == 1){
                        toggleReverse = 0;
                     }
                 }
              }
           break;
           case 'C':
              lcd.clear();
              lcd.cursorTo(1, 0);
              entry = "000";
              lcd.printIn("Entry Cleared");
           break;
           case '.':
              lcd.clear();
              lcd.cursorTo(1, 0);
              lcd.printIn("Invalid Keypress");
              delay(1000);
           break;
           case 'E':
              if (entry == "000"){
                 lcd.clear();
                 lcd.cursorTo(1, 0);
                 lcd.printIn("Invalid Entry");
                 delay(1000);
                 //possible break needed here?
              }
              else {
                 entrydone++;
                 Serial.print("Entry Completed");
              }
           break;
           
   }
 //lcd.printIn(" " + key);
 Serial.println(key);
 entry = String(entry + key);
  }
 }
}

 void EnterSteps(){ //To get the Total number of steps per revolution of motor under test
 int x;
 if (totalSteps != 0){
 lcd.clear();
 lcd.cursorTo(1, 0);
 lcd.printIn("Enter Steps Per");
 lcd.cursorTo(2, 0);
 lcd.printIn("Revolution:");
 lcd.cursorTo(2, 12);
 GetEntry();
 if (entrydone == 1){
    //int x = atoi(entry);
    totalSteps = x;
 Serial.print("EnterSteps ok");
 }
}
}

 void EnterSpeed(){ //To get motor Speed (Range - 0 to ???) range has to be filtered to usable values
 int y;
 lcd.clear();
 lcd.cursorTo(1, 0);
 lcd.printIn("Enter Stepper");
 lcd.cursorTo(2, 0);
 lcd.printIn("Speed:");
 lcd.cursorTo(2, 12);
 GetEntry();
 if (entrydone == 1){
    //int y = atoi(entry);
    stepSpeed = y;
  Serial.println("EnterSpeed ok");
 }
}

 void StepsToGo(){
 lcd.clear();
 lcd.cursorTo(1, 0);
 lcd.printIn("Enter Steps to Go");
 lcd.cursorTo(2, 0);
 lcd.printIn("(- for rev)");
 lcd.cursorTo(2, 12);
 GetEntry();
 }
 
 void RunCounter(){
 lcd.clear();
 lcd.cursorTo(1, 0);
 lcd.printIn("Enter Number of");
 lcd.cursorTo(2, 0);
 lcd.printIn("Motor Runs:");
 lcd.cursorTo(2, 12);
 GetEntry();
 if (entry > 0){
    entry = runCount;
 }
 else{
    lcd.clear();
    lcd.cursorTo(1, 0);
    lcd.printIn("Need + Run Count");
 }
 }
 
 void motorStart() {  //I want to move-it move-it! lol
 if (toggleReverse = 1){
    myStepper.step(-stepsToGo);
 }
 else{
    myStepper.step(stepsToGo);
 }
}
 
 
 




Here's another error I get, again with the conversions of the entry recieved from the keypad (string) to a usable numeric value (integer) I just cant seem to figure this one out, but I need it in a few places.  Really need some help on this...


liudr

The first one was simply not C or C++, it is a BASIC or Java format. You can't use a "+" to concatenate strings or convert numbers to strings and concatenate them. You may have indulged yourself with Java too much, but to use C or C++, you should use:

sprintf(target_string, format, list of parameters);
like:

sprintf(out_str, "%s %d", "Runs to go", runCount);
Then you can lcd.print(out_str);

You should check this link:

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

The second one, you simply can't do that. READ the reference of atoi()

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

Metalfan1185

#8
Feb 20, 2011, 01:18 pm Last Edit: Feb 20, 2011, 01:22 pm by Metalfan1185 Reason: 1
Thanks for the reply. and yes I have played with some java and BASIC recently lol.

So, there's no way that I can start a string variable, and add a numeral every keypress for instance:

(pressed 1)
x = "1"

(pressed 2)
x = "12"

(pressed 7)
x = "127"

(pressed Enter)
stepper.setspeed(x); for instance wont work because its a string, not an int.



and then somehow convert this string to an integer?  I understand obviously that i cant give it a "-" or "." or anything not numeric, but I hope there's a way to convert this...

Metalfan1185


Again, but this time, you are trying to convert entry (a string) to an int (holder) using atoi, atoi needs a char, not a string.

Someone help me here, but I also think that the Case statement is designed for numbers, not characters.




So If I can convert single characters and not the whole string at once, would I be able to set up atoi() to convert each character on keypress before that number is added to the entry string (well, it would be int at that point if it can be done)

Go Up