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

Revision:
1:8b9c0e69c6c9
Parent:
0:6999a083fb46
Child:
2:710e07252f28
--- a/main.cpp	Thu Jun 14 12:18:18 2012 +0000
+++ b/main.cpp	Mon Apr 05 16:07:12 2021 +0000
@@ -1,62 +1,101 @@
-/*
-############################################
-##           sMotor v0.1 Test Program     ##
-##          created by Samuel Matildes    ##
-############################################
-        ---- sam.naeec@gmail.com -----
-This library was made for 4-Phase Stepper Motors
-I don't take any resposability for the damage caused to your equipment.
-
-*/
-
 #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); 
 
-Serial pc(USBTX, USBRX);
-sMotor motor(p9, p10, p11, p12); // creates new stepper motor: IN1, IN2, IN3, IN4
+int step_speed = 1100; // set default motor speed
+int dir = 0; //0 is CW, 1 is CCW
 
-int step_speed = 1200 ; // set default motor speed
-int numstep = 512 ; // defines full turn of 360 degree
-//you might want to calibrate this value according to your motor
-
+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("4 Phase Stepper Motor v0.1 - Test Program\r\n");
-    printf("developed by Samuel Matildes\r\n");
+    printf("Cat Tower - Test Program\r\n");
     printf("\n\r");
 
     // Screen Menu
     printf("Default Speed: %d\n\r",step_speed);
-    printf("1- 360 degree clockwise step\n\r");
-    printf("2- 360 degree anticlockwise step\n\r");
-    printf("3- 180 degree clockwise step\n\r");
-    printf("4- 180 degree anticlockwise step\n\r");
-    printf("5- Change Speed\n\r");
+    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) {
-
-        if (pc.readable()) { // checks for serial
-
-            if (pc.getc()=='1')
-                motor.step(numstep,0,step_speed); // number of steps, direction, speed
-
-            if (pc.getc()=='2')
-                motor.step(numstep,1,step_speed);
-
-            if (pc.getc()=='3')
-                motor.step(numstep/2,0,step_speed);
-
-            if (pc.getc()=='4')
-                motor.step(numstep/2,1,step_speed);
-
-            if (pc.getc()=='5') {
-                printf("Current Speed: %d\n\r", step_speed);
-                printf("New speed: \n\r");
-                pc.scanf("%d",&step_speed); // sets new speed
+        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
         }
     }
 }
\ No newline at end of file