Bouncing Betty Halloween Prop. A prop skull mounted with an mbed microcontroller armed to scare anyone who comes close to it. This project uses an infrared sonar, speaker, and, shiftbrite LEDs.
Dependencies: mbed wave_player
main.cpp@0:92661f51fe68, 2011-10-27 (annotated)
- Committer:
- mafischl
- Date:
- Thu Oct 27 07:19:10 2011 +0000
- Revision:
- 0:92661f51fe68
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mafischl | 0:92661f51fe68 | 1 | // Bouncing Betty Halloween Effect |
mafischl | 0:92661f51fe68 | 2 | // By: Michael Fischler |
mafischl | 0:92661f51fe68 | 3 | // Residence Hall Association 2011-2012 |
mafischl | 0:92661f51fe68 | 4 | |
mafischl | 0:92661f51fe68 | 5 | #include "mbed.h" // Built for Mbed NXP LPC1768 Microprocessor |
mafischl | 0:92661f51fe68 | 6 | #include "PowerControl/PowerControl.h" // for power mgmt |
mafischl | 0:92661f51fe68 | 7 | #include "PowerControl/EthernetPowerControl.h" // for power mgmt |
mafischl | 0:92661f51fe68 | 8 | #include "wave_player.h" // for speakers |
mafischl | 0:92661f51fe68 | 9 | #define USR_POWERDOWN (0x104) |
mafischl | 0:92661f51fe68 | 10 | int semihost_powerdown() { |
mafischl | 0:92661f51fe68 | 11 | uint32_t arg; |
mafischl | 0:92661f51fe68 | 12 | return __semihost(USR_POWERDOWN, &arg); |
mafischl | 0:92661f51fe68 | 13 | } |
mafischl | 0:92661f51fe68 | 14 | |
mafischl | 0:92661f51fe68 | 15 | DigitalOut latch1(p15); // Latch on RGB LED 1 |
mafischl | 0:92661f51fe68 | 16 | DigitalOut enable1(p16); // Enable on RGB LED 1 |
mafischl | 0:92661f51fe68 | 17 | SPI spi1(p11, p12, p13); // SPI for RGB LED 1 |
mafischl | 0:92661f51fe68 | 18 | |
mafischl | 0:92661f51fe68 | 19 | DigitalOut latch2(p8); // Latch on RGB LED 2 |
mafischl | 0:92661f51fe68 | 20 | DigitalOut enable2(p9); // Enable on RGB LED 2 |
mafischl | 0:92661f51fe68 | 21 | SPI spi2(p5, p6, p7); // SPI for RGB LED 2 |
mafischl | 0:92661f51fe68 | 22 | |
mafischl | 0:92661f51fe68 | 23 | AnalogOut DACout(p18); |
mafischl | 0:92661f51fe68 | 24 | wave_player waver(&DACout); |
mafischl | 0:92661f51fe68 | 25 | |
mafischl | 0:92661f51fe68 | 26 | AnalogIn sonar(p20); // Sonar Input |
mafischl | 0:92661f51fe68 | 27 | DigitalIn pushbutton(p24); // Pushbutton Input |
mafischl | 0:92661f51fe68 | 28 | |
mafischl | 0:92661f51fe68 | 29 | DigitalOut wdtLED(LED4);//WatchDog Indicator |
mafischl | 0:92661f51fe68 | 30 | |
mafischl | 0:92661f51fe68 | 31 | LocalFileSystem local("local"); |
mafischl | 0:92661f51fe68 | 32 | |
mafischl | 0:92661f51fe68 | 33 | float distance; // distance for sonar analog input |
mafischl | 0:92661f51fe68 | 34 | float j = 0; // Alternate Variable for Blue/Green |
mafischl | 0:92661f51fe68 | 35 | bool j_increase = 1; // Direction of Blue/Green Alternating |
mafischl | 0:92661f51fe68 | 36 | |
mafischl | 0:92661f51fe68 | 37 | class Watchdog { |
mafischl | 0:92661f51fe68 | 38 | public: |
mafischl | 0:92661f51fe68 | 39 | // Load timeout value in watchdog timer and enable |
mafischl | 0:92661f51fe68 | 40 | void kick(float s) { |
mafischl | 0:92661f51fe68 | 41 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
mafischl | 0:92661f51fe68 | 42 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
mafischl | 0:92661f51fe68 | 43 | LPC_WDT->WDTC = s * (float)clk; |
mafischl | 0:92661f51fe68 | 44 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
mafischl | 0:92661f51fe68 | 45 | kick(); |
mafischl | 0:92661f51fe68 | 46 | } |
mafischl | 0:92661f51fe68 | 47 | // "kick" or "feed" the dog - reset the watchdog timer |
mafischl | 0:92661f51fe68 | 48 | // by writing this required bit pattern |
mafischl | 0:92661f51fe68 | 49 | void kick() { |
mafischl | 0:92661f51fe68 | 50 | LPC_WDT->WDFEED = 0xAA; |
mafischl | 0:92661f51fe68 | 51 | LPC_WDT->WDFEED = 0x55; |
mafischl | 0:92661f51fe68 | 52 | } |
mafischl | 0:92661f51fe68 | 53 | }; |
mafischl | 0:92661f51fe68 | 54 | |
mafischl | 0:92661f51fe68 | 55 | Watchdog wdt; |
mafischl | 0:92661f51fe68 | 56 | |
mafischl | 0:92661f51fe68 | 57 | void RGB_LED(int red1, int green1, int blue1, int red2, int green2, int blue2) { //Use SPI hardware to write color values to LED driver chip |
mafischl | 0:92661f51fe68 | 58 | unsigned int low_color1=0; |
mafischl | 0:92661f51fe68 | 59 | unsigned int high_color1=0; |
mafischl | 0:92661f51fe68 | 60 | unsigned int low_color2=0; |
mafischl | 0:92661f51fe68 | 61 | unsigned int high_color2=0; |
mafischl | 0:92661f51fe68 | 62 | high_color1=(blue1<<4)|((red1&0x3C0)>>6); |
mafischl | 0:92661f51fe68 | 63 | low_color1=(((red1&0x3F)<<10)|(green1)); |
mafischl | 0:92661f51fe68 | 64 | high_color2=(blue2<<4)|((red2&0x3C0)>>6); |
mafischl | 0:92661f51fe68 | 65 | low_color2=(((red2&0x3F)<<10)|(green2)); |
mafischl | 0:92661f51fe68 | 66 | spi1.write(high_color1); |
mafischl | 0:92661f51fe68 | 67 | spi1.write(low_color1); |
mafischl | 0:92661f51fe68 | 68 | spi2.write(high_color2); |
mafischl | 0:92661f51fe68 | 69 | spi2.write(low_color2); |
mafischl | 0:92661f51fe68 | 70 | latch1=1; |
mafischl | 0:92661f51fe68 | 71 | latch2=1; |
mafischl | 0:92661f51fe68 | 72 | wait(0.1); |
mafischl | 0:92661f51fe68 | 73 | latch1=0; |
mafischl | 0:92661f51fe68 | 74 | latch2=0; |
mafischl | 0:92661f51fe68 | 75 | } |
mafischl | 0:92661f51fe68 | 76 | |
mafischl | 0:92661f51fe68 | 77 | void RGB_LED1(int red1, int green1, int blue1) { //Use SPI hardware to write color values to LED driver chip |
mafischl | 0:92661f51fe68 | 78 | unsigned int low_color1=0; |
mafischl | 0:92661f51fe68 | 79 | unsigned int high_color1=0; |
mafischl | 0:92661f51fe68 | 80 | high_color1=(blue1<<4)|((red1&0x3C0)>>6); |
mafischl | 0:92661f51fe68 | 81 | low_color1=(((red1&0x3F)<<10)|(green1)); |
mafischl | 0:92661f51fe68 | 82 | spi1.write(high_color1); |
mafischl | 0:92661f51fe68 | 83 | spi1.write(low_color1); |
mafischl | 0:92661f51fe68 | 84 | latch1=1; |
mafischl | 0:92661f51fe68 | 85 | wait(0.1); |
mafischl | 0:92661f51fe68 | 86 | latch1=0; |
mafischl | 0:92661f51fe68 | 87 | } |
mafischl | 0:92661f51fe68 | 88 | |
mafischl | 0:92661f51fe68 | 89 | void RGB_LED2(int red2, int green2, int blue2) { //Use SPI hardware to write color values to LED driver chip |
mafischl | 0:92661f51fe68 | 90 | unsigned int low_color2=0; |
mafischl | 0:92661f51fe68 | 91 | unsigned int high_color2=0; |
mafischl | 0:92661f51fe68 | 92 | high_color2=(blue2<<4)|((red2&0x3C0)>>6); |
mafischl | 0:92661f51fe68 | 93 | low_color2=(((red2&0x3F)<<10)|(green2)); |
mafischl | 0:92661f51fe68 | 94 | spi2.write(high_color2); |
mafischl | 0:92661f51fe68 | 95 | spi2.write(low_color2); |
mafischl | 0:92661f51fe68 | 96 | latch2=1; |
mafischl | 0:92661f51fe68 | 97 | wait(0.1); |
mafischl | 0:92661f51fe68 | 98 | latch2=0; |
mafischl | 0:92661f51fe68 | 99 | } |
mafischl | 0:92661f51fe68 | 100 | |
mafischl | 0:92661f51fe68 | 101 | int main() { |
mafischl | 0:92661f51fe68 | 102 | |
mafischl | 0:92661f51fe68 | 103 | int result = 0; |
mafischl | 0:92661f51fe68 | 104 | if ((LPC_WDT->WDMOD >> 2) & 1) |
mafischl | 0:92661f51fe68 | 105 | wdtLED = 1; |
mafischl | 0:92661f51fe68 | 106 | else wdtLED = 0; |
mafischl | 0:92661f51fe68 | 107 | |
mafischl | 0:92661f51fe68 | 108 | // 30 second timeout on watchdog timer hardware |
mafischl | 0:92661f51fe68 | 109 | wdt.kick(10.0); |
mafischl | 0:92661f51fe68 | 110 | |
mafischl | 0:92661f51fe68 | 111 | //Power Management |
mafischl | 0:92661f51fe68 | 112 | PHY_PowerDown(); // Power Down Ethernet |
mafischl | 0:92661f51fe68 | 113 | result = semihost_powerdown(); // Power Down USB Interface Chip (blue status LED) |
mafischl | 0:92661f51fe68 | 114 | |
mafischl | 0:92661f51fe68 | 115 | |
mafischl | 0:92661f51fe68 | 116 | |
mafischl | 0:92661f51fe68 | 117 | pushbutton.mode(PullUp); |
mafischl | 0:92661f51fe68 | 118 | spi1.format(16,0); |
mafischl | 0:92661f51fe68 | 119 | spi1.frequency(500000); |
mafischl | 0:92661f51fe68 | 120 | spi2.format(16,0); |
mafischl | 0:92661f51fe68 | 121 | spi2.frequency(500000); |
mafischl | 0:92661f51fe68 | 122 | enable1=0; |
mafischl | 0:92661f51fe68 | 123 | latch1=0; |
mafischl | 0:92661f51fe68 | 124 | enable2=0; |
mafischl | 0:92661f51fe68 | 125 | latch2=0; |
mafischl | 0:92661f51fe68 | 126 | wait(2); |
mafischl | 0:92661f51fe68 | 127 | |
mafischl | 0:92661f51fe68 | 128 | RGB_LED(50,50,50,50,50,50); |
mafischl | 0:92661f51fe68 | 129 | |
mafischl | 0:92661f51fe68 | 130 | wait(5); |
mafischl | 0:92661f51fe68 | 131 | |
mafischl | 0:92661f51fe68 | 132 | while (1) { |
mafischl | 0:92661f51fe68 | 133 | //Analog Input |
mafischl | 0:92661f51fe68 | 134 | distance = 11.261/sonar; //0.4 (80cm) to 0.98 (6cm) |
mafischl | 0:92661f51fe68 | 135 | |
mafischl | 0:92661f51fe68 | 136 | if ((distance < 40)||(pushbutton == 0)) { |
mafischl | 0:92661f51fe68 | 137 | FILE *wave_file; // create pointer |
mafischl | 0:92661f51fe68 | 138 | wave_file = fopen("/local/scream.wav","r"); // Open wave file |
mafischl | 0:92661f51fe68 | 139 | RGB_LED(50,0,0,50,0,0); // LEDS to Red |
mafischl | 0:92661f51fe68 | 140 | waver.play(wave_file); // Play Sound |
mafischl | 0:92661f51fe68 | 141 | fclose(wave_file); // Close File |
mafischl | 0:92661f51fe68 | 142 | } else { |
mafischl | 0:92661f51fe68 | 143 | if (j_increase) { |
mafischl | 0:92661f51fe68 | 144 | j = j + 2; |
mafischl | 0:92661f51fe68 | 145 | if (j > 48) { |
mafischl | 0:92661f51fe68 | 146 | j_increase = 0; |
mafischl | 0:92661f51fe68 | 147 | } |
mafischl | 0:92661f51fe68 | 148 | } else { |
mafischl | 0:92661f51fe68 | 149 | j=j - 2; |
mafischl | 0:92661f51fe68 | 150 | if (j < 2) { |
mafischl | 0:92661f51fe68 | 151 | j_increase = 1; |
mafischl | 0:92661f51fe68 | 152 | } |
mafischl | 0:92661f51fe68 | 153 | } |
mafischl | 0:92661f51fe68 | 154 | RGB_LED(0,50-j,j,0,50-j,j); // Fade between blue and green |
mafischl | 0:92661f51fe68 | 155 | |
mafischl | 0:92661f51fe68 | 156 | } |
mafischl | 0:92661f51fe68 | 157 | wdt.kick(); |
mafischl | 0:92661f51fe68 | 158 | } |
mafischl | 0:92661f51fe68 | 159 | |
mafischl | 0:92661f51fe68 | 160 | } |