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.
Dependencies: mbed-rtos mbed Servo Motor SoftPWM
Diff: main.cpp
- Revision:
- 7:f65ab31f81e7
- Parent:
- 5:e5bf798e0522
- Child:
- 8:c54f1cfcbfe0
--- a/main.cpp Wed Nov 16 00:12:04 2022 +0000
+++ b/main.cpp Wed Nov 16 01:35:20 2022 +0000
@@ -10,6 +10,18 @@
Motor left_motor(p23, p15, p16); // pwm, fwd, rev
Motor right_motor(p22, p17, p18); // pwm, fwd, rev
+// IR
+Serial ir_device(p13, p14); // tx, rx
+DigitalOut rx_led(LED1); //LED1 and LED2 indicate TX/RX activity for IR
+DigitalOut tx_led(LED2);
+PwmOut IRLED(p21);
+
+// Lives LEDs
+BusOut lives_leds(p5, p6, p7);
+
+// Speaker
+PwmOut speaker(p25);
+
volatile float speed_left = 0;
volatile float speed_right = 0;
volatile float turret_delta = 0;
@@ -18,12 +30,13 @@
Thread thread_drive;
Thread thread_turret;
-Thread thread_fire;
+Thread thread_ir_receive;
void input_loop();
inline void set_speed(float l, float r);
inline void set_turret_delta(float delta);
inline void fire_cannon(bool f);
+inline void decrease_lives();
void input_thread() {
@@ -53,15 +66,30 @@
}
}
-void fire_thread() {
-
+void ir_receive_thread() {
+ // IR setup
+ //Drive IR LED data pin with 38Khz PWM Carrier
+ //Drive IR LED gnd pin with serial TX
+ IRLED.period(1.0/38000.0);
+ IRLED = 0.5;
+ ir_device.baud(2400);
+ while(1) {
+ if(ir_device.readable()) {
+ rx_led = 1;
+ pc.putc(ir_device.getc());
+ decrease_lives();
+ rx_led = 0;
+ }
+ }
}
int main() {
+ lives_leds = 7;
+
thread_drive.start(drive_thread);
//thread_turret.start(turret_thread);
- thread_fire.start(fire_thread);
+ thread_ir_receive.start(ir_receive_thread);
input_loop();
}
@@ -145,5 +173,19 @@
}
inline void fire_cannon(bool f) {
- fire = f;
+ if (f) {
+ tx_led = 1;
+ ir_device.putc('f');
+ tx_led = 0;
+ }
+}
+
+inline void decrease_lives() {
+ if (lives_leds == 7) {
+ lives_leds = 3;
+ } else if (lives_leds == 3) {
+ lives_leds = 1;
+ } else {
+ lives_leds = 0;
+ }
}
\ No newline at end of file
