accelerometer trial

Dependencies:   mbed C12832 MMA7660 USBDevice

Committer:
knasp
Date:
Tue Oct 29 20:14:10 2019 +0000
Revision:
24:5c54ff941447
Parent:
23:4bf323a37a92
Child:
25:f8db641d33ab
final 2.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:291a88a2c151 1 #include "mbed.h"
samux 1:291a88a2c151 2 #include "USBKeyboard.h"
knasp 7:ffe2220d3880 3 #include "MMA7660.h"
knasp 10:150f8d03f18d 4 #include "C12832.h"
knasp 9:66681432403a 5
knasp 17:0b1c5f6dbb5c 6 MMA7660 Accelerometer (p28,p27); //define accelerometer
knasp 17:0b1c5f6dbb5c 7 USBKeyboard Usbkeyboard; //define keyboard
knasp 17:0b1c5f6dbb5c 8 C12832 LCD (p5,p7,p6,p8,p11); //define LCD
knasp 17:0b1c5f6dbb5c 9 DigitalOut led1(p12); //leds
knasp 17:0b1c5f6dbb5c 10 DigitalOut led2(p13);
knasp 17:0b1c5f6dbb5c 11 DigitalOut led3(p14);
knasp 17:0b1c5f6dbb5c 12 DigitalOut led4(p15);
knasp 17:0b1c5f6dbb5c 13
knasp 23:4bf323a37a92 14 DigitalIn Switch1 (p17); //switch 1
knasp 23:4bf323a37a92 15 DigitalIn Switch2 (p16); //switch 2
knasp 23:4bf323a37a92 16 DigitalIn Switch3 (p18); //spacebar
knasp 9:66681432403a 17
knasp 17:0b1c5f6dbb5c 18 int main (void) //main program
knasp 11:73e8c1bb5467 19 {
knasp 23:4bf323a37a92 20 //enum Orientation {Up, Down,Right, Left,Back, Front,Unknown};
knasp 23:4bf323a37a92 21 MMA7660::Orientation myOrientation; //defining orientation
knasp 23:4bf323a37a92 22
knasp 23:4bf323a37a92 23 while (true) {
knasp 23:4bf323a37a92 24
knasp 23:4bf323a37a92 25
knasp 23:4bf323a37a92 26 myOrientation = Accelerometer.getOrientation (); //getting the orientation from accelerometer
knasp 14:116f94a428c9 27
knasp 23:4bf323a37a92 28 if (Switch3){
knasp 23:4bf323a37a92 29
knasp 23:4bf323a37a92 30 Usbkeyboard.printf (" "); //spacebar
knasp 23:4bf323a37a92 31
knasp 23:4bf323a37a92 32 }
knasp 23:4bf323a37a92 33
knasp 24:5c54ff941447 34 if ((!Switch1) && (Switch2)){ //letters
knasp 14:116f94a428c9 35
knasp 23:4bf323a37a92 36 if (myOrientation == MMA7660::Up) {
knasp 23:4bf323a37a92 37
knasp 23:4bf323a37a92 38 LCD.printf("LEFT");
knasp 23:4bf323a37a92 39 Usbkeyboard.printf ("a\0"); //instead of simulating pressing of the "left arrow", simulates pressing "a key"
knasp 23:4bf323a37a92 40 led2=0;
knasp 23:4bf323a37a92 41 led1=0;
knasp 23:4bf323a37a92 42 led3=0;
knasp 23:4bf323a37a92 43 led4=1;
knasp 23:4bf323a37a92 44 } else if (myOrientation == MMA7660::Down) {
knasp 23:4bf323a37a92 45
knasp 23:4bf323a37a92 46 LCD.printf("right");
knasp 23:4bf323a37a92 47 Usbkeyboard.printf ("d\0");
knasp 23:4bf323a37a92 48 led3=0;
knasp 23:4bf323a37a92 49 led1=0;
knasp 23:4bf323a37a92 50 led2=1;
knasp 23:4bf323a37a92 51 led4=0;
knasp 23:4bf323a37a92 52 } else if (myOrientation == MMA7660::Left) {
knasp 23:4bf323a37a92 53
knasp 23:4bf323a37a92 54 LCD.printf("Up");
knasp 23:4bf323a37a92 55 Usbkeyboard.printf ("w\0");
knasp 23:4bf323a37a92 56 led1=1;
knasp 23:4bf323a37a92 57 led3=0;
knasp 23:4bf323a37a92 58 led2=0;
knasp 23:4bf323a37a92 59 led4=0;
knasp 23:4bf323a37a92 60 } else if (myOrientation == MMA7660::Right) {
knasp 23:4bf323a37a92 61
knasp 23:4bf323a37a92 62 LCD.printf("Down");
knasp 23:4bf323a37a92 63 Usbkeyboard.printf ("s\0");
knasp 23:4bf323a37a92 64 led4=0;
knasp 23:4bf323a37a92 65 led1=0;
knasp 23:4bf323a37a92 66 led3=1;
knasp 23:4bf323a37a92 67 led2=0;
knasp 23:4bf323a37a92 68
knasp 23:4bf323a37a92 69
knasp 23:4bf323a37a92 70
knasp 14:116f94a428c9 71 }
knasp 23:4bf323a37a92 72
knasp 23:4bf323a37a92 73 else if (myOrientation == MMA7660::Unknown) {
knasp 23:4bf323a37a92 74
knasp 23:4bf323a37a92 75 LCD.printf("Don't Know");
knasp 23:4bf323a37a92 76
knasp 23:4bf323a37a92 77 }
knasp 23:4bf323a37a92 78 }
knasp 23:4bf323a37a92 79
knasp 24:5c54ff941447 80 else if ((!Switch1) && (!Switch2)){ //standby mode
knasp 24:5c54ff941447 81 led3=1;
knasp 24:5c54ff941447 82 led1=1;
knasp 24:5c54ff941447 83 led2=1;
knasp 24:5c54ff941447 84 led4=1;
knasp 23:4bf323a37a92 85 }
knasp 23:4bf323a37a92 86
knasp 24:5c54ff941447 87 else if ((Switch1) && (!Switch2)){ //numbers
knasp 23:4bf323a37a92 88
knasp 23:4bf323a37a92 89 if (myOrientation == MMA7660::Up) {
knasp 23:4bf323a37a92 90
knasp 22:a255721ffc58 91 LCD.printf("One");
knasp 22:a255721ffc58 92 Usbkeyboard.printf ("1\0"); //instead of simulating pressing of the "left arrow", simulates pressing "numbers"
knasp 22:a255721ffc58 93 led2=0;
knasp 22:a255721ffc58 94 led1=0;
knasp 22:a255721ffc58 95 led3=0;
knasp 22:a255721ffc58 96 led4=1;
knasp 23:4bf323a37a92 97 } else if (myOrientation == MMA7660::Down) {
knasp 23:4bf323a37a92 98
knasp 22:a255721ffc58 99 LCD.printf("Three");
knasp 22:a255721ffc58 100 Usbkeyboard.printf ("3\0");
knasp 22:a255721ffc58 101 led3=0;
knasp 22:a255721ffc58 102 led1=0;
knasp 22:a255721ffc58 103 led2=1;
knasp 22:a255721ffc58 104 led4=0;
knasp 23:4bf323a37a92 105 } else if (myOrientation == MMA7660::Left) {
knasp 23:4bf323a37a92 106
knasp 22:a255721ffc58 107 LCD.printf("Two");
knasp 22:a255721ffc58 108 Usbkeyboard.printf ("2\0");
knasp 22:a255721ffc58 109 led1=1;
knasp 22:a255721ffc58 110 led3=0;
knasp 22:a255721ffc58 111 led2=0;
knasp 22:a255721ffc58 112 led4=0;
knasp 23:4bf323a37a92 113 } else if (myOrientation == MMA7660::Right) {
knasp 23:4bf323a37a92 114
knasp 22:a255721ffc58 115 LCD.printf("Four");
knasp 22:a255721ffc58 116 Usbkeyboard.printf ("4\0");
knasp 22:a255721ffc58 117 led4=0;
knasp 22:a255721ffc58 118 led1=0;
knasp 22:a255721ffc58 119 led3=1;
knasp 22:a255721ffc58 120 led2=0;
knasp 23:4bf323a37a92 121
knasp 23:4bf323a37a92 122
knasp 22:a255721ffc58 123 }
knasp 23:4bf323a37a92 124
knasp 23:4bf323a37a92 125
knasp 23:4bf323a37a92 126 else if (myOrientation == MMA7660::Unknown) {
knasp 23:4bf323a37a92 127
knasp 23:4bf323a37a92 128 LCD.printf("Don't Know");
knasp 23:4bf323a37a92 129
knasp 23:4bf323a37a92 130 }
knasp 23:4bf323a37a92 131
knasp 23:4bf323a37a92 132 }
knasp 23:4bf323a37a92 133
knasp 24:5c54ff941447 134 else if ((Switch1) && (Switch2)){ //arrows
knasp 22:a255721ffc58 135
knasp 23:4bf323a37a92 136 // A
knasp 23:4bf323a37a92 137 if (myOrientation == MMA7660::Up) { //if the orientation is up
knasp 23:4bf323a37a92 138
knasp 23:4bf323a37a92 139 LCD.printf("Left"); //print "left" on LCD (due to orientation of the accelerometer on the limb, the directions are changed)
knasp 23:4bf323a37a92 140 Usbkeyboard.keyCode (LEFT_ARROW); //device simulates pressing "left arrow key" on the keyboard
knasp 23:4bf323a37a92 141 led2=0; //led off
knasp 23:4bf323a37a92 142 led1=0; //led off
knasp 23:4bf323a37a92 143 led3=0; //led off
knasp 23:4bf323a37a92 144 led4=1; //led on
knasp 23:4bf323a37a92 145 } else if (myOrientation == MMA7660::Down) {
knasp 23:4bf323a37a92 146
knasp 23:4bf323a37a92 147 LCD.printf("Right");
knasp 23:4bf323a37a92 148 Usbkeyboard.keyCode (RIGHT_ARROW);
knasp 17:0b1c5f6dbb5c 149 led3=0;
knasp 17:0b1c5f6dbb5c 150 led1=0;
knasp 17:0b1c5f6dbb5c 151 led2=1;
knasp 17:0b1c5f6dbb5c 152 led4=0;
knasp 23:4bf323a37a92 153 } else if (myOrientation == MMA7660::Left) {
knasp 23:4bf323a37a92 154
knasp 23:4bf323a37a92 155 LCD.printf("UP");
knasp 23:4bf323a37a92 156 Usbkeyboard.keyCode (UP_ARROW);
knasp 17:0b1c5f6dbb5c 157 led1=1;
knasp 17:0b1c5f6dbb5c 158 led3=0;
knasp 17:0b1c5f6dbb5c 159 led2=0;
knasp 17:0b1c5f6dbb5c 160 led4=0;
knasp 23:4bf323a37a92 161 } else if (myOrientation == MMA7660::Right) {
knasp 23:4bf323a37a92 162
knasp 23:4bf323a37a92 163 LCD.printf("DOWN");
knasp 23:4bf323a37a92 164 Usbkeyboard.keyCode (DOWN_ARROW);
knasp 17:0b1c5f6dbb5c 165 led4=0;
knasp 17:0b1c5f6dbb5c 166 led1=0;
knasp 17:0b1c5f6dbb5c 167 led3=1;
knasp 17:0b1c5f6dbb5c 168 led2=0;
knasp 23:4bf323a37a92 169
knasp 23:4bf323a37a92 170
knasp 23:4bf323a37a92 171 } else if (myOrientation == MMA7660::Unknown) {
knasp 23:4bf323a37a92 172
knasp 23:4bf323a37a92 173 LCD.printf("Don't Know");
knasp 23:4bf323a37a92 174
knasp 17:0b1c5f6dbb5c 175 }
knasp 23:4bf323a37a92 176 }
knasp 23:4bf323a37a92 177 }
knasp 23:4bf323a37a92 178
wojt86 16:6b502d21abf4 179
knasp 11:73e8c1bb5467 180 return false;
knasp 23:4bf323a37a92 181
knasp 22:a255721ffc58 182 }