IAN LANG ELECTRONICS

Using the matrix keypad we can accurately position the servo to 1 degree by typing in the desired degrees on the keypad. The circuit is set up as shown:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The red wire of the servo is at the 5V pin of the Arduino and the black at any of the GND pins. The yellow (or it may be white, depending on who made your servo) is the signal line and goes to pin 6 of your Arduino. The servo needs a considerable amount of current to drive it and if this is supplied by USB there may not be enough to drive both servo and Arduino. I therefore recommend using a 9V PSU or battery to run this circuit and unplugging it from your USB port after uploading. Before uploading, you may wish to disconnect the wire powering the servo too. Don't forget to reconnect it afterwards.

This is the code to drive it.

 

#include <Servo.h>

 Servo myservo;

boolean pressed=false;

int inputvalue=-1;

int countup=0;

int position=0;

int stopdigit=0;

char* inputbutton="";

void setup(){

for(int t=2;t<5;t++){

pinMode(t,OUTPUT);

digitalWrite(t,1);}

pinMode(6,OUTPUT);

myservo.attach(6);

myservo.write(0);  

}

 

void loop(){

 

if ( digitalRead(8)==0&&digitalRead(9)==0&&digitalRead(10)==0&&digitalRead(11)==0){

pressed=false;stopdigit=0;}

else{pressed=true;}

poller();

}

void poller (){

if (pressed==true&&stopdigit==0){

tone(5,440,150);

stopdigit=1;

 //**************TOP ROW**************

if (digitalRead(8)==1){

digitalWrite(2,0); //col 1

if (digitalRead(8)==0){

inputvalue=1;}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(8)==0){

inputvalue=2;}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

if (digitalRead(8)==0){

inputvalue=3;}

digitalWrite(4,1);}// col 3

 //**************SECOND ROW**************

if (digitalRead(9)==1){

digitalWrite(2,0); //col 1

if (digitalRead(9)==0){

inputvalue=4;}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(9)==0){

inputvalue=5;}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

if (digitalRead(9)==0){

inputvalue=6;}

digitalWrite(4,1);}// col 3

//**************THIRD ROW**************

if (digitalRead(10)==1){

digitalWrite(2,0); //col 1

if (digitalRead(10)==0){

inputvalue=7;}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(10)==0){

inputvalue=8;}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

if (digitalRead(10)==0){

inputvalue=9;}

digitalWrite(4,1);}// col 3

//**************BOTTOM ROW**************

if (digitalRead(11)==1){

 digitalWrite(2,0); //col 1

if (digitalRead(11)==0){

inputbutton= "*";}

 digitalWrite(2,1); //col 1  

digitalWrite(3,0);//col 2

if (digitalRead(11)==0){

inputvalue=0;}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

if (digitalRead(11)==0){

inputbutton= "#";}

digitalWrite(4,1);// col 3

digitalWrite(2,0);// col 1

if (digitalRead(11)==0){

inputbutton= "*";}

digitalWrite(2,1);}// col 1

if (inputbutton =="#"){

servopos();}

if (inputvalue>-1){

positionmultiply();}

}}

 

void positionmultiply(){

countup++;

switch (countup){

case 1:

position=inputvalue;break;

case 2:

position=(position*10)+inputvalue;break;

case 3:

position=(position*10)+inputvalue;break;

case 4:

position=(position*10)+inputvalue;break;

case 5:

position=inputvalue;countup=0;break;}

 }

 

void servopos(){

inputbutton="";countup=0;inputvalue=0;

    myservo.write(position);

 

 

 

}

 

Once you have uploaded the code and reconnected the servo the servo should return to zero degrees. Type in a position between 0 and 180 and press the # key, and the servo will move to that position and stay there. If you want the servo to move back to it's origin, press the # key on its own.

 

The discriminatror and multiplier are old news to us as we looked at them thoroughly in the chapter in which we examined the keypad. The servo is a new thing, however.

To make it work we include the servo library:

 

#include <Servo.h>

 

then give the servo an objectname:

 

 Servo myservo;

 

and tell the Arduino where the servo's signal wire is attached:

 

myservo.attach(6);

 

Once the # key has been pressed the function servopos is called:

 

void servopos(){

inputbutton="";countup=0;inputvalue=0;

    myservo.write(position);

 

all this does is to clear the variables and then write the position to the servo, causing it to move.Currently, if you type in a value greater than 180, the servo can become confused and act in unpredictable ways. So if you change the function like so:

 

 

void servopos(){

  inputbutton="";countup=0;inputvalue=0;

  if(position<181){

    myservo.write(position); }}

 

 

it discounts any input greater than 180.

 

For those of you not in the know, a servo is a positional rotator. In principle it is an electric motor and a gearbox. However, we can use PWM to turn and stop the turn at a desired angle rather than rotate continually. Servos produce a great deal of torque through their gearboxes, and thus are the basics of cybernetics. Congratulations- you've just produced the beginnings of a programmable robot!

 

Ian Lang, December 2011

 

 

 

 

A Keypad Controlled Servo

Go Back