hadrovic oled

Dependencies:   mbed

Committer:
perodot
Date:
Wed Apr 09 13:32:39 2014 +0000
Revision:
0:b13343630d26
Hardovic Oled

Who changed what in which revision?

UserRevisionLine numberNew contents of line
perodot 0:b13343630d26 1 /**
perodot 0:b13343630d26 2 * This project contains a an example monitoring the light sensor,
perodot 0:b13343630d26 3 * accelerometer and trim potentiometer and
perodot 0:b13343630d26 4 * displaying the values on the OLED display.
perodot 0:b13343630d26 5 */
perodot 0:b13343630d26 6
perodot 0:b13343630d26 7 #include "mbed.h"
perodot 0:b13343630d26 8 #include "adc.h"
perodot 0:b13343630d26 9 #include "acc.h"
perodot 0:b13343630d26 10 #include "light.h"
perodot 0:b13343630d26 11 #include "EAOLED.h"
perodot 0:b13343630d26 12
perodot 0:b13343630d26 13 DigitalOut myled(LED1);
perodot 0:b13343630d26 14 //Initialise ADC to 100000 sample rate and cclk divide set to 1
perodot 0:b13343630d26 15 ADC adc(100000, 1);
perodot 0:b13343630d26 16 EAOLED oled(p5, p6, p7, p8, p25); // mosi, dnc, sclk, cs, power
perodot 0:b13343630d26 17 ACC acc(p28, p27);
perodot 0:b13343630d26 18 LIGHT light(p28, p27);
perodot 0:b13343630d26 19
perodot 0:b13343630d26 20
perodot 0:b13343630d26 21 int main() {
perodot 0:b13343630d26 22 int xoff = 0;
perodot 0:b13343630d26 23 int yoff = 0;
perodot 0:b13343630d26 24 int zoff = 0;
perodot 0:b13343630d26 25
perodot 0:b13343630d26 26 int x = 0;
perodot 0:b13343630d26 27 int y = 0;
perodot 0:b13343630d26 28 int z = 0;
perodot 0:b13343630d26 29
perodot 0:b13343630d26 30 int lux = 0;
perodot 0:b13343630d26 31
perodot 0:b13343630d26 32 //Set up ADC on pin 15 (channel 0)
perodot 0:b13343630d26 33 adc.setup(p15,1);
perodot 0:b13343630d26 34 //No interrupt
perodot 0:b13343630d26 35 adc.interrupt_state(p15, 0);
perodot 0:b13343630d26 36 //Measure pin 15
perodot 0:b13343630d26 37 adc.select(p15);
perodot 0:b13343630d26 38
perodot 0:b13343630d26 39
perodot 0:b13343630d26 40 light.enable();
perodot 0:b13343630d26 41 light.setRange(light.LIGHT_RANGE_4000);
perodot 0:b13343630d26 42
perodot 0:b13343630d26 43 // Assume base board in zero-g position when reading first value.
perodot 0:b13343630d26 44 acc.read(&x, &y, &z);
perodot 0:b13343630d26 45 xoff = 0-x;
perodot 0:b13343630d26 46 yoff = 0-y;
perodot 0:b13343630d26 47 zoff = 64-z;
perodot 0:b13343630d26 48 //Clear screen
perodot 0:b13343630d26 49 oled.cls();
perodot 0:b13343630d26 50 oled.locate(0,1);
perodot 0:b13343630d26 51 oled.printf("Light: ");
perodot 0:b13343630d26 52 oled.locate(0,2);
perodot 0:b13343630d26 53 oled.printf("Pot : ");
perodot 0:b13343630d26 54 oled.locate(0,3);
perodot 0:b13343630d26 55 oled.printf("Acc x: ");
perodot 0:b13343630d26 56 oled.locate(0,4);
perodot 0:b13343630d26 57 oled.printf("Acc y: ");
perodot 0:b13343630d26 58 oled.locate(0,5);
perodot 0:b13343630d26 59 oled.printf("Acc z: ");
perodot 0:b13343630d26 60
perodot 0:b13343630d26 61 oled.locate(0,6);
perodot 0:b13343630d26 62 oled.printf("Petar");
perodot 0:b13343630d26 63
perodot 0:b13343630d26 64 while (1) {
perodot 0:b13343630d26 65
perodot 0:b13343630d26 66 //light
perodot 0:b13343630d26 67 lux = light.read();
perodot 0:b13343630d26 68 oled.locate(8,1);
perodot 0:b13343630d26 69 oled.printf(" ");
perodot 0:b13343630d26 70 oled.locate(8,1);
perodot 0:b13343630d26 71 oled.printf("%d", lux);
perodot 0:b13343630d26 72
perodot 0:b13343630d26 73
perodot 0:b13343630d26 74 adc.start();
perodot 0:b13343630d26 75 //Wait for it to complete
perodot 0:b13343630d26 76 while (!adc.done(p15));
perodot 0:b13343630d26 77 //Get trimpot
perodot 0:b13343630d26 78 int trimpot = adc.read(p15);
perodot 0:b13343630d26 79 oled.locate(8,2);
perodot 0:b13343630d26 80 oled.printf(" ");
perodot 0:b13343630d26 81 oled.locate(8,2);
perodot 0:b13343630d26 82 oled.printf("%d", trimpot);
perodot 0:b13343630d26 83
perodot 0:b13343630d26 84 // Accelerometer
perodot 0:b13343630d26 85 acc.read(&x, &y, &z);
perodot 0:b13343630d26 86 x = x+xoff;
perodot 0:b13343630d26 87 y = y+yoff;
perodot 0:b13343630d26 88 z = z+zoff;
perodot 0:b13343630d26 89
perodot 0:b13343630d26 90
perodot 0:b13343630d26 91 oled.locate(8,3);
perodot 0:b13343630d26 92 oled.printf(" ");
perodot 0:b13343630d26 93 oled.locate(8,3);
perodot 0:b13343630d26 94 oled.printf("%d",x);
perodot 0:b13343630d26 95
perodot 0:b13343630d26 96
perodot 0:b13343630d26 97 oled.locate(8,4);
perodot 0:b13343630d26 98 oled.printf(" ");
perodot 0:b13343630d26 99 oled.locate(8,4);
perodot 0:b13343630d26 100 oled.printf("%d",y);
perodot 0:b13343630d26 101
perodot 0:b13343630d26 102
perodot 0:b13343630d26 103 oled.locate(8,5);
perodot 0:b13343630d26 104 oled.printf(" ");
perodot 0:b13343630d26 105 oled.locate(8,5);
perodot 0:b13343630d26 106 oled.printf("%d",z);
perodot 0:b13343630d26 107
perodot 0:b13343630d26 108
perodot 0:b13343630d26 109 myled = 1;
perodot 0:b13343630d26 110 wait(0.2);
perodot 0:b13343630d26 111 myled = 0;
perodot 0:b13343630d26 112 wait(0.2);
perodot 0:b13343630d26 113 }
perodot 0:b13343630d26 114 }