Control stepper motor by compare two input data.

Hello,
I am beginner in Arduino programming. i have this code below:

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define dirPin 12
#define stepPin 9
#define enPin 13
int pdd;


void setup()
{   Serial.begin(9600); 
    lcd.init();                     
    lcd.backlight(); 
}

void loop()
{
  int sval = getSensorvalue();
  lcd.setCursor(0,1);
  lcd.print("S.Value:");
  lcd.print(sval);
  int pdd = GetNumber();
  lcd.setCursor(0,0);
  lcd.print("D.Value:");
  lcd.print(pdd);
   
   motor_keypad();
   
 if (pdd>(sval+10))
 {
  motor_rotate_right();
 }
 else if (pdd<(sval-10))
 {
  motor_rotate_left();
 }
 else
  {
     motor_sationary();
    }
}

int getSensorvalue()
{
  int pval = analogRead(A0);
  int val = map (pval, 0, 1023, 0, 200);
  Serial.println(val);
  //lcd.setCursor(0,1);
  //lcd.print("Value");
  //lcd.print(val);
  return val;
  }
      
int GetNumber()
{ 
   int num = 0;
   char key = keypad.getKey();
   while(key != '#')
   {
      switch (key)
      {
         case NO_KEY:
            break;

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
           // lcd.print(key);
            num = num * 10 + (key - '0');
            break;

         case '*':
            //num = 0;
            lcd.clear();
            break;
      }

      key = keypad.getKey();
   }

   return num;
}

 int motor_keypad()
    {
      pinMode(dirPin, OUTPUT);
      pinMode(stepPin, OUTPUT);
      pinMode(enPin, OUTPUT);
      digitalWrite(enPin, HIGH);
      digitalWrite(dirPin, HIGH);
      for(int x = 0; x < pdd; x++)
       {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(2000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(2000);
   }   
   delay(1000);
}


 int motor_rotate_right()
   {
       digitalWrite(enPin, HIGH);
       digitalWrite(dirPin, HIGH);
       for(int x = 0; x < 200; x++)
          {
             digitalWrite(stepPin,HIGH);
             delayMicroseconds(2000);
             digitalWrite(stepPin, LOW);
             delayMicroseconds(2000); 
          } 
          delay(1000);
   }

int motor_rotate_left() 
     {
       digitalWrite(enPin, HIGH);
       digitalWrite(dirPin, LOW);
       for(int x = 0; x < 200; x++)
          {
             digitalWrite(stepPin,HIGH);
             delayMicroseconds(2000);
             digitalWrite(stepPin, LOW);
             delayMicroseconds(2000);
          }
           delay(1000); 
      }

 int motor_sationary()
      {
        digitalWrite(enPin, LOW);
      }

The code is uploaded, but it not compare with keypad input ( int pdd = GetNumber() ) with sensor input ( int sval = getSensorvalue() ) and (void loop() )code is not work properly.

Please guide me.

Thanks

rksardckt2708:
The code is uploaded, but it not compare with keypad input ( int pdd = GetNumber() ) with sensor input ( int sval = getSensorvalue() ) and (void loop() )code is not work properly.

You need to explain clearly what you want the program to do. "not work properly" does not tell us that.

And a good description of the overall project will make it much easier to help you.

...R

Hi,
Welcome to the forum.

Thanks for using code tags.

What is your code supposed to do?
What does your code do?
What model Arduino are you using?

Did you write this code, if not can you post a link to where you got it please?

Can you please post a circuit diagram?

Thanks.. Tom.. :slight_smile:

Robin2:
You need to explain clearly what you want the program to do. "not work properly" does not tell us that.

And a good description of the overall project will make it much easier to help you.

...R

Sir,
Firstly when i enter input keypad then stepper motor rotate . Then After input keypad compare with sensor input value and rotate stepper motor according to given conditions.
But in this given code input keypad and sensor value not compare and given condition is not execute.

TomGeorge:
Hi,
Welcome to the forum.

Thanks for using code tags.

What is your code supposed to do?
What does your code do?
What model Arduino are you using?

Did you write this code, if not can you post a link to where you got it please?

Can you please post a circuit diagram?

Thanks.. Tom.. :slight_smile:

Sir,
Firstly when i enter input keypad then stepper motor rotate . Then After input keypad compare with sensor input value and rotate stepper motor according to given conditions.

But in this given code input keypad and sensor value not compare and given condition is not execute.

I am using Atmega 328p Arduino Uno .

I am try to write code not use any link

circuit diagram is attach below

rksardckt2708:
Firstly when i enter input keypad then stepper motor rotate . Then After input keypad compare with sensor input value and rotate stepper motor according to given conditions.

I'm still not clear what is supposed to happen - especially as regards when the different things are to happen

This code

 int sval = getSensorvalue();
  lcd.setCursor(0,1);
  lcd.print("S.Value:");
  lcd.print(sval);
  int pdd = GetNumber();
  lcd.setCursor(0,0);
  lcd.print("D.Value:");
  lcd.print(pdd);
   
   motor_keypad();
   
 if (pdd>(sval+10))

gets the sensor value and the keypad value and then gets the motor to move according to the keypad value. The movement will take some time and there is a long delay() at the end of it.

After that movement (and the delay) the value of the keypad is checked against the sensor value and depending on the value the motor will do another long move either right or left or the motor will be stopped without any delay.

My wild guess is that you either don't need the call to motor_keypad() at all, or it should be in a different place in the code - perhaps part of the block of IF tests.

Separately all those blocking motor moves and delay() are going to make the program very unresponsive.

The functions delay() and delayMicroseconds() as well as FOR and WHILE loops block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

And see Using millis() for timing. A beginners guide if you need more explanation.

Displaying values on the LCD in every iteration of loop() will also slow things down. Only update the LCD if the value changes.

...R

Robin2:
I'm still not clear what is supposed to happen - especially as regards when the different things are to happen

This code

 int sval = getSensorvalue();

lcd.setCursor(0,1);
 lcd.print("S.Value:");
 lcd.print(sval);
 int pdd = GetNumber();
 lcd.setCursor(0,0);
 lcd.print("D.Value:");
 lcd.print(pdd);
 
  motor_keypad();
 
if (pdd>(sval+10))



gets the sensor value and the keypad value and then gets the motor to move according to the keypad value. The movement will take some time and there is a long delay() at the end of it.

After that movement (and the delay) the value of the keypad is checked against the sensor value and depending on the value the motor will do another long move either right or left or the motor will be stopped without any delay.

My wild guess is that you either don't need the call to motor_keypad() at all, or it should be in a different place in the code - perhaps part of the block of IF tests.


Separately all those blocking motor moves and delay() are going to make the program very unresponsive.

The functions delay() and delayMicroseconds() as well as FOR and WHILE loops block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in [Several Things at a Time](http://forum.arduino.cc/index.php?topic=223286.0).

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

And see [Using millis() for timing. A beginners guide](http://forum.arduino.cc/index.php?topic=503368.0) if you need more explanation.


Displaying values on the LCD in every iteration of loop() will also slow things down. Only update the LCD if the value changes.

...R

Sir,
I am confused that where should i use millis() in this programming?

I want that motor should rotate according to the keypad value. After this it gets the sensor value then keypad is checked against the sensor value in IF condition. Motor should rotate according to if condition. Motor should stop until the sensor value get change.

Problems-

  1. On giving keypad value more than 57 numeric value, motor is not rotating.
  2. Motor is doing one complete rotation on giving any value upto 57.
  3. IF statement is unable to execute in void loop.

I have written two code separately -

  1. Enter multiple value using keypad
  2. Control stepper motor by manually entering value in programming and comparing obtained value against sensor value.

After this I merged this two codes. It Attached below

Thanks

Keypad_F.txt (1.47 KB)

Practice_3.txt (1.36 KB)

apard204:
Sir,
I am confused that where should i use millis() in this programming?

I want that motor should rotate according to the keypad value. After this it gets the sensor value then keypad is checked against the sensor value in IF condition. Motor should rotate according to if condition. Motor should stop until the sensor value get change.

First, there is no need to call me "Sir" - my name is Robin.

Let's skip the millis() question for the moment and concentrate on what you want the program to do.

It now seems to me that you want to start things off by entering a number from the keypad and the motor should do a complete move based on that number.

Then when that move is finished it must change to a different mode of operation in which it gets BOTH a sensor value and a keypad value and compares the the two values to decide how to move?

Is that correct?

If not please tell me what I have got wrong.

You have not actually described the project you are trying to create. It makes it much easier to give useful advice when we understand the complete project.

...R

Robin2:
First, there is no need to call me "Sir" - my name is Robin.

Let's skip the millis() question for the moment and concentrate on what you want the program to do.

It now seems to me that you want to start things off by entering a number from the keypad and the motor should do a complete move based on that number.

Then when that move is finished it must change to a different mode of operation in which it gets BOTH a sensor value and a keypad value and compares the the two values to decide how to move?

Is that correct?

If not please tell me what I have got wrong.

You have not actually described the project you are trying to create. It makes it much easier to give useful advice when we understand the complete project.

...R

Robin2:
First, there is no need to call me "Sir" - my name is Robin.

Let's skip the millis() question for the moment and concentrate on what you want the program to do.

It now seems to me that you want to start things off by entering a number from the keypad and the motor should do a complete move based on that number.

Then when that move is finished it must change to a different mode of operation in which it gets BOTH a sensor value and a keypad value and compares the the two values to decide how to move?

Is that correct?

If not please tell me what I have got wrong.

You have not actually described the project you are trying to create. It makes it much easier to give useful advice when we understand the complete project.

...R

Oh.... Sorry Robin
Yes, you are getting my point exactly.

apard204:
Yes, you are getting my point exactly.

Then I think you need to separate the first time the code runs from all the other times - perhaps like this.

At the top of the program create a variable

bool firstMoveComplete = false;

Then add this to the code in loop()

   lcd.setCursor(0,0);
  lcd.print("D.Value:");
  lcd.print(pdd);
   
   if (firstMoveComplete == false) {  // NEW
       motor_keypad();
       firstMoveComplete = true;
   }                                                 // NEW
   else {                                         // NEW 
   
      if (pdd>(sval+10))
      {
            motor_rotate_right();
      
      // other existing code

     else
     {
         motor_sationary();
      }
   }                                             // NEW - end of the new ELSE
}

...R

Robin2:
Then I think you need to separate the first time the code runs from all the other times - perhaps like this.

At the top of the program create a variable

bool firstMoveComplete = false;

Then add this to the code in loop()

   lcd.setCursor(0,0);

lcd.print("D.Value:");
 lcd.print(pdd);
 
  if (firstMoveComplete == false) {  // NEW
      motor_keypad();
      firstMoveComplete = true;
  }                                                 // NEW
  else {                                         // NEW
 
     if (pdd>(sval+10))
     {
           motor_rotate_right();
     
     // other existing code

else
    {
        motor_sationary();
     }
  }                                             // NEW - end of the new ELSE
}




...R
#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define dirPin 12
#define stepPin 9
#define enPin 13
int pdd;
bool firstMoveComplete = false;

void setup()
{   Serial.begin(9600); 
    lcd.init();                     
    lcd.backlight(); 
}

void loop()
{
   int pdd = GetNumber();
   lcd.setCursor(0,0);
   lcd.print("D.Value:");
   lcd.print(pdd);
     
      int sval = getSensorvalue();
      lcd.setCursor(0,1);
      lcd.print("S.Value:");
      lcd.print(sval);
   
   if (firstMoveComplete == false) {  // NEW
       motor_keypad();
       firstMoveComplete = true;
   }                                                 // NEW
   else {                                         // NEW
    
      if (pdd>(sval+10))
      {
            motor_rotate_right();    // other existing code
       }
    else if (pdd<(sval-10))
       {
          motor_rotate_left();
       }
      else
     {
         motor_sationary();
      }
    }                                             // NEW - end of the new ELSE
  }

int getSensorvalue()
{
  int pval = analogRead(A0);
  int val = map (pval, 0, 1023, 0, 200);
  Serial.println(val);
  //lcd.setCursor(0,1);
  //lcd.print("Value");
  //lcd.print(val);
  return val;
  }

int GetNumber()
{ 
   int num = 0;
   char key = keypad.getKey();
   while(key != '#')
   {
      switch (key)
      {
         case NO_KEY:
            break;

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
           // lcd.print(key);
            num = num * 10 + (key - '0');
            break;

         case '*':
            //num = 0;
            lcd.clear();
            break;
      }

      key = keypad.getKey();
   }

   return num;
}

 int motor_keypad()
    {
      pinMode(dirPin, OUTPUT);
      pinMode(stepPin, OUTPUT);
      pinMode(enPin, OUTPUT);
      digitalWrite(enPin, HIGH);
      digitalWrite(dirPin, HIGH);
      for(int x = 0; x < pdd; x++)
       {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(2000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(2000);
   }   
  // delay(1000);
}


 int motor_rotate_right()
   {
       digitalWrite(enPin, HIGH);
       digitalWrite(dirPin, HIGH);
       for(int x = 0; x < pdd; x++)
          {
             digitalWrite(stepPin,HIGH);
             delayMicroseconds(2000);
             digitalWrite(stepPin, LOW);
             delayMicroseconds(2000); 
          } 
         // delay(1000);
   }

int motor_rotate_left() 
     {
       digitalWrite(enPin, HIGH);
       digitalWrite(dirPin, LOW);
       for(int x = 0; x < 200; x++)
          {
             digitalWrite(stepPin,HIGH);
             delayMicroseconds(2000);
             digitalWrite(stepPin, LOW);
             delayMicroseconds(2000);
          }
          //delay(1000); 
      }

 int motor_sationary()
      {
        digitalWrite(enPin, LOW);
      }

Whatever I understood from your reply, I did this.

Is it correct ?

Hi,
@rksardckt2708 or @apard204 ???????????????????
I'm confused..
Whose threads is this???

Or are you the same person?
Or are we talking about two different projects.

To quote an Aussie politician. "Please explain?"

Tom.. :slight_smile:

I note that both accounts have the same IP address

TomGeorge:
Whose threads is this???

In another Thread (now locked) one of them said it is two friends working on one project.

...R

rksardckt2708:

#include <Keypad.h>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define dirPin 12
#define stepPin 9
#define enPin 13
int pdd;
bool firstMoveComplete = false;

void setup()
{   Serial.begin(9600);
   lcd.init();                    
   lcd.backlight();
}

void loop()
{
  int pdd = GetNumber();
  lcd.setCursor(0,0);
  lcd.print("D.Value:");
  lcd.print(pdd);
   
     int sval = getSensorvalue();
     lcd.setCursor(0,1);
     lcd.print("S.Value:");
     lcd.print(sval);
 
  if (firstMoveComplete == false) {  // NEW
      motor_keypad();
      firstMoveComplete = true;
  }                                                 // NEW
  else {                                         // NEW
   
     if (pdd>(sval+10))
     {
           motor_rotate_right();    // other existing code
      }
   else if (pdd<(sval-10))
      {
         motor_rotate_left();
      }
     else
    {
        motor_sationary();
     }
   }                                             // NEW - end of the new ELSE
 }

int getSensorvalue()
{
 int pval = analogRead(A0);
 int val = map (pval, 0, 1023, 0, 200);
 Serial.println(val);
 //lcd.setCursor(0,1);
 //lcd.print("Value");
 //lcd.print(val);
 return val;
 }

int GetNumber()
{
  int num = 0;
  char key = keypad.getKey();
  while(key != '#')
  {
     switch (key)
     {
        case NO_KEY:
           break;

case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
          // lcd.print(key);
           num = num * 10 + (key - '0');
           break;

case '*':
           //num = 0;
           lcd.clear();
           break;
     }

key = keypad.getKey();
  }

return num;
}

int motor_keypad()
   {
     pinMode(dirPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(enPin, OUTPUT);
     digitalWrite(enPin, HIGH);
     digitalWrite(dirPin, HIGH);
     for(int x = 0; x < pdd; x++)
      {
       digitalWrite(stepPin,HIGH);
       delayMicroseconds(2000);
       digitalWrite(stepPin, LOW);
       delayMicroseconds(2000);
  }  
 // delay(1000);
}

int motor_rotate_right()
  {
      digitalWrite(enPin, HIGH);
      digitalWrite(dirPin, HIGH);
      for(int x = 0; x < pdd; x++)
         {
            digitalWrite(stepPin,HIGH);
            delayMicroseconds(2000);
            digitalWrite(stepPin, LOW);
            delayMicroseconds(2000);
         }
        // delay(1000);
  }

int motor_rotate_left()
    {
      digitalWrite(enPin, HIGH);
      digitalWrite(dirPin, LOW);
      for(int x = 0; x < 200; x++)
         {
            digitalWrite(stepPin,HIGH);
            delayMicroseconds(2000);
            digitalWrite(stepPin, LOW);
            delayMicroseconds(2000);
         }
         //delay(1000);
     }

int motor_sationary()
     {
       digitalWrite(enPin, LOW);
     }




Whatever I understood from your reply, I did this.

Is it correct ?

Please tell me it is correct or not ?

rksardckt2708:
Please tell me it is correct or not ?

You have to tell us, in as much detail as possible, what happens when you try the latest version of your program.

The Arduino system is great for learning-by-doing

...R

Robin2:
You have to tell us, in as much detail as possible, what happens when you try the latest version of your program.

The Arduino system is great for learning-by-doing

...R

By this last code which I have sent you, motor got totally unresponsive i.e. it is not rotating now.

rksardckt2708:
By this last code which I have sent you, motor got totally unresponsive i.e. it is not rotating now.

I presume you mean the program in Reply #14

Why do you think the motor is not rotating? Thinking about that is an essential part of programming.

Your program shows stuff on the LCD and you have not told us what that is. The LCD should be showing the value of the variable pdd - what value does it have?

...R

Robin2:
I presume you mean the program in Reply #14

Why do you think the motor is not rotating? Thinking about that is an essential part of programming.

Your program shows stuff on the LCD and you have not told us what that is. The LCD should be showing the value of the variable pdd - what value does it have?

...R

We know that it is an important part of programming, but when we run the program in reply #14, it is not rotating.
We are using LCD only to print variable pdd and sval

Yes, both values are showing in LCD.

pdd variable shows value on entering by keypad and sval variable shows value of sensor.

rksardckt2708:
pdd variable shows value on entering by keypad and sval variable shows value of sensor.

What pdd value shows when you start the Arduino and have not entered any value using the keypad?

Do you think that might have any relevance for the fact that the motor is not rotating?

...R