A Simple Programming Workshop - University of Calgary
Dependencies: mbed C12832 MMA7660
main.cpp@3:7278142d6a1a, 2019-05-15 (annotated)
- Committer:
- mozhdehshahbazi
- Date:
- Wed May 15 00:15:50 2019 +0000
- Revision:
- 3:7278142d6a1a
- Parent:
- 2:c2ebc7fa80eb
Created for STEM Explore - Geomatics 2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mozhdehshahbazi | 3:7278142d6a1a | 1 | #include "mbed.h" // General board |
mpetovello | 0:a19771db916c | 2 | #include "MMA7660.h" // accelerometer sensor |
mpetovello | 0:a19771db916c | 3 | #include "C12832.h" // LCD display |
mpetovello | 0:a19771db916c | 4 | |
mozhdehshahbazi | 3:7278142d6a1a | 5 | //Some ideas: |
mozhdehshahbazi | 3:7278142d6a1a | 6 | // create a for loop to turn the LED on and off with a pattern. For example, every 1 second. |
mozhdehshahbazi | 3:7278142d6a1a | 7 | // create a condition to turn the LED on when the joystick fire pin is pressed, and turn it off when any other pin is pressed |
mpetovello | 0:a19771db916c | 8 | |
mpetovello | 0:a19771db916c | 9 | int main() |
mpetovello | 0:a19771db916c | 10 | { |
mozhdehshahbazi | 3:7278142d6a1a | 11 | // Variables |
mozhdehshahbazi | 3:7278142d6a1a | 12 | int x, y, joy, press; |
mozhdehshahbazi | 3:7278142d6a1a | 13 | float accX, accY, accZ, acc3; |
mozhdehshahbazi | 3:7278142d6a1a | 14 | |
mpetovello | 2:c2ebc7fa80eb | 15 | // create an accelerometer object |
mpetovello | 2:c2ebc7fa80eb | 16 | MMA7660 accelerometer(p28, p27); |
mpetovello | 2:c2ebc7fa80eb | 17 | |
mozhdehshahbazi | 3:7278142d6a1a | 18 | // LCD display object |
mpetovello | 2:c2ebc7fa80eb | 19 | C12832 lcd(p5, p7, p6, p8, p11); |
mpetovello | 2:c2ebc7fa80eb | 20 | |
mozhdehshahbazi | 3:7278142d6a1a | 21 | //An external LED powered by pin 30 |
mozhdehshahbazi | 3:7278142d6a1a | 22 | DigitalOut redled(p30); |
mozhdehshahbazi | 3:7278142d6a1a | 23 | redled = 1; //1 means the LED is turned on |
mozhdehshahbazi | 3:7278142d6a1a | 24 | wait(2.0); //Make the program pause for 2 seconds |
mozhdehshahbazi | 3:7278142d6a1a | 25 | redled=0; // 0 means the LED is turned off |
mpetovello | 2:c2ebc7fa80eb | 26 | |
mozhdehshahbazi | 3:7278142d6a1a | 27 | // joystick object |
mozhdehshahbazi | 3:7278142d6a1a | 28 | BusIn joystick(p15,p12,p13,p16); |
mozhdehshahbazi | 3:7278142d6a1a | 29 | DigitalIn fire(p14); |
mozhdehshahbazi | 3:7278142d6a1a | 30 | |
mozhdehshahbazi | 3:7278142d6a1a | 31 | // Left LED |
mozhdehshahbazi | 3:7278142d6a1a | 32 | DigitalOut leftled(LED1); |
mozhdehshahbazi | 3:7278142d6a1a | 33 | |
mozhdehshahbazi | 3:7278142d6a1a | 34 | //A way to manipulate all the LEDs together |
mozhdehshahbazi | 3:7278142d6a1a | 35 | BusOut LEDs(LED1, LED2, LED3, LED4); |
mpetovello | 2:c2ebc7fa80eb | 36 | |
mpetovello | 0:a19771db916c | 37 | // try to establish a connection with the accelerometer |
mpetovello | 0:a19771db916c | 38 | if( !accelerometer.testConnection() ) |
mpetovello | 0:a19771db916c | 39 | { |
mpetovello | 0:a19771db916c | 40 | // turn on all LEDs (indicating an error state) |
mpetovello | 0:a19771db916c | 41 | LEDs = 0xf; |
mpetovello | 0:a19771db916c | 42 | return 0; |
mpetovello | 0:a19771db916c | 43 | } |
mpetovello | 0:a19771db916c | 44 | |
mpetovello | 0:a19771db916c | 45 | // clear the LCD |
mpetovello | 0:a19771db916c | 46 | lcd.cls(); |
mpetovello | 0:a19771db916c | 47 | |
mpetovello | 0:a19771db916c | 48 | |
mpetovello | 0:a19771db916c | 49 | // TODO: Move to the centre of the LCD and set the corresponding pixel to black |
mpetovello | 0:a19771db916c | 50 | // We will call the current location the "active pixel" |
mozhdehshahbazi | 3:7278142d6a1a | 51 | x = lcd.width() / 2; |
mozhdehshahbazi | 3:7278142d6a1a | 52 | y = lcd.height() / 2; |
mozhdehshahbazi | 3:7278142d6a1a | 53 | lcd.pixel(x, y, 1); |
mozhdehshahbazi | 3:7278142d6a1a | 54 | lcd.copy_to_lcd(); |
mpetovello | 0:a19771db916c | 55 | |
mpetovello | 0:a19771db916c | 56 | // create an infinite loop (i.e., the program runs as long as the processor is on) |
mpetovello | 0:a19771db916c | 57 | while(1) |
mpetovello | 0:a19771db916c | 58 | { |
mpetovello | 0:a19771db916c | 59 | // TODO: Get the state/value of the joystick |
mozhdehshahbazi | 3:7278142d6a1a | 60 | joy = joystick.read(); |
mpetovello | 0:a19771db916c | 61 | |
mpetovello | 0:a19771db916c | 62 | // TODO: Determine the coordinates of the next active pixel based on joystick data |
mozhdehshahbazi | 3:7278142d6a1a | 63 | if (joy == 1 && y > 0) // UP |
mozhdehshahbazi | 3:7278142d6a1a | 64 | { |
mozhdehshahbazi | 3:7278142d6a1a | 65 | --y; |
mozhdehshahbazi | 3:7278142d6a1a | 66 | } |
mozhdehshahbazi | 3:7278142d6a1a | 67 | else if (joy == 2 && y < lcd.height() - 1) // DOWN |
mozhdehshahbazi | 3:7278142d6a1a | 68 | { |
mozhdehshahbazi | 3:7278142d6a1a | 69 | ++y; |
mozhdehshahbazi | 3:7278142d6a1a | 70 | } else if (joy == 4 && x > 0) // LEFT |
mozhdehshahbazi | 3:7278142d6a1a | 71 | { |
mozhdehshahbazi | 3:7278142d6a1a | 72 | --x; |
mozhdehshahbazi | 3:7278142d6a1a | 73 | } |
mozhdehshahbazi | 3:7278142d6a1a | 74 | else if (joy == 8 && x < lcd.width() - 1) // RIGHT |
mozhdehshahbazi | 3:7278142d6a1a | 75 | { |
mozhdehshahbazi | 3:7278142d6a1a | 76 | ++x; |
mozhdehshahbazi | 3:7278142d6a1a | 77 | } |
mozhdehshahbazi | 3:7278142d6a1a | 78 | else if (joy == 5 && x > 0 && y > 0) // LEFT-UP |
mozhdehshahbazi | 3:7278142d6a1a | 79 | { |
mozhdehshahbazi | 3:7278142d6a1a | 80 | --x; |
mozhdehshahbazi | 3:7278142d6a1a | 81 | --y; |
mozhdehshahbazi | 3:7278142d6a1a | 82 | } |
mozhdehshahbazi | 3:7278142d6a1a | 83 | else if (joy == 6 && x > 0 && y < lcd.height() - 1) // LEFT-DOWN |
mozhdehshahbazi | 3:7278142d6a1a | 84 | { |
mozhdehshahbazi | 3:7278142d6a1a | 85 | --x; |
mozhdehshahbazi | 3:7278142d6a1a | 86 | ++y; |
mozhdehshahbazi | 3:7278142d6a1a | 87 | } |
mozhdehshahbazi | 3:7278142d6a1a | 88 | else if (joy == 9 && x < lcd.width() - 1 && y > 0) // RIGHT-UP |
mozhdehshahbazi | 3:7278142d6a1a | 89 | { |
mozhdehshahbazi | 3:7278142d6a1a | 90 | ++x; |
mozhdehshahbazi | 3:7278142d6a1a | 91 | --y; |
mozhdehshahbazi | 3:7278142d6a1a | 92 | } |
mozhdehshahbazi | 3:7278142d6a1a | 93 | else if (joy == 10 && x < lcd.width() - 1 && y < lcd.height() - 1) |
mozhdehshahbazi | 3:7278142d6a1a | 94 | { // RIGHT-DOWN |
mozhdehshahbazi | 3:7278142d6a1a | 95 | ++x; |
mozhdehshahbazi | 3:7278142d6a1a | 96 | ++y; |
mozhdehshahbazi | 3:7278142d6a1a | 97 | } |
mozhdehshahbazi | 3:7278142d6a1a | 98 | |
mozhdehshahbazi | 3:7278142d6a1a | 99 | // TODO: Set the next pixel to black |
mozhdehshahbazi | 3:7278142d6a1a | 100 | lcd.pixel(x, y, 1); |
mozhdehshahbazi | 3:7278142d6a1a | 101 | lcd.copy_to_lcd(); |
mozhdehshahbazi | 3:7278142d6a1a | 102 | |
mpetovello | 0:a19771db916c | 103 | |
mpetovello | 0:a19771db916c | 104 | |
mozhdehshahbazi | 3:7278142d6a1a | 105 | // TODO: Get the accelerometer measurements |
mpetovello | 0:a19771db916c | 106 | |
mozhdehshahbazi | 3:7278142d6a1a | 107 | accX = accelerometer.x(); |
mozhdehshahbazi | 3:7278142d6a1a | 108 | accY = accelerometer.y(); |
mozhdehshahbazi | 3:7278142d6a1a | 109 | accZ = accelerometer.z(); |
mozhdehshahbazi | 3:7278142d6a1a | 110 | acc3 = sqrt(accX * accX + accY * accY + accZ * accZ); //this the amount of acceleration (how fast you displace or shake the board!) |
mpetovello | 0:a19771db916c | 111 | |
mpetovello | 0:a19771db916c | 112 | // TODO: Check if the screen should be cleared based on the accelerometer |
mpetovello | 0:a19771db916c | 113 | // measurements, and if so, clear the screen but don't move the active |
mpetovello | 0:a19771db916c | 114 | // pixel. |
mpetovello | 0:a19771db916c | 115 | // |
mpetovello | 0:a19771db916c | 116 | // NOTE: Although you may have an idea of how to do this, it may take some |
mpetovello | 0:a19771db916c | 117 | // "tuning" to get it to "feel right". |
mozhdehshahbazi | 3:7278142d6a1a | 118 | |
mozhdehshahbazi | 3:7278142d6a1a | 119 | if (acc3 > 1.5 || fire ) { |
mozhdehshahbazi | 3:7278142d6a1a | 120 | |
mozhdehshahbazi | 3:7278142d6a1a | 121 | lcd.cls(); |
mozhdehshahbazi | 3:7278142d6a1a | 122 | x = lcd.width() / 2; |
mozhdehshahbazi | 3:7278142d6a1a | 123 | y = lcd.height() / 2; |
mozhdehshahbazi | 3:7278142d6a1a | 124 | lcd.pixel(x, y, 1); |
mozhdehshahbazi | 3:7278142d6a1a | 125 | lcd.copy_to_lcd(); |
mozhdehshahbazi | 3:7278142d6a1a | 126 | |
mozhdehshahbazi | 3:7278142d6a1a | 127 | leftled=1; |
mozhdehshahbazi | 3:7278142d6a1a | 128 | wait(0.2); |
mozhdehshahbazi | 3:7278142d6a1a | 129 | leftled=0; |
mozhdehshahbazi | 3:7278142d6a1a | 130 | |
mozhdehshahbazi | 3:7278142d6a1a | 131 | } |
mpetovello | 0:a19771db916c | 132 | |
mpetovello | 0:a19771db916c | 133 | |
mpetovello | 0:a19771db916c | 134 | // wait a short period |
mpetovello | 0:a19771db916c | 135 | wait(0.1); |
mpetovello | 0:a19771db916c | 136 | } |
mpetovello | 0:a19771db916c | 137 | |
mpetovello | 0:a19771db916c | 138 | // end of program |
mpetovello | 0:a19771db916c | 139 | return 0; |
mpetovello | 0:a19771db916c | 140 | } |