Skip to content

Instantly share code, notes, and snippets.

@yue82
Created October 22, 2016 20:01
Show Gist options
  • Select an option

  • Save yue82/9bf0a0b0b1fb1cded1f851e645b84137 to your computer and use it in GitHub Desktop.

Select an option

Save yue82/9bf0a0b0b1fb1cded1f851e645b84137 to your computer and use it in GitHub Desktop.
#include "mbed.h"
AnalogIn sensor[] = {dp19, dp20, dp21};
BusOut mdA(dp7, dp6);
BusOut mdB(dp9, dp8);
Serial pc(USBTX, USBRX);
int sensor2bin(float sensor_var, float threshold)
{
if(sensor_var > threshold) {
return 1;
} else{
return 0;
}
}
int main()
{
int s[3] = {0, 0, 0};
float ts = 0.3;
float temp = 0.0;
mdA = 0b11;
mdB = 0b11;
while(1) {
for (int i = 0; i < 3; i++){
temp = sensor[i].read();
s[i] = sensor2bin(temp, ts);
pc.printf("%d: %d %f\r\n", i, s[i], temp);
}
pc.printf("-----\r\n");
if(s[0]==0 && s[1]==1 && s[2]==0){
mdA = 0b10;
mdB = 0b10;
pc.printf("straight\r\n");
} else if(s[0]==1 && s[1]==1 && s[2]==1){
mdA = 0b11;
mdB = 0b11;
pc.printf("stop\r\n");
} else if(s[0]==1){
mdA = 0b10;
mdB = 0b00;
pc.printf("left\r\n");
} else if(s[2]==1){
mdA = 0b00;
mdB = 0b10;
pc.printf("right\r\n");
}
pc.printf("-----\r\n");
//wait(1.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment