This version has sonar added to the rest of the code, but the sonar is not currently working. I suspect that the interrupts from the bluetooth are interfering with the sonar working
Dependencies: mbed sMotor HC_SR04_Ultrasonic_Library
main.cpp
- Committer:
- bseleb3
- Date:
- 2021-04-05
- Revision:
- 1:8b9c0e69c6c9
- Parent:
- 0:6999a083fb46
- Child:
- 2:710e07252f28
File content as of revision 1:8b9c0e69c6c9:
#include "mbed.h" #include "sMotor.h" Serial pc(USBTX, USBRX); Serial Blue(p28,p27); sMotor motor(p9, p10, p11, p12); // creates new stepper motor: IN1, IN2, IN3, IN4 DigitalOut laser(p20); int step_speed = 1100; // set default motor speed int dir = 0; //0 is CW, 1 is CCW volatile int C = 30; volatile int numstep; // defines basic turn distance -- 256 steps == 360 degrees volatile bool stationary = 1; volatile bool isrand = 0; volatile bool manual = 0; volatile int bnum = 0; volatile int bhit; volatile int power = 1; //state used to remember previous characters read in a button message enum statetype {start = 0, got_exclm, got_B, got_num, got_hit}; statetype state = start; //Interrupt routine to parse message with one new character per serial RX interrupt void parse_message() { switch (state) { case start: if (Blue.getc()=='!') state = got_exclm; else state = start; break; case got_exclm: if (Blue.getc() == 'B') state = got_B; else state = start; break; case got_B: bnum = Blue.getc(); state = got_num; break; case got_num: bhit = Blue.getc(); state = got_hit; break; case got_hit: if (Blue.getc() == char(~('!' + ' B' + bnum + bhit))) { switch (bnum) { case '1': //number button 1 isrand = 0; stationary = 1; break; case '2': //number button 2 stationary = 0; isrand = 0; numstep = C; break; case '3': //number button 3 stationary = 0; isrand = 1; break; case '4': //number button 4 power = !power; } } default: Blue.getc(); state = start; } } int main() { //Credits printf("Cat Tower - Test Program\r\n"); printf("\n\r"); // Screen Menu printf("Default Speed: %d\n\r",step_speed); printf("Use Adafruit Bluefruit buttons to select mode"); // attach interrupt function for each new Bluetooth serial character Blue.attach(&parse_message,Serial::RxIrq); //int count = 0; //int temp = 1; while (1) { laser = power; if (stationary != 1) { if (isrand == 1) { numstep = rand() % C+1; //if (count + temp*numstep < 0) { // numstep = count; //} else if (count + temp*numstep > C) { // numstep = C - count; //} //count = count + temp*numstep; //temp = -temp; } motor.step(numstep, dir, step_speed); // number of steps, direction, speed dir = 1 - dir; //swap directions } } }