Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 2:9c5448e60f15, committed 2016-10-04
- Comitter:
- isaeldiaz@developer.mbed.org
- Date:
- Tue Oct 04 21:23:14 2016 +0200
- Parent:
- 1:464fc8833089
- Commit message:
- The echo functions do not attach to the InterruptIn class, not sure why
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Sep 08 15:38:58 2016 +0200
+++ b/main.cpp Tue Oct 04 21:23:14 2016 +0200
@@ -5,70 +5,55 @@
enum eDirection { COAST, FORWARD, REVERSE, BRAKE};
MicroBit uBit;
-MicrobitUltrasound sonar(MICROBIT_PIN_P1, MICROBIT_PIN_P2);
+MicrobitUltrasound sonar(MICROBIT_PIN_P1,MICROBIT_PIN_BUTTON_A);
+TimedInterruptIn eventIn(MICROBIT_PIN_BUTTON_A);
-//MicroBitPin P1 = MicroBitPin(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_DIGITAL);
-
-
-void driveMotors(eDirection dir)
+void onPressedB(MicroBitEvent e)
{
- switch(dir)
- {
- case FORWARD:
- uBit.display.scroll("FORWARD");
- uBit.io.P0.setDigitalValue(1);
- uBit.io.P8.setDigitalValue(1);
- uBit.io.P12.setDigitalValue(0);
- uBit.io.P16.setDigitalValue(0);
- break;
- case REVERSE:
- uBit.display.scroll("REVERSE");
- uBit.io.P0.setDigitalValue(0);
- uBit.io.P8.setDigitalValue(0);
- uBit.io.P12.setDigitalValue(1);
- uBit.io.P16.setDigitalValue(1);
- break;
- case BRAKE:
- uBit.display.scroll("BRAKE");
- uBit.io.P0.setDigitalValue(1);
- uBit.io.P8.setDigitalValue(1);
- uBit.io.P12.setDigitalValue(1);
- uBit.io.P16.setDigitalValue(1);
- break;
- default:
- uBit.io.P0.setDigitalValue(0);
- uBit.io.P8.setDigitalValue(0);
- uBit.io.P12.setDigitalValue(0);
- uBit.io.P16.setDigitalValue(0);
- break;
- }
-
+ int duration = e.timestamp;
+ uBit.serial.printf("B%d, ", duration);
+}
+void onPressedA(MicroBitEvent e)
+{
+ int duration = e.timestamp;
+ uBit.serial.printf("A%d, ", duration);
}
-void onPulse(MicroBitEvent e)
+void onPulse()
{
- int duration = e.timestamp;
- uBit.serial.printf("%d, ", duration);
+ uBit.serial.printf("it works!");
}
+void onTrigger(MicroBitEvent e)
+{
+ uBit.serial.printf(".");
+}
+
+
int main()
{
- // Initialise the micro:bit runtime.
- uBit.init();
+// // Initialise the micro:bit runtime.
+// uBit.init();
uBit.serial.printf("Initializing\n");
- uBit.display.scroll("SONAR");
+ uBit.display.scroll("1");
- //uBit.messageBus.listen(MICROBIT_ID_IO_P1, MICROBIT_PIN_EVT_PULSE_LO, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE);
- uBit.messageBus.listen(MICROBIT_ID_ULTRASOUND, MICROBIT_ULTRASOUND_EVT_ECHO_PULSE_LO, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE);
- sonar.start();
- while(1){
- uBit.sleep(2000);
- }
+// uBit.messageBus.listen(MICROBIT_ID_ULTRASOUND, MICROBIT_ULTRASOUND_EVT_ECHO_PULSE_LO, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE);
+// uBit.messageBus.listen(MICROBIT_ID_ULTRASOUND, MICROBIT_ULTRASOUND_EVT_TRIGGER, onTrigger, MESSAGE_BUS_LISTENER_IMMEDIATE);
+// sonar.eventOn(MICROBIT_ULTRASOUND_EVT_TRIGGER);
+// sonar.eventOn(MICROBIT_ULTRASOUND_EVT_ECHO_PULSE_LO);
+ //sonar.start();
+ //sonar.setPeriod(3000);
+ //sonar.onEchoRise();
+ eventIn.rise(&onPulse);
+ //uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onPressedA);
+ uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onPressedB);
- // If main exits, there may still be other fibers running or registered event handlers etc.
- // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
- // sit in the idle task forever, in a power efficient sleep.
+ while(1)
+ {
+ fiber_sleep(3000);
+ }
+
release_fiber();
}