1 You have an Arduino UNO R3 a Shiftbrite RGB LED a 4 x 4 Ma
1. You have an Arduino UNO R3, a Shiftbrite RGB LED, a 4 x 4 Matrix keypad, and a SparkFun MMA8452Q Accelerometer. Write the code to make to work together and show the connection diagram.
Solution
#include<p18f4520.h>
 #include<delays.h>
 #pragma config OSC=HS,WDT=OFF,FCMEN=ON,XINST=OFF,IESO=OFF,LVP=OFF
 
 #define HIGH 1
 #define LOW 0
 #define R1 PORTBbits.RB0
 #define R2 PORTBbits.RB1
 #define R3 PORTBbits.RB2
 #define R4 PORTBbits.RB3
 #define C1 PORTBbits.RB4
 #define C2 PORTBbits.RB5
 #define C3 PORTBbits.RB6
 #define C4 PORTBbits.RB7
 #define SEG_EN1 LATCbits.LATC1
 #define SEG_EN2 LATCbits.LATC0
 
 /*-----Function Declaration--------*/
 void seg_wrt(void);
 void update(unsigned char);
 
 unsigned char val = 0;
 
 void main(void)
 {
     ADCON1 = 0x0F;
     TRISD = 0x00;
     TRISC = 0x00;
     TRISB = 0xf0;
     LATC = 0x00;
     LATD = 0x00;
     INTCON2bits.RBPU = LOW;    // Enable Weak internal pull-ups in pin RB4 and RB5
     while(1)
     {
         LATB = 0xf0;
         if(C1 == LOW){
             R1 = HIGH;
             if(C1 == HIGH)
                 update(16);
             else {
                 R2 = HIGH;
                 if(C1 == HIGH)
                     update(15);
                 else {
                     R3 = HIGH;
                     if(C1 == HIGH)
                         update(14);
                     else update(13);
         }
             }
     }
     if(C2 == LOW){
             R1 = HIGH;
             if(C2 == HIGH)
                 update(12);
             else {
                 R2 = HIGH;
                 if(C2 == HIGH)
                     update(11);
                 else{
                     R3 = HIGH;
                     if(C2 == HIGH)
                         update(10);
                     else update(9);
                 }
             }
     }
     if(C3 == LOW){
             R1 = HIGH;
             if(C3 == HIGH)
                 update(8);
             else {
                 R2 = HIGH;
                 if(C3 == HIGH)
                     update(7);
                   else{
                     R3 = HIGH;
                     if(C3 == HIGH)
                         update(6);
                     else update(5);
                 }
             }
     }
     if(C4 == LOW){
             R1 = HIGH;
             if(C4 == HIGH)
                 update(4);
             else {
                 R2 = HIGH;
                 if(C4 == HIGH)
                     update(3);
                     else{
                         R3 = HIGH;
                         if(C4 == HIGH)
                             update(2);
                         else update(1);
                 }
             }
     }
         seg_wrt();
     }   // end of while(1)
 }
 
 void update(unsigned char data)
 {
     val = data;
 }
 
 void seg_wrt() {
     unsigned char lookup[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
     long unit,ten;
     long idx = 0;
     unit = val/10;
     ten = val%10;
     for(idx=0;idx<100; idx++)
     {
         LATD = lookup[unit];
         SEG_EN1 = HIGH;
         Delay10TCYx(10);
         SEG_EN1 = LOW;
         LATD = lookup[ten];
         SEG_EN2 = HIGH;
         Delay10TCYx(10);
         SEG_EN2 = LOW;
     }
 }



