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 ADS1115 StepperMotor SRF05 TPA81new
main.cpp
00001 #include "mbed.h" 00002 #include "TPA81new.h" 00003 #include "SRF05.h" 00004 #include "Uvtron.h" 00005 #include "Stepper.h" 00006 #include "AMG8833.h" 00007 #include "Adafruit_ADS1015.h" 00008 00009 // Konstanta 00010 #define WAIT_TIME 0.02 00011 // infrared 00012 #define a_depan 5000000 00013 #define b_depan 1.229 00014 #define a_ads 500000 00015 #define b_ads 1.184 00016 #define I2C_ADDR 0xC0 00017 00018 Serial pc(USBTX,USBRX, 115200); 00019 00020 // AMG 00021 Adafruit_AMG88xx amg(PB_3, PB_10); // sda, scl 00022 float pixels[AMG88xx_PIXEL_ARRAY_SIZE]; 00023 00024 DigitalOut relay(PB_2); 00025 Uvtron uv(PC_12); 00026 DigitalIn sound(PA_9); 00027 AnalogIn line(PC_1); 00028 00029 // Infrared 00030 // atas 00031 AnalogIn ira1(PC_5); 00032 AnalogIn ira2(PA_5); 00033 AnalogIn ira3(PC_2); 00034 AnalogIn ira4(PC_3); 00035 AnalogIn ira5(PC_4); 00036 AnalogIn ira6(PB_1); 00037 AnalogIn ira7(PA_7); 00038 AnalogIn ira8(PA_6); 00039 // bawah 00040 AnalogIn irb1(PA_0); 00041 AnalogIn irb2(PA_1); 00042 AnalogIn irb3(PA_4); 00043 AnalogIn irb4(PB_0); 00044 // ADC 00045 I2C ads_i2c(PB_9, PB_8); // sda, scl 00046 Adafruit_ADS1015 ads(&ads_i2c); 00047 00048 // kompas 00049 I2C cmps12(PB_4, PA_8); //sda, scl 00050 00051 // stepper(PinName _en, PinName ms1, PinName ms2, PinName ms3, PinName _stepPin, PinName dir); 00052 stepper s(PC_8, PC_6, PA_12, PA_11, PB_12, PC_7); 00053 00054 // sound 00055 //DigitalIn sound(PA_9); 00056 00057 DigitalOut led_api(PB_6); 00058 DigitalOut led_sound(PH_1); 00059 00060 float call_depan(uint16_t x) 00061 { 00062 float side_distance = a_depan/pow(x,b_depan)+1; 00063 return side_distance; 00064 } 00065 00066 float call_ads(uint16_t ir) { 00067 float voltage = ir * 6.2676 * 10e-5; // ubah ke mV 00068 uint16_t x = (voltage/3.3f) * 0xFFFF; // ubah ke uint16_t sesuai max nucleo 00069 float dist = a_ads/pow(x, b_ads)+1; 00070 return dist; 00071 } 00072 00073 int main() 00074 { 00075 uint16_t ads_raw[4] = {}; 00076 int status = amg.begin(AMG88xx_ADDRESS); 00077 amg.setMovingAverageMode(1); 00078 if (!status) { 00079 pc.printf("Could not find a valid AMG88xx sensor, check wiring! \n"); 00080 while (1); 00081 } 00082 relay = 1; 00083 wait_ms(100); 00084 while (sound.read() == 1){ 00085 wait_ms(100); 00086 }; 00087 pc.printf("Sound detected\n"); 00088 while (1) { 00089 led_sound = 1; 00090 // IR 00091 for (uint8_t i=0; i < 4; i++) { 00092 ads_raw[i] = ads.readADC_SingleEnded(i); 00093 wait_ms(10); 00094 }; 00095 uint16_t raw_a1 = ira1.read_u16(); 00096 uint16_t raw_a2 = ira2.read_u16(); 00097 uint16_t raw_a3 = ira3.read_u16(); 00098 uint16_t raw_a4 = ira4.read_u16(); 00099 uint16_t raw_a5 = ira5.read_u16(); 00100 uint16_t raw_a6 = ira6.read_u16(); 00101 uint16_t raw_a7 = ira7.read_u16(); 00102 uint16_t raw_a8 = ira8.read_u16(); 00103 00104 uint16_t raw_b1 = irb1.read_u16(); 00105 uint16_t raw_b2 = irb2.read_u16(); 00106 uint16_t raw_b3 = irb3.read_u16(); 00107 uint16_t raw_b4 = irb4.read_u16(); 00108 00109 float dist_a1 = call_depan(raw_a1); 00110 float dist_a2 = call_depan(raw_a2); 00111 float dist_a3 = call_depan(raw_a3); 00112 float dist_a4 = call_depan(raw_a4); 00113 float dist_a5 = call_depan(raw_a5); 00114 float dist_a6 = call_depan(raw_a6); 00115 float dist_a7 = call_depan(raw_a7); 00116 float dist_a8 = call_depan(raw_a8); 00117 float dist_a9 = call_ads(ads_raw[1]); 00118 float dist_a10 = call_ads(ads_raw[3]); 00119 float dist_a11 = call_ads(ads_raw[2]); 00120 float dist_a12 = call_ads(ads_raw[0]); 00121 float dist_b1 = call_depan(raw_b1); 00122 float dist_b2 = call_depan(raw_b2); 00123 float dist_b3 = call_depan(raw_b3); 00124 float dist_b4 = call_depan(raw_b4); 00125 00126 pc.printf("ir atas\n"); 00127 pc.printf("%.2f %.2f %.2f %.2f\n", dist_a9, dist_a10, dist_a11, dist_a12); 00128 pc.printf("%.2f %.2f\n", dist_a3, dist_a1); 00129 pc.printf("%.2f %.2f\n", dist_a4, dist_a6); 00130 pc.printf("%.2f %.2f %.2f %.2f\n", dist_a2, dist_a8, dist_a7, dist_a5); 00131 00132 pc.printf("ir bawah\n"); 00133 pc.printf(" %.2f\n", dist_b1); 00134 pc.printf("%.2f %.2f\n", dist_b2, dist_b3); 00135 pc.printf(" %.2f\n", dist_b4); 00136 00137 // baca amg 00138 float therm_temp = amg.readThermistor(); 00139 amg.readPixels(pixels); 00140 pc.printf("%.2f\n", therm_temp); 00141 float max_temp = pixels[0]; 00142 int max_i = 0; 00143 float sum_temp = 0; 00144 for (int i = 1; i <= AMG88xx_PIXEL_ARRAY_SIZE; i++) { 00145 // pc.printf("%.2f ", pixels[i-1]); 00146 // if (i % 8 == 0) pc.printf("\n"); 00147 if (pixels[i-1] > max_temp) { 00148 max_temp = pixels[i-1]; 00149 max_i = i; 00150 } 00151 sum_temp += pixels[i-1]; 00152 } 00153 00154 // uvtron 00155 uv.Read(); 00156 int read = uv.Flag; 00157 if (read) pc.printf("FIRE DETECTED\n"); 00158 else pc.printf("NOT DETECTED\n"); 00159 00160 pc.printf("temp max= %.2f at %d\n", max_temp, max_i); 00161 pc.printf("temp rata2 = %.2f\n", sum_temp/AMG88xx_PIXEL_ARRAY_SIZE); 00162 00163 // baca kompas 00164 char data[2] = {0x1, 0xFF}; 00165 int nack; 00166 nack = cmps12.write(I2C_ADDR, data, 1); 00167 nack = cmps12.read(I2C_ADDR, data+1, 1); 00168 pc.printf("Bearing kompas = %.2f\n", (float) data[1]/255*360); 00169 00170 // stepper 00171 pc.printf("Stepper \n"); 00172 for (int i = 0; i < 50; i++) { 00173 s.step(1, 1, 1/WAIT_TIME); // CCW 00174 } 00175 00176 for (int i = 0; i < 50; i++) { 00177 s.step(1, 0, 1/WAIT_TIME); // CW 00178 } 00179 00180 // pompa 00181 relay = 0; // active low 00182 pc.printf("Pompa on!\n"); 00183 wait(1); 00184 relay = 1; 00185 pc.printf("Pompa off\n"); 00186 wait(1); 00187 00188 wait_ms(200); 00189 00190 pc.printf("\033[2J\033[H"); 00191 } 00192 }
Generated on Wed Jul 13 2022 00:38:51 by
1.7.2