Arduino Forum

Using Arduino => Programming Questions => Topic started by: Abdulrahman_alrawi on Jan 31, 2017, 07:50 pm

Title: controlling servo with keypad
Post by: Abdulrahman_alrawi on Jan 31, 2017, 07:50 pm
Hello, i want to connect 2 continuous rotational servos with 4*4 keypad, and the keypad determine the angle each servos goes to
​could you help me with the code please?​
 :smiley-sad:
Title: Re: controlling servo with keypad
Post by: J-M-L on Jan 31, 2017, 07:52 pm
Yes we could. show us where you are from your own research and what questions do you face
Title: Re: controlling servo with keypad
Post by: PaulS on Jan 31, 2017, 07:53 pm
Quote
could you help me with the code please?
Sure. You post the code that you have. You explain what it actually does, and how that differs from what you want, and we'll help you fix it. We won't write the code for you, though.
Title: Re: controlling servo with keypad
Post by: Abdulrahman_alrawi on Feb 01, 2017, 12:13 pm
I'm using this code, i want the servo go to the desired position and go back to the initial position after few seconds
the code;
#include <Keypad.h>
#include <Servo.h>

int pos = 0;    // variable to store the servo position

 
Servo myservo;  // create servo object to control a servo

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
String value = "";

void setup(){
  Serial.begin(9600);
  myservo.attach(10);  // attaches the servo on pin 9 to the servo object
}
 
void loop(){
  char customKey = customKeypad.getKey();       // get the key pressed
  if(customKey=='1'){                            // 'D' works as "enter"
     



       for(pos = 0; pos < 60; pos += 1 )           // goes from 0 degrees to 180 degrees
  {                                               // in steps of 1 degree
     myservo.write(0);                          // tell servo to go to position in variable 'pos'
    delay(15);                                  // waits 15ms for the servo to reach the position
  }
Title: Re: controlling servo with keypad
Post by: Abdulrahman_alrawi on Feb 01, 2017, 12:19 pm
I want the servo go to the desired position and go back to the initial position after few seconds (my project is to control to servos with keypad such as entering A and 1 will rotate the first servo by 60 degrees)
the code;
#include <Keypad.h>
#include <Servo.h>

int pos = 0;    // variable to store the servo position

 
Servo myservo;  // create servo object to control a servo

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
String value = "";

void setup(){
  Serial.begin(9600);
  myservo.attach(10);  // attaches the servo on pin 9 to the servo object
}
 
void loop(){
  char customKey = customKeypad.getKey();       // get the key pressed
  if(customKey=='1'){                            // 'D' works as "enter"
     



       for(pos = 0; pos < 60; pos += 1 )           // goes from 0 degrees to 180 degrees
  {                                               // in steps of 1 degree
     myservo.write(0);                          // tell servo to go to position in variable 'pos'
    delay(15);                                  // waits 15ms for the servo to reach the position
  }
Title: Re: controlling servo with keypad
Post by: PaulS on Feb 01, 2017, 12:28 pm
You need to re-read Reply #2. Read it several times, if necessary. We still do not know what the code ACTUALLY does, or how that differs from what you want.
Title: Re: controlling servo with keypad
Post by: UKHeliBob on Feb 01, 2017, 12:31 pm
Quote
i want to connect 2 continuous rotational servos with 4*4 keypad, and the keypad determine the angle each servos goes to
You can want it all you like but you cannot make continuous rotation "servos" go to a particular angle.  The value written to such servos determines the speed and direction not the position.

Why not use conventional servos.
Title: Re: controlling servo with keypad
Post by: TomGeorge on Feb 01, 2017, 12:38 pm
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html (http://forum.arduino.cc/index.php/topic,148850.0.html) then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)