Alex Louden / Mbed 2 deprecated KeypadServo

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 
00004 DigitalOut myled1(LED1);
00005 DigitalOut myled2(LED2);
00006 DigitalOut myled3(LED3);
00007 DigitalOut myled4(LED4);
00008 
00009 char code[] = "1234";
00010 
00011 Serial pc(USBTX, USBRX);      // debugging
00012 
00013 //--------------- IC2 trial ---------------
00014 
00015 const int address = 0x30;
00016 
00017 // write value into register regno, return success
00018 bool write_reg(int regno, int value)
00019 {
00020   char data[2] = {regno, value}; 
00021   return i2c.write(address, data, 2) == 0;
00022 }
00023 
00024 // read value from register regno, return success
00025 bool read_reg(int regno, int *value)
00026 {
00027   char data = regno; 
00028   if (i2c.write(address, &data, 1) == 0 && i2c.read(address, &data, 1) == 0)
00029   {
00030     *value = data;
00031     return true;
00032   }
00033   return false;
00034 }
00035 
00036 // read complete value of X axis, return it or -1 on failure
00037 int read_x()
00038 {
00039   int low, high;
00040   if ( read_reg(XOUT_H, &high) && read_reg(XOUT_L, &low) )
00041     return high<<8 | low;
00042   else
00043     return -1;
00044 }
00045 
00046 void accData(){
00047 
00048     DigitalOut enable(p26);    //enable pin
00049     I2C i2c(p28, p27);         // accelerometer
00050     
00051     enable = 1; //set enable pin to 1
00052     i2c.frequency(400000); //set frequency to 400 KHz
00053     
00054     const int CTRL_REGB = 0x0D;
00055     const int CTRL_REGC = 0x0C;
00056     
00057     write_reg(CTRL_REGB, 0xC2);
00058     write_reg(CTRL_REGC, 0x00);
00059     printf("X axis: %d\n", read_x());
00060     
00061 
00062     
00063     /*
00064     const int address = 0x97; //slave address
00065     
00066     char data[12];
00067     
00068     data[0] = 0x42;
00069     i2c.write(CTRL_REGB, data, 1); // register CTRL_REGB to 0x42 (start condition)
00070     
00071     wait(0.1);
00072     
00073     data[0] = 0x00;
00074     i2c.write(CTRL_REGC, data, 1); // register CTRL_REGC to 0x00 (start condition)
00075     
00076     wait(0.1);
00077     
00078     data[0] = 0x00;
00079     i2c.write(address, data, 1); // tell accelerometer i want to talk to it?
00080 
00081     wait(0.1);
00082     
00083     const int XOUT_H = 0x00; //x high register
00084     const int XOUT_L = 0x01; //x low register
00085 
00086     for(int i = 0; i < 10; i++){
00087         pc.printf("x high: '%s'\n", i2c.read(XOUT_H, data, 12));
00088         wait(0.1);
00089         pc.printf("x low: '%s'\n", i2c.read(XOUT_L, data, 12));
00090         wait(0.1);
00091     }
00092 
00093     enable = 0;
00094 
00095     
00096     pc.printf("started\n");
00097     
00098     
00099     for (int i = 0; i < 10; i++){ //get 10 results
00100     
00101         const int CTRL_REGB = 0x0D;
00102         const int CTRL_REGC = 0x0C;
00103 
00104         cmd[0] = 0x2;
00105         i2c.write(addr, data, 2);
00106         
00107         i2c.read(addr, data, 8); // read the echo result
00108 
00109         // print the ranging data to the screen
00110         //float echo = 0.01 * ((cmd[0] << 8) + cmd[1]);
00111         
00112         pc.printf ("Data: %s\n", cmd);
00113 
00114         wait(0.1);
00115     }
00116     
00117     i2c.write(
00118     
00119     wait(0.07);
00120     
00121     const int XOUT_H = 0x00;
00122     const int XOUT_L = 0x01;
00123     
00124     i2c.read(XOUT_H, data, 8); // read the result
00125     pc.printf ("XOUT_H: '%s'\n", data);
00126     
00127     wait(0.07);
00128     
00129     i2c.read(XOUT_L, data, 8); // read the result
00130     pc.printf ("XOUT_L: '%s'\n", data);    
00131     */
00132 
00133 }
00134 
00135 
00136 //--------------- Servo position reset ---------------
00137 
00138 void resetServo(){
00139 
00140 Servo myServo (p21);          // steering servo
00141 
00142   int i;
00143   bool j = 0;
00144 
00145   //clockwise 180
00146   for (i=0 ; i<200 ; i++){
00147     myServo = i/100.0;
00148     wait (0.01);
00149     if(i%10==0){
00150         j = !j;
00151         myled1 = j;
00152     }
00153   }
00154   
00155   pc.printf("Servo at full rotation\n");
00156   
00157   //wait a second
00158   for (i = 0; i < 10; i++){
00159      wait (0.1);
00160      j = !j;
00161      myled2 = j;
00162   }
00163   
00164   pc.printf("End pause\n");
00165   
00166   //back to start
00167   for (i=100 ; i>0 ; i--){
00168     myServo = i/100.0;
00169     wait (0.01);
00170     if(i%10==0){
00171         j = !j;
00172         myled3 = j;
00173     }
00174   }
00175   pc.printf("Servo at default position\n");
00176 }
00177 
00178 //--------------- Turning all LEDs off or on ---------------
00179 
00180 void all(bool status){
00181 
00182         myled1 = status;
00183         myled2 = status;
00184         myled3 = status;
00185         myled4 = status;
00186         
00187 }
00188 
00189 
00190 //--------------- Digital pin checking ---------------
00191 
00192 void checkPin5(){
00193 
00194 DigitalIn input5(p5);
00195 
00196 //check for pin 5
00197   while(1) {
00198       if(input5){
00199           myled4 = !myled4;
00200           pc.printf("pin 5 in!\n");
00201       }
00202       wait(0.1);
00203   }
00204 }
00205 
00206 //--------------- Get key number ---------------
00207 
00208 int keyNum(){
00209 
00210 DigitalIn col1(p18);
00211 DigitalIn col2(p20);
00212 DigitalIn col3(p16);
00213 
00214 DigitalOut row1(p19);
00215 DigitalOut row2(p14);
00216 DigitalOut row3(p15);
00217 DigitalOut row4(p17);
00218 
00219     row1 = 1;
00220     row2 = 0;
00221     row3 = 0;
00222     row4 = 0;
00223     
00224     if (col1)
00225         return 1;
00226     else if (col2)
00227         return 2;
00228     else if (col3)
00229         return 3; 
00230         
00231     row1 = 0;
00232     row2 = 1;
00233     row3 = 0;
00234     row4 = 0;
00235     
00236     if (col1)
00237         return 4;
00238     else if (col2)
00239         return 5;
00240     else if (col3)
00241         return 6;     
00242     
00243     row1 = 0;
00244     row2 = 0;
00245     row3 = 1;
00246     row4 = 0;
00247     
00248     if (col1)
00249         return 7;
00250     else if (col2)
00251         return 8;
00252     else if (col3)
00253         return 9;  
00254         
00255     row1 = 0;
00256     row2 = 0;
00257     row3 = 0;
00258     row4 = 1;
00259     
00260     if (col1)
00261         return 10;
00262     else if (col2)
00263         return 0;
00264     else if (col3)
00265         return 11;  
00266                    
00267     row1 = 0;
00268     row2 = 0;
00269     row3 = 0;
00270     row4 = 0;
00271     
00272     return -1;
00273 }
00274 
00275 //--------------- Check against code ---------------
00276 
00277 bool getCode(){
00278 
00279     char codeTry[] = "";
00280     int previous = -1;
00281     int a = -1;
00282     
00283     while(strlen(codeTry) < strlen(code)){
00284     
00285         int cycles = 0;
00286     
00287         while ((a == previous && cycles < 40) || a == -1){ //keep looping until different to last, or held down
00288             a = keyNum();
00289             cycles++;
00290             wait(0.01);
00291         }
00292        
00293         sprintf(codeTry, "%s%d", codeTry, a);
00294         
00295         switch(strlen(codeTry)){
00296             case 1: myled1 = 1; break;
00297             case 2: myled2 = 1; break;
00298             case 3: myled3 = 1; break;
00299             case 4: myled4 = 1;
00300         }
00301         
00302         previous = a;
00303         wait (0.1);
00304     }
00305     
00306     all(0);
00307     
00308     pc.printf("code entered: '%s'\n", codeTry);
00309     
00310     if(strcmp(code, codeTry) == 0){
00311         return true;
00312     }
00313     
00314     return false;
00315 }
00316 
00317 //--------------- Check what key is pressed ---------------
00318 
00319 void keyCheck(){
00320 
00321     bool j = 0;
00322     
00323     while (1){
00324         int a = keyNum();
00325         if(a == 11){     //on hash close the app
00326             myled1 = 0;
00327             break;
00328         }
00329         if(a!=-1){
00330             pc.printf("%d\n", a); //send key to pc
00331         }
00332         
00333         j = !j;
00334         myled1 = j;        //flash LED1
00335         
00336         wait (0.1);
00337     }
00338 }
00339 
00340 //---------------- Door keypad ---------------
00341 
00342 void doorlock(){
00343 
00344     bool password = getCode();
00345 
00346     if (password){
00347         pc.printf("Password correct\n");
00348         resetServo();
00349     } else {
00350         all(1);
00351         wait(0.5);
00352         all(0);
00353         pc.printf("Password incorrect\n");
00354     }
00355 }
00356 
00357 //-------------------- Main -------------------
00358 
00359 int main() {
00360     
00361     pc.printf("Program started\n");
00362     
00363     accData();
00364     
00365     //doorlock();
00366     //resetServo();
00367     
00368     pc.printf("Program complete\n\n");
00369 }