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: 4DGL-uLCD-SE SDFileSystem
Revision 14:2c2395937c57, committed 2016-10-18
- Comitter:
- kswanson31
- Date:
- Tue Oct 18 18:34:44 2016 +0000
- Parent:
- 13:c14dc22c38ba
- Commit message:
- Included majority of all lab code, formatted to show device names and pins, device purpose information
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Sep 26 21:58:59 2016 +0000
+++ b/main.cpp Tue Oct 18 18:34:44 2016 +0000
@@ -1,51 +1,92 @@
-#include "mbed.h"
-#include "uLCD_4DGL.h"
-//#include "LSM9DS1.h"
-#include "math.h"
-//#include "SongPlayer.h"
#include <fstream>
#include <string>
+#include "mbed.h"
+#include "LSM9DS1.h"
+#include "math.h"
+#include "uLCD_4DGL.h"
+#include "Servo.h"
+#include "Motor.h"
+#include "StepperMotor_X27168.h"
#include "SDFileSystem.h"
+#include "SongPlayer.h"
-/* serial
+/* Serial via USB
Serial pc(USBTX, USBRX);
*/
-/* PART 1
+/* Sine wave output pin
AnalogOut aout(p18);
-AnalogIn ain(p15);
+*/
+
+/* IR distance sensor
+AnalogIn ain(p15); // p15 <- senor out, yellow
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
*/
-/* PART 3
+/* IMU, using I2C
+LSM9DS1 imu(p9, p10, 0xD6, 0x3C); // dev SDA, SCL
+*/
+
+/* RS232 Serial
+Serial pc(p13, p14); // dev RX, TX (serial)
+*/
-Serial pc(p13, p14); // TX, RX
+/* LCD level and compass
+uLCD_4DGL lcd(p28, p27, p30); // dev RX, TX, reset. Use +5 (Vu)
+int ax, ay;
+double theta;
+*/
+
+/* Control high current device with xtor/relay
+DigitalOut binaryCtrl(p25); // control signal
+DigitalOut myled(LED1);
*/
-/* PART 8
+/* With Power MOSFET for pwm */
+PwmOut pwmCtrl(p25); // digital control with pwm
+PwmOut myled(LED1);
+
+/* Servo movement
+Servo myservo(p21); // dev yellow. Use Vu
+*/
+
+/* DC Motor control with H-bridge
+Motor m(p23, p11, p12); // pwm control, fwd, rev
+*/
+
+/* Stepper motor
+// pwmA and B set to 3.3V (no speed control, only directional)
+// A poles on left side (top view). A1 pole to A02, etc.
+StepperMotor_X27168 smotor(p25, p26, p23, p22); // bfwd, brev, afwd, arev
+*/
+
+/* MEMS microphone
BusOut myleds(LED1,LED2,LED3,LED4);
class microphone
{
-public :
- microphone(PinName pin);
- float read();
- operator float ();
-private :
- AnalogIn _pin;
+ public :
+ microphone(PinName pin);
+ float read();
+ operator float ();
+ private :
+ AnalogIn _pin;
};
+
microphone::microphone (PinName pin):
_pin(pin)
{
}
+
float microphone::read()
{
return _pin.read();
}
+
inline microphone::operator float ()
{
return _pin.read();
@@ -54,22 +95,12 @@
microphone mymicrophone(p16);
*/
-/* PART 4
-uLCD_4DGL lcd(p13,p14,p12);
-int ax, ay;
+/* SPI bus: microSD card
+SDFileSystem sd(p5, p6, p7, p8, "sd"); mosi -> DI, miso <- DO, slck -> sclck, CS -> CS
+// CD on device could be used in PullUp mode to check if a card is present
*/
-/* PART 6 */
-DigitalOut myled(LED1);
-DigitalOut Ctrl(p25); // comment this out for digital, comment below
-//PwmOut Ctrl(p25);
-
-
-/*PART 9, SD CARD
-SDFileSystem sd(p5, p6, p7, p8, "sd");
-*/
-
-/* PART 9B, SOUND
+/* Speaker
float note[16]= {329.628,329.628,329.628,349.228,391.995,391.995,391.995,349.228,
329.628,349.228,391.995,349.228,329.628,293.665,329.628,349.228
};
@@ -81,16 +112,15 @@
int main() {
- /* PART 1
+ /* Sine wave output pin
const double pi = 3.141592653589793238462;
- const double amplitude = 1.0f;
- const double offset = 65535/2; // 0xFFFF/2
+ const double amplitude = 1.0f; // 3.3 V
+ const double offset = 65535/2; // 0xFFFF/2, half the full 3.3V
double rads = 0.0;
uint16_t sample = 0;
*/
- /* PART 4, BUBBLE, PART 3
- LSM9DS1 imu(p9, p10, 0xD6, 0x3C);
+ /* RS232 Serial, LCD bubble level
imu.begin();
if (!imu.begin()) {
pc.printf("Failed to communicate with imu9DS1.\n");
@@ -98,13 +128,45 @@
imu.calibrate();
*/
+ /* Servo movement
+ // servo adjusts until desired angle is sensed, determind by the pulse width
+ // width varies from 1 - 2 ms, 1ms - 0 deg, 1.5 - 90 deg, 2 ms - 180
+ // servo updates below every 20ms
+ for(float p=0; p<1.0; p += 0.1) {
+ myservo = p;
+ wait(0.2);
+ }
+ */
+
+ /* DC Motor control with H-bridge
+ for (float s= -1.0; s < 1.0 ; s += 0.01) {
+ m.speed(s);
+ wait(0.02);
+ }
+ */
+
+ /* Stepper motor
+ smotor.step_position(180);
+ wait(0.5);
+
+ smotor.step_position(100);
+ wait(0.5);
+
+ smotor.angle_position(270);
+ wait(0.5);
+
+ smotor.step_position(0);
+ wait(0.5);
+ */
- /* PART 9, SD CARD
+ /* SPI bus: microSD card
mkdir("/sd/mydir", 0777);
+
FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
+
fprintf(fp, "Hello SD file World!");
fclose(fp);
@@ -118,130 +180,84 @@
}
*/
- /* PART 9, SPEAKER
- // setup instance of new SongPlayer class, mySpeaker using pin 26
- // the pin must be a PWM output pin
- SongPlayer mySpeaker(p26);
+ /* Speaker
+ SongPlayer mySpeaker(p26); // pwm pin
// Start song and return once playing starts
- mySpeaker.PlaySong(note,duration);
- double theta;
+ mySpeaker.PlaySong(note, duration);
*/
while (1) {
- /* PART 1
-
- // analog out
+ /* Sine wave output pin
for (int i = 0; i < 720; i++) {
rads = (pi * i) / 180.0f;
sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) + offset);
aout.write_u16(sample);
}
+ */
- // analog in
- led1 = (ain > 0.2f) ? 1 : 0;
+ /* IR distance sensor
+ led1 = (ain > 0.2f) ? 1 : 0; // more LEDs with increasing distance
led2 = (ain > 0.4f) ? 1 : 0;
led3 = (ain > 0.6f) ? 1 : 0;
led4 = (ain > 0.8f) ? 1 : 0;
wait(.01);
*/
- /* PART 8
- //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display
- myleds = int(abs((mymicrophone - (0.67/3.3)))*500.0);
- //Use an 8kHz audio sample rate (phone quality audio);
- wait(1.0/8000.0);
- */
-
- /* PART 3
-
-
+ /* RS232 Serial
imu.readAccel();
imu.readMag();
imu.readGyro();
-
pc.printf("gyro: %d %d %d\n\r", imu.gx, imu.gy, imu.gz);
pc.printf("accel: %d %d %d\n\r", imu.ax, imu.ay, imu.az);
pc.printf("mag: %d %d %d\n\n\r", imu.mx, imu.my, imu.mz);
*/
- /* PART 4, BUBBLE/COMPASS
-
+ /* LCD level and compass
imu.readAccel();
imu.readMag();
imu.readGyro();
+
+ // level
- lcd.filled_circle(64 + ax / 250, 64 + ay/ 250 , 8, BLACK);
- ax = imu.ax, ay = imu.ay;
- lcd.filled_circle(64 + ax / 250, 64 + ay/ 250 , 8, WHITE);
- lcd.circle(64, 64, 10, WHITE);
+ lcd.filled_circle(64 + ax / 250, 64 + ay/ 250 , 8, BLACK); // write over past circle
+ ax = imu.ax, ay = imu.ay; // get data
+ lcd.filled_circle(64 + ax / 250, 64 + ay/ 250 , 8, WHITE); // draw new 'reading'
+ lcd.circle(64, 64, 10, WHITE); // redraw the center (constant)
- // compass, PART 4 extra credit
+ // compass
- lcd.line(64,64,64+60*cos(theta), 64+60*sin(theta), BLACK);
- theta = atan2((double)imu.mx, (double)imu.my);
- lcd.line(64,64,64+60*cos(theta), 64+60*sin(theta), GREEN);
+ lcd.line(64,64,64+60*cos(theta), 64+60*sin(theta), BLACK); // write over past heading
+ theta = atan2((double)imu.mx, (double)imu.my); // get data
+ lcd.line(64,64,64+60*cos(theta), 64+60*sin(theta), GREEN); // draw new heading
lcd.locate(0,1);
- lcd.printf("%f",180 * theta / 3.14159);
-
+ lcd.printf("%f",180 * theta / 3.14159); // write degree heading at the top left
*/
- /* PART 6 */
-
- // using DigitalOut
- Ctrl = 1;
+ /* Control high current device with xtor/relay
+ binaryCtrl = 1; // slow out because of relay
myled = 1;
wait(.2);
- Ctrl = 0;
+ binaryCtrl = 0;
myled = 0;
wait(.2);
+ */
- // using PwmOut
- /*
- myled = 1;
+ /* With Power MOSFET for pwm */
+ myled = 1.0f;
for (int i = 0; i < 5; i++) {
- Ctrl = i*0.2f;
- wait(.1);
- myled = 0;
- wait(.1);
+ pwmCtrl = i*0.2f;
+ myled = pwmCtrl;
+ wait(.2);
}
+
+ /* MEMS microphone
+ //read in, subtract 0.67 DC bias, take absolute value, and scale up from .1Vpp to 15 (0xFFFF) for builtin LEDs
+ myleds = (int) abs( mymicrophone - 0.67/3.3 ) * 500.0;
+ //Use an 8kHz audio sample rate (phone quality audio);
+ wait(1.0/8000.0);
*/
}
-}
-
-
-
-/*
- ,
- / \
- { }
- p !
- ; : ;
- | : |
- | : |
- l ; l
- l ; l
- I ; I
- I ; I
- I ; I
- I ; I
- d | b
- H | H
- H | H
- H I H
- ,;, H I H ,;,
- ;H@H; ;_H_;, ;H@H;
- `\Y/d_,;|4H@HK|;,_b\Y/'
- '\;MMMMM$@@@$MMMMM;/'
- "~~~*;!8@8!;*~~~"
- ;888;
- ;888;
- ;888;
- ;888;
- d8@8b
- O8@8O
- T808T
- `~`
-*/
\ No newline at end of file
+}
\ No newline at end of file
