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:b88fecb5232c, committed 2019-07-29
- Comitter:
- BertieHarte
- Date:
- Mon Jul 29 20:33:04 2019 +0000
- Parent:
- 1:760c5b759868
- Commit message:
- Final version. Added counter to loop, program will stop once 2 loops completed.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Jul 29 08:40:09 2019 +0000
+++ b/main.cpp Mon Jul 29 20:33:04 2019 +0000
@@ -1,24 +1,25 @@
+//lab 2_4_1_Modified.
+// Modify the code so that the for loop parameters are variables which can be initialised at the start of the program
+// and modified in the while loop to produce a different sound second time around.
#include "mbed.h"
-//modified version of initial program.
-// declare varibles for 'i+=' (rate of change) and spkr.period calculation (pitch).
+
DigitalIn fire(p14); // centre pin of joystick.
PwmOut spkr(p26); // speaker output
-// add analog inputs for pot 1 & 2
-AnalogIn pot1(p19);
-AnalogIn pot2(p20);
-
+int b = 1; // base time for pwm output. this will be divided by the value of 'r'. This will increment up by 10 after first loop.
+int r = 100; // base value for the increment of float 'i'. This is used in the PWM calculation, and will increment up by 10 after the first loop.
+int c = 0; //counter on loop.
int main() {
- while(1) {
- float roc = (pot1*100); // read pot 1 and * by 100.
- printf("pot1 = %.0f \n\r", (roc)); // used for display during testing.
- float spk = (pot2*10); // read pot 1 and * by 10.
- printf("pot2 = %.0f \n\r", (spk)); // display for testing
- for (float i=2000.0; i<10000; i+=(roc)) { // increment i by the calculated roc each loop if the value at the start of the loop is less than 1000
- spkr.period((spk)/i); // base time is taken for the pot 2 calculation.
+ while(c < 2) { // only repeat loop twice (c will increment after each pass), once c !<2 the program will stop.
+ for (float i=2000.0; i<10000; i+=r) { // increment i by 'r' each loop if the value at the start of the loop is less than 1000
+ spkr.period((b)/i); // base time/i
spkr=0.5;
wait(0.1);
}
spkr=0.0;
+ b += 10;// increment b by +10
+ r += 10; // increment r by +10
+ c +=1;// increment count by one.
+ printf("Count is %i \n\r", c); // only for display purposes during testing.
while(!fire) {}
}
}