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.
main.cpp
00001 #include "mbed.h" 00002 #include "Servo.h" 00003 00004 I2CSlave slave(p9, p10); //Configure I2C slave 00005 PwmOut steering (p21);// Define PWM Output to wheels servo 00006 Servo motor (p22); // Define PWM Output to the ESC 00007 DigitalOut forward(LED1); 00008 DigitalOut back(LED2); 00009 DigitalOut stop(LED3); 00010 DigitalOut led1(p13); // Indicator Light for center turn 00011 DigitalOut led2(p15); // Indicator Light for left turn 00012 DigitalOut led3(p17);// Indicator Light for right turn 00013 char switch_word ; //word we will send 00014 char recd_val; //value received from master 00015 00016 int main() //start of main programme 00017 { 00018 slave.address(0x52); // The slave is addressed 00019 while(1) 00020 { 00021 led1=0; 00022 led2=0; 00023 led3=0; 00024 forward=0; 00025 back=0; 00026 stop=0; 00027 switch_word=0xa0; //set up a recognisable output pattern 00028 slave.write(switch_word); //load up word to send 00029 /*test for I2C, and act accordingly*/ 00030 int i = slave.receive(); 00031 if (i == 3) //slave is addressed, Master will write 00032 { 00033 recd_val= slave.read(); 00034 recd_val=recd_val&0x0F; // AND out unwatnted LSB bits 00035 /*Locomotion and Sterring Control based on received Value*/ 00036 if (recd_val==1) 00037 { 00038 led1=1; 00039 forward=1; 00040 steering.pulsewidth (0.0015); //centre 00041 wait(0.2); 00042 motor.write(0.3); //full forward 00043 wait(1); 00044 } 00045 /* 00046 if (recd_val==2) 00047 { 00048 led3=1; 00049 forward=1; 00050 steering.pulsewidth (0.0009);//right 00051 wait(0.2); 00052 motor.write(0.3); //full forward 00053 wait(1); 00054 } 00055 */ 00056 if (recd_val==3) 00057 { 00058 led3=1; 00059 forward=1; 00060 steering.pulsewidth (0.0009);//right 00061 wait(0.2); 00062 motor.write(0.3);//forward 00063 wait(1); 00064 } 00065 /* 00066 if(recd_val==4) 00067 { 00068 led3=1; 00069 back=1; 00070 steering.pulsewidth (0.0009);//right 00071 wait(0.2); 00072 motor.write(0.6); //full backwards 00073 wait(1); 00074 }*/ 00075 if (recd_val==5) 00076 { 00077 led1=1; 00078 back=1; 00079 steering.pulsewidth (0.0015);//center 00080 motor.write(0.6); //full backwards 00081 wait(1); 00082 } 00083 /* 00084 if(recd_val==6) 00085 { 00086 led2=1; 00087 back=1; 00088 steering.pulsewidth (0.0021);//LEFT 00089 wait(0.2); 00090 motor.write(0.6); //full backwards 00091 wait(1); 00092 } 00093 */ 00094 if (recd_val==7) 00095 { 00096 led2=1; 00097 forward=1; 00098 steering.pulsewidth (0.0021);//LEFT 00099 wait(0.2); 00100 motor.write(0.3); //full forward 00101 wait(1); 00102 led1=0; 00103 led2=0; 00104 led3=0; 00105 } 00106 /* 00107 if (recd_val==8) 00108 { 00109 led2=1; 00110 forward=1; 00111 steering.pulsewidth (0.0021);//LEFT 00112 wait(0.2); 00113 motor.write(0.3); //full FORWARD 00114 wait(1); 00115 } 00116 */ 00117 if (recd_val==9) 00118 { 00119 stop=1; 00120 steering.pulsewidth(0.0015);//stop 00121 wait(0.2); 00122 motor.write(0.5);//stop 00123 wait(1); 00124 } 00125 if (recd_val==0) 00126 { 00127 stop=1; 00128 steering.pulsewidth(0.0015);//stop 00129 wait(0.2); 00130 motor.write(0.5);//stop 00131 wait(1); 00132 } 00133 } 00134 } 00135 }
Generated on Tue Jul 19 2022 00:55:00 by
1.7.2