Kristopher Bahar / Mbed 2 deprecated Shiftbright_Hall

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* ----- 4180 Final Design Project -----
00002     Bicycle Wheel LED Image Display
00003 */
00004 #include "mbed.h"
00005 
00006 InterruptIn hall(p28);                              // This interupt is used to poll the Hall Effect IC
00007 DigitalOut latch(p8);       
00008 DigitalOut enable(p9);
00009 Timer timer;                                        // This timer is used to time the wheel's rotation
00010 
00011 int LEDRowNumber = 10;                              // This is equal to the number of divisions the user has setup for his or her image
00012 int cycle_time = 0;                                 // Global Variable for the full rotation
00013 int rowIndex = 0;                                   // Index for shifting through the Image array
00014 bool top_wheel = false;                             // Variable Debug LED Circuit
00015 int color = 0;                                      // Variable for shifting through colors
00016 int j;                                              // For Loop index for LED debug circuit
00017 float adj_cycle_time = 0;                           // Variable used to hold "cycle_time / LEDRowNumber"
00018 
00019 //Cycles through different colors on RGB LED
00020 SPI spi(p5, p6, p7);
00021 
00022 //Use SPI hardware to write color values to LED driver chip
00023 void RGB_LED(int red, int green, int blue) {
00024     unsigned int low_color=0;
00025     unsigned int high_color=0;
00026     high_color=(blue<<4)|((red&0x3C0)>>6);
00027     low_color=(((red&0x3F)<<10)|(green));
00028     spi.write(high_color);
00029     spi.write(low_color);
00030     latch=1;
00031     latch=0;
00032 }
00033 
00034 //Interupt Routine used to detect when the magnet passes the Hall Effect Sensor
00035 void intRoutine() {
00036     cycle_time = timer.read_ms();                   //This is only called then the Hall Effect IC returns a "0"
00037     adj_cycle_time = cycle_time / LEDRowNumber;     //Calculates the time between sending each LED Row within the color Arrays
00038     timer.reset();                                  //Resets the Timer
00039     rowIndex = 0;                                   //Resets the Row to be sent so the image always stays in the relatively same place
00040     color=0;                                        //Resets the color Index for the LED sample wheel so the image always stays in the same place
00041 }
00042 
00043 int main() {
00044     hall.mode(PullUp);                              //Initializes the PullUp Resistor for the Hall Effect Sensor
00045     hall.fall(&intRoutine);                         //Initializes the Interrupt Routine for the Hall Effect Sensor (on falling edge)
00046     
00047     spi.format(16,0);                               //Initializes the Shiftbright LED Chain
00048     spi.frequency(500000);
00049     enable=0;
00050     latch=0;
00051     wait(2);
00052 
00053     timer.start();                                  //Starts the Timer
00054 
00055 /*//----- Color Initialization for Debug Program -----    
00056     int red=0;
00057     int green=0;
00058     int blue=0;
00059     */
00060     
00061 /* //----- Spiral Pattern -----
00062     int red[10][10]={50,0,0,0,0,0,0,0,0,0
00063                      ,0,50,0,0,0,0,0,0,0,0
00064                      ,0,0,50,0,0,0,0,0,0,0
00065                      ,0,0,0,50,0,0,0,0,0,0
00066                      ,0,0,0,0,50,0,0,0,0,0
00067                      ,0,0,0,0,0,50,0,0,0,0
00068                      ,0,0,0,0,0,0,50,0,0,0
00069                      ,0,0,0,0,0,0,0,50,0,0
00070                      ,0,0,0,0,0,0,0,0,50,0
00071                      ,0,0,0,0,0,0,0,0,0,50};
00072     int green[10][10]={50,0,0,0,0,0,0,0,0,0
00073                      ,0,50,0,0,0,0,0,0,0,0
00074                      ,0,0,50,0,0,0,0,0,0,0
00075                      ,0,0,0,50,0,0,0,0,0,0
00076                      ,0,0,0,0,50,0,0,0,0,0
00077                      ,0,0,0,0,0,50,0,0,0,0
00078                      ,0,0,0,0,0,0,50,0,0,0
00079                      ,0,0,0,0,0,0,0,50,0,0
00080                      ,0,0,0,0,0,0,0,0,50,0
00081                      ,0,0,0,0,0,0,0,0,0,50};
00082     int blue[10][10]={50,0,0,0,0,0,0,0,0,0
00083                      ,0,50,0,0,0,0,0,0,0,0
00084                      ,0,0,50,0,0,0,0,0,0,0
00085                      ,0,0,0,50,0,0,0,0,0,0
00086                      ,0,0,0,0,50,0,0,0,0,0
00087                      ,0,0,0,0,0,50,0,0,0,0
00088                      ,0,0,0,0,0,0,50,0,0,0
00089                      ,0,0,0,0,0,0,0,50,0,0
00090                      ,0,0,0,0,0,0,0,0,50,0
00091                      ,0,0,0,0,0,0,0,0,0,50};
00092 */
00093 
00094 //----- Pac Man -----
00095 
00096     int red[10][10]={50,50,50,50,50,50,50,50,50,50
00097                      ,50,50,50,50,50,50,50,50,50,50
00098                      ,50,50,50,50,50,50,50,50,50,50
00099                      ,50,50,50,50,50,50,50,50,50,50
00100                      ,50,50,50,50,50,50,0,0,50,50
00101                      ,50,50,50,50,50,50,50,50,50,50
00102                      ,0,0,0,0,0,0,0,0,0,0
00103                      ,0,0,0,0,0,0,0,0,0,0
00104                      ,50,50,50,50,50,50,50,50,50,50
00105                      ,50,50,50,50,50,50,50,50,50,50};
00106                      
00107     int green[10][10]={50,50,50,50,50,50,50,50,50,50
00108                      ,50,50,50,50,50,50,50,50,50,50
00109                      ,50,50,50,50,50,50,50,50,50,50
00110                      ,50,50,50,50,50,50,50,50,50,50
00111                      ,50,50,50,50,50,50,0,0,50,50
00112                      ,50,50,50,50,50,50,50,50,50,50
00113                      ,0,0,0,0,0,0,0,0,0,0
00114                      ,0,0,0,0,0,0,0,0,0,0
00115                      ,50,50,50,50,50,50,50,50,50,50
00116                      ,50,50,50,50,50,50,50,50,50,50};
00117                      
00118     int blue[10][10]={0,0,0,0,0,0,0,0,0,0
00119                      ,0,0,0,0,0,0,0,0,0,0
00120                      ,0,0,0,0,0,0,0,0,0,0
00121                      ,0,0,0,0,0,0,0,0,0,0
00122                      ,0,0,0,0,0,0,0,0,0,0
00123                      ,0,0,0,0,0,0,0,0,0,0
00124                      ,0,0,0,0,0,0,0,0,0,0
00125                      ,0,0,0,0,0,0,0,0,0,0
00126                      ,0,0,0,0,0,0,0,0,0,0
00127                      ,0,0,0,0,0,0,0,0,0,0};
00128                      
00129                      
00130 /*// -----Blue Triangle: From Bitmap Image Program -----
00131     int red[36][10]={0,0,0,0,0,0,0,0,0,0,
00132                     0,0,0,0,0,42,42,42,42,42,
00133                     0,0,0,0,0,42,42,42,42,42,
00134                     0,0,0,0,0,0,0,42,42,42,
00135                     0,0,0,0,0,0,0,0,0,42,
00136                     0,0,0,0,0,0,0,0,0,0,
00137                     0,0,0,0,0,0,0,0,0,42,
00138                     0,0,0,0,0,0,0,0,0,42,
00139                     0,0,0,0,0,0,0,0,42,42,
00140                     0,0,0,0,0,0,0,0,42,42,
00141                     0,0,0,0,0,0,0,0,42,42,
00142                     0,0,0,0,0,0,0,0,0,42,
00143                     0,0,0,0,0,0,0,0,0,42,
00144                     0,0,0,0,0,0,0,0,0,0,
00145                     0,0,0,0,0,0,0,0,0,42,
00146                     0,0,0,0,0,0,0,42,42,42,
00147                     0,0,0,0,0,42,42,42,42,42,
00148                     0,0,0,0,0,42,42,42,42,42,
00149                     0,0,0,0,0,0,0,0,0,0,
00150                     0,0,0,0,0,0,0,0,0,0,
00151                     0,0,0,0,0,0,0,0,0,0,
00152                     0,0,0,0,0,0,0,0,0,0,
00153                     0,0,0,0,0,0,0,0,0,0,
00154                     0,0,0,0,0,0,0,0,0,0,
00155                     0,0,0,0,0,0,0,0,0,0,
00156                     0,0,0,0,0,0,0,0,0,0,
00157                     0,0,0,0,0,0,0,0,0,0,
00158                     0,0,0,0,0,0,0,0,0,0,
00159                     0,0,0,0,0,0,0,0,0,0,
00160                     0,0,0,0,0,0,0,0,0,0,
00161                     0,0,0,0,0,0,0,0,0,0,
00162                     0,0,0,0,0,0,0,0,0,0,
00163                     0,0,0,0,0,0,0,0,0,0,
00164                     0,0,0,0,0,0,0,0,0,0,
00165                     0,0,0,0,0,0,0,0,0,0,
00166                     0,0,0,0,0,0,0,0,0,0}; 
00167     
00168 int green[36][10]={0,0,0,0,42,42,42,42,42,42,
00169                     0,0,0,0,0,0,0,0,0,0,
00170                     0,0,0,0,0,0,0,0,0,0,
00171                     0,0,0,0,0,0,0,0,0,0,
00172                     0,0,0,0,0,0,0,0,0,0,
00173                     0,0,0,0,0,0,0,0,0,0,
00174                     0,0,0,0,0,0,0,0,0,0,
00175                     0,0,0,0,0,0,0,0,0,0,
00176                     0,0,0,0,0,0,0,0,0,0,
00177                     0,0,0,0,0,0,0,0,0,0,
00178                     0,0,0,0,0,0,0,0,0,0,
00179                     0,0,0,0,0,0,0,0,0,0,
00180                     0,0,0,0,0,0,0,0,0,0,
00181                     0,0,0,0,0,0,0,0,0,0,
00182                     0,0,0,0,0,0,0,0,0,0,
00183                     0,0,0,0,0,0,0,0,0,0,
00184                     0,0,0,0,0,0,0,0,0,0,
00185                     0,0,0,0,0,0,0,0,0,0,
00186                     0,0,0,0,42,42,42,42,42,42,
00187                     0,0,0,0,42,42,42,42,42,42,
00188                     0,0,0,0,42,42,42,42,42,42,
00189                     0,0,0,0,42,42,42,42,42,42,
00190                     0,0,0,0,42,42,42,42,42,42,
00191                     0,0,0,0,42,42,42,42,42,42,
00192                     0,0,0,0,42,42,42,42,42,42,
00193                     0,0,0,0,0,42,42,42,42,42,
00194                     0,0,0,0,0,0,0,42,42,42,
00195                     0,0,0,0,0,0,0,0,42,42,
00196                     0,0,0,0,0,0,0,42,42,42,
00197                     0,0,0,0,0,42,42,42,42,42,
00198                     0,0,0,0,0,42,42,42,42,42,
00199                     0,0,0,0,42,42,42,42,42,42,
00200                     0,0,0,0,42,42,42,42,42,42,
00201                     0,0,0,0,42,42,42,42,42,42,
00202                     0,0,0,0,42,42,42,42,42,42,
00203                     0,0,0,0,42,42,42,42,42,42};
00204     
00205     int blue[36][10]={42,42,42,42,0,0,0,0,0,0,
00206                     42,42,42,42,42,0,0,0,0,0,
00207                     42,42,42,42,42,0,0,0,0,0,
00208                     42,42,42,42,42,42,42,0,0,0,
00209                     42,42,42,42,42,42,42,42,42,0,
00210                     42,42,42,42,42,42,42,42,42,42,
00211                     42,42,42,42,42,42,42,42,42,0,
00212                     42,42,42,42,42,42,42,42,42,0,
00213                     42,42,42,42,42,42,42,42,0,0,
00214                     42,42,42,42,42,42,42,42,0,0,
00215                     42,42,42,42,42,42,42,42,0,0,
00216                     42,42,42,42,42,42,42,42,42,0,
00217                     42,42,42,42,42,42,42,42,42,0,
00218                     42,42,42,42,42,42,42,42,42,42,
00219                     42,42,42,42,42,42,42,42,42,0,
00220                     42,42,42,42,42,42,42,0,0,0,
00221                     42,42,42,42,42,0,0,0,0,0,
00222                     42,42,42,42,42,0,0,0,0,0,
00223                     42,42,42,42,0,0,0,0,0,0,
00224                     42,42,42,42,0,0,0,0,0,0,
00225                     42,42,42,42,0,0,0,0,0,0,
00226                     42,42,42,42,0,0,0,0,0,0,
00227                     42,42,42,42,0,0,0,0,0,0,
00228                     42,42,42,42,0,0,0,0,0,0,
00229                     42,42,42,42,0,0,0,0,0,0,
00230                     42,42,42,42,42,0,0,0,0,0,
00231                     42,42,42,42,42,42,42,0,0,0,
00232                     42,42,42,42,42,42,42,42,0,0,
00233                     42,42,42,42,42,42,42,0,0,0,
00234                     42,42,42,42,42,0,0,0,0,0,
00235                     42,42,42,42,42,0,0,0,0,0,
00236                     42,42,42,42,0,0,0,0,0,0,
00237                     42,42,42,42,0,0,0,0,0,0,
00238                     42,42,42,42,0,0,0,0,0,0,
00239                     42,42,42,42,0,0,0,0,0,0,
00240                     42,42,42,42,0,0,0,0,0,0};
00241 */
00242     while(1) {
00243         //led1 = hall;      // Used for Debugging the Hall Effect IC
00244         //led2 = !hall;     // Used for Debugging the Hall Effect IC
00245 
00246         //----- Code for Cycling through a 2D Picture Array -----
00247         wait_ms(adj_cycle_time);
00248         for(int i=9;i>=0;i--){  //If we update without any waiting, the LEDs change colors super fast, and you won't notice the shifting LEDs
00249             RGB_LED( red[rowIndex][i], green[rowIndex][i], blue[rowIndex][i]);
00250         }
00251         rowIndex++;
00252         if(rowIndex>=LEDRowNumber){
00253             rowIndex = 0;
00254         }
00255         
00256         
00257        /* //----- Debug Code: Rainbow Pattern ------ (Pre 2D Color Array)
00258        wait_ms(adj_cycle_time);
00259        top_wheel = true;
00260        if (top_wheel && (color == 0)) {
00261             // Red, Color 0
00262             //printf("RED RED RED\r\n");
00263             red = 50;
00264             blue = 0;
00265             green = 0;
00266             for(j = 0; j < 10; j++)
00267                 RGB_LED(red, green, blue);
00268             top_wheel = false;
00269             color++;
00270         }
00271         if (top_wheel && (color == 1)) {
00272             //printf("ORANGE ORANGE ORANGE\r\n");
00273             // Orange, Color 1
00274             red = 50;
00275             blue = 0;
00276             green = 25;
00277             for(j = 0; j < 10; j++)
00278                 RGB_LED(red, green, blue);
00279             top_wheel = false;
00280             color++;
00281         }
00282         if (top_wheel && (color == 2)) {
00283             //printf("YELLOW YELLOW YELLOW\r\n");
00284             // Yellow, Color 2
00285             red = 50;
00286             blue = 0;
00287             green = 50;
00288             for(j = 0; j < 10; j++)
00289                 RGB_LED(red, green, blue);
00290             top_wheel = false;
00291             color++;
00292         }
00293        if (top_wheel && (color == 3)) {
00294             //printf("GREEN GREEN GREEN\r\n");
00295             // Green, Color 3
00296             red = 0;
00297             blue = 0;
00298             green = 50;
00299             for(j = 0; j < 10; j++)
00300                 RGB_LED(red, green, blue);
00301             top_wheel = false;
00302             color++;
00303         }
00304         if (top_wheel && (color == 4)) {
00305             //printf("CYAN CYAN CYAN\r\n");
00306             // Cyan, Color 4
00307             red = 0;
00308             blue = 50;
00309             green = 50;
00310             for(j = 0; j < 10; j++)
00311                 RGB_LED(red, green, blue);
00312             top_wheel = false;
00313             color++;
00314         }
00315         if (top_wheel && (color == 5)) {   
00316             //printf("BLUE BLUE BLUE\r\n"); 
00317             // Blue, Color 5
00318             red = 0;
00319             blue = 50;
00320             green = 0;
00321             for(j = 0; j < 10; j++)
00322                 RGB_LED(red, green, blue);
00323             top_wheel = false;
00324             color++;
00325         }
00326         if (top_wheel && (color == 6)) {
00327             //printf("PURPLE PURPLE PURPLE\r\n");
00328             //Purple, Color 6
00329             red = 50;
00330             blue = 50;
00331             green = 0;
00332             for(j = 0; j < 10; j++)
00333                 RGB_LED(red, green, blue);
00334             top_wheel = false;
00335             color = 0;
00336         }
00337         */
00338     }
00339 }