4180 lab 1

Dependencies:   mbed MCP23S17 PinDetect USBDevice

Committer:
emilywilson
Date:
Wed Jan 15 18:13:43 2020 +0000
Revision:
0:c1654e25cc43
Child:
1:f121b88d9c85
parts 1-3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilywilson 0:c1654e25cc43 1 #include "mbed.h"
emilywilson 0:c1654e25cc43 2 #include "RGBLed.h"
emilywilson 0:c1654e25cc43 3 #include "PinDetect.h"
emilywilson 0:c1654e25cc43 4
emilywilson 0:c1654e25cc43 5 DigitalOut myled(p26);
emilywilson 0:c1654e25cc43 6 DigitalIn pb(p22);
emilywilson 0:c1654e25cc43 7 DigitalIn pb2(p21);
emilywilson 0:c1654e25cc43 8
emilywilson 0:c1654e25cc43 9 // p10 is most significant bit
emilywilson 0:c1654e25cc43 10 //BusIn ledSelect(p8, p9, p10);
emilywilson 0:c1654e25cc43 11 //
emilywilson 0:c1654e25cc43 12 //PinName redPin = p11;
emilywilson 0:c1654e25cc43 13 //PinName greenPin = p12;
emilywilson 0:c1654e25cc43 14 //PinName bluePin = p13;
emilywilson 0:c1654e25cc43 15 //
emilywilson 0:c1654e25cc43 16 PinDetect pb1(p8);
emilywilson 0:c1654e25cc43 17 PinDetect pb2(p9);
emilywilson 0:c1654e25cc43 18
emilywilson 0:c1654e25cc43 19 float volatile p;
emilywilson 0:c1654e25cc43 20
emilywilson 0:c1654e25cc43 21 void pb1_hit_callback() {
emilywilson 0:c1654e25cc43 22 if (p > 0) {
emilywilson 0:c1654e25cc43 23 p -= 0.1;
emilywilson 0:c1654e25cc43 24 }
emilywilson 0:c1654e25cc43 25 }
emilywilson 0:c1654e25cc43 26
emilywilson 0:c1654e25cc43 27 void pb2_hit_callback() {
emilywilson 0:c1654e25cc43 28 if (p < 0) {
emilywilson 0:c1654e25cc43 29 p += 0.1;
emilywilson 0:c1654e25cc43 30 }
emilywilson 0:c1654e25cc43 31 }
emilywilson 0:c1654e25cc43 32
emilywilson 0:c1654e25cc43 33 int main() {
emilywilson 0:c1654e25cc43 34 while(1) {
emilywilson 0:c1654e25cc43 35 // Part 1
emilywilson 0:c1654e25cc43 36 myled = !pb;
emilywilson 0:c1654e25cc43 37
emilywilson 0:c1654e25cc43 38 // Part 2
emilywilson 0:c1654e25cc43 39 float p = 1.0f;
emilywilson 0:c1654e25cc43 40 pb1.mode(PullUp);
emilywilson 0:c1654e25cc43 41 pb2.mode(PullUp);
emilywilson 0:c1654e25cc43 42 wait(0.1)
emilywilson 0:c1654e25cc43 43
emilywilson 0:c1654e25cc43 44 if (pb1) {
emilywilson 0:c1654e25cc43 45 if (p > 0) {
emilywilson 0:c1654e25cc43 46 p -= 0.1;
emilywilson 0:c1654e25cc43 47 }
emilywilson 0:c1654e25cc43 48 } else if (pb2) {
emilywilson 0:c1654e25cc43 49 if (p < 1) {
emilywilson 0:c1654e25cc43 50 p += 0.1;
emilywilson 0:c1654e25cc43 51 }
emilywilson 0:c1654e25cc43 52 }
emilywilson 0:c1654e25cc43 53 myled = p;
emilywilson 0:c1654e25cc43 54
emilywilson 0:c1654e25cc43 55 // Part 3
emilywilson 0:c1654e25cc43 56 // RGBLed myRBGLed = RGBLed(redPin, greenPin, bluePin);
emilywilson 0:c1654e25cc43 57 // float redVal = 0.0f;
emilywilson 0:c1654e25cc43 58 // float greenVal = 0.0f;
emilywilson 0:c1654e25cc43 59 // float blueVal = 0.0f;
emilywilson 0:c1654e25cc43 60 //
emilywilson 0:c1654e25cc43 61 // pb1.mode(PullUp)
emilywilson 0:c1654e25cc43 62 // pb2.mode(PullUp)
emilywilson 0:c1654e25cc43 63 // wait(0.1)
emilywilson 0:c1654e25cc43 64 //
emilywilson 0:c1654e25cc43 65 // // Possibly try attach_asserted
emilywilson 0:c1654e25cc43 66 // pb1.attach_deasserted(&pb1_hit_callback);
emilywilson 0:c1654e25cc43 67 // pb2.attach_deasserted(&pb2_hit_callback);
emilywilson 0:c1654e25cc43 68 // pb1.setSampleFrequency();
emilywilson 0:c1654e25cc43 69 // pb2.setSampleFrequency();
emilywilson 0:c1654e25cc43 70 //
emilywilson 0:c1654e25cc43 71 // if (ledSelect & 0x1) {
emilywilson 0:c1654e25cc43 72 // redVal = 1.0 * p;
emilywilson 0:c1654e25cc43 73 // }
emilywilson 0:c1654e25cc43 74 // if (ledSelect & 0x2) {
emilywilson 0:c1654e25cc43 75 // greenVal = 1.0 * p;
emilywilson 0:c1654e25cc43 76 // }
emilywilson 0:c1654e25cc43 77 // if (ledselect & 0x4) {
emilywilson 0:c1654e25cc43 78 // blueVal = 1.0 * p;
emilywilson 0:c1654e25cc43 79 // }
emilywilson 0:c1654e25cc43 80 // }
emilywilson 0:c1654e25cc43 81
emilywilson 0:c1654e25cc43 82 // Part 4
emilywilson 0:c1654e25cc43 83
emilywilson 0:c1654e25cc43 84 }