Lab 1 Task 1

Dependencies:   mbed m3pi

Fork of 3pi_Lab1_Task2 by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 3pi Lab 1 Example 1
00002 
00003 (c) Dr Craig A. Evans, University of Leeds
00004 
00005 June 2017
00006 
00007 */
00008 
00009 #include "mbed.h"
00010 #include "m3pi.h"
00011 
00012 // API objects
00013 m3pi robot;
00014 DigitalIn button_enter(p24);
00015 AnalogIn pot_S(p20);
00016 BusOut leds(LED4,LED3,LED2,LED1);
00017 
00018 // Global Variables
00019 const char g_song[] = "!T240 L8 a gafaeada c+adaeafa >aa>bac#ada c#adaeaf4";
00020 float speed = 0.0;
00021 
00022 // Function Prototypes
00023 void init();
00024 
00025 // Main Function
00026 int main()
00027 {
00028     // call initialisation function - must do this first
00029     init();
00030 
00031     // move cursor to position (0,0) - top-left
00032     robot.lcd_goto_xy(0,0);
00033     robot.lcd_print("Lab 1",5); // 5 is number of characters in message (max 8)
00034     robot.lcd_goto_xy(0,1);
00035     robot.lcd_print("Task 1",6);
00036 
00037     // play song then leave a small delay for it to finish playing
00038     robot.play_music(g_song,sizeof(g_song));
00039     wait(5.0);
00040 
00041     // loop through 0 to 15, displaying the value in binary on the leds
00042     for (int value = 0; value < 16; value++) {
00043         leds = value;
00044         wait(0.1);
00045     }
00046     leds = 0;  // turn them all off
00047 
00048     // keep looping until button has been pressed
00049     while (button_enter.read() == 1) {
00050 
00051     }
00052 
00053     // main loop - this runs forever
00054     while(1) {
00055 
00056         // clear the screen
00057         robot.lcd_clear();
00058         // array to store message in
00059         char buffer[8];
00060 
00061         // check if enter button is pressed
00062         if (button_enter.read() == 0) { 
00063             speed = pot_S.read();  // if so, read speed potentiometer and store in variable
00064             sprintf(buffer,"S=%.3f",speed); // create message and store in array .3 means three decimal places
00065         }
00066         
00067         // go to bottom line of LCD
00068         robot.lcd_goto_xy(0,1);
00069         // print the message
00070         robot.lcd_print(buffer,7);
00071 
00072         // small delay
00073         wait(0.1);
00074 
00075     }
00076 }
00077 
00078 // Functions
00079 void init()
00080 {
00081     // initialise the robot
00082     robot.init();
00083 
00084     // turn on the pull-up resistor on the button
00085     // the button will read 1 when not pressed and 0 when pressed
00086     button_enter.mode(PullUp);
00087 
00088 }