Use IQS62X sensor and move motor by detected angle

Dependencies:   DRV8830 IQS62x IQSDisplayTerminal UIT_ACM1602NI mbed

Fork of Nucleo_ACM1602_I2C_DC by Thinkbed

Revision:
3:6474ab60854e
Parent:
2:ea066749e515
Child:
4:83f7df775d46
--- a/main.cpp	Wed Jun 07 15:01:11 2017 +0000
+++ b/main.cpp	Mon Sep 04 15:28:40 2017 +0000
@@ -5,16 +5,23 @@
 //------------------------------------------------------------
 
 #include "ACM1602NI.hpp"
+#include "DRV8830.h"
+
 //Cycle 
-#define ON_DURATION  20     //On time   [*100ms]
-#define SWITCH_PERIOD 200   //Cycle time[*100ms]
-#define TOTAL_TIMES 10000  //total times n
-#define INITIAL_DELAY 10   //Initial delay for cycle
+#define ON_DURATION    5    //On time   [*100ms]
+#define SWITCH_PERIOD 30   //Cycle time[*100ms]
+#define WAIT_DELAY 5      //Delay time [*100ms]
+#define TOTAL_TIMES 30000  //total times n
+
+
+const float spd_table[] = {1.0,0.7,0.5};
+static int sp_index=0;
 
 
 using namespace Mikami;
 
 Acm1602Ni lcd_;                               // Default, OK
+
 //Acm1602Ni lcd_(D14, D15);                     // OK
 //Acm1602Ni lcd_(D14, D15, 200000);             // OK
 //Acm1602Ni lcd_(D14, D15, 200000, true, true); // OK
@@ -23,15 +30,20 @@
 //Acm1602Ni lcd_(PB_4, PA_8);                   // OK 
 
 Ticker timer_;
-DigitalOut Relay1(D2);
+I2C     i2c(D14, D15);
+DRV8830 motor(i2c, DRV8830ADDR_NN);
+//DigitalOut Relay1(D2);
 InterruptIn button1(USER_BUTTON);
+static float motor_speed;
 
+ 
 // Display elapsed time in minutes and seconds
 void TimerIsr()
 {
+    
+    //For LED Time-Sec display
     static int k = 0;
     static char ctext[4]="---";
-    
     div_t d_Cycle = div (k, SWITCH_PERIOD);
     
     //for Current time
@@ -45,18 +57,36 @@
     int tf_min = df_sec.quot;
     div_t df_min = div(tf_min,60);   //1min=60s
     int tf_hr  = df_min.quot;
-
+    
+    
+    //Motor activation
     
     
-    if(d_Cycle.rem ==INITIAL_DELAY)
+    if(d_Cycle.rem ==WAIT_DELAY)
+        {
+          motor_speed=spd_table[sp_index];
+          strcpy(ctext," CW");
+         //ctext="CW";
+        }
+        
+    else if (d_Cycle.rem == (WAIT_DELAY+ON_DURATION) )
         {
-         Relay1=1;
-         strcpy(ctext," ON");
-         //ctext="ON!";
+          //wait_ms(20);
+          motor_speed=0;
+          strcpy(ctext,"OFF");
         }
-    else if (d_Cycle.rem == (INITIAL_DELAY+ON_DURATION) )
+        
+    else if (d_Cycle.rem == (2*WAIT_DELAY+ON_DURATION) )
         {
-          Relay1=0;
+          //wait_ms(20);
+          motor_speed=-spd_table[sp_index];
+          strcpy(ctext,"CCW");
+        }
+        
+    else if (d_Cycle.rem == (2*WAIT_DELAY+ON_DURATION*2) )
+        {
+          //wait_ms(20);
+          motor_speed=0;
           strcpy(ctext,"OFF");
         }
         
@@ -65,6 +95,7 @@
     {
         timer_.detach();    
     }
+    
 
                              
 /*
@@ -75,9 +106,12 @@
 
     //1 Row
     //lcd_.WriteStringXY("#",0,0);
+    
     lcd_.WriteValueXY("%s", ctext, 0,0);
     lcd_.WriteValue("% 5d/", d_Cycle.quot);
     lcd_.WriteValue("%0d",TOTAL_TIMES);
+    lcd_.WriteValue("V%0d",sp_index);
+    
     
     //2 Row
     lcd_.WriteValueXY("%03dh", t_hr, 0, 1);
@@ -85,7 +119,7 @@
     lcd_.WriteValue("%03ds/",  d_sec.rem);
     lcd_.WriteValue("%03dh", tf_hr);
     lcd_.WriteValue("%02dm",   df_min.rem);
-
+    
     
     k++;        
 }
@@ -95,6 +129,8 @@
 void flip() {
     static bool b = false;
     
+    
+    
     if(b==false)
     {
         timer_.attach(&TimerIsr, 0.1);
@@ -103,28 +139,40 @@
     else
     {
         timer_.detach();
-        Relay1=0;
+        //Relay1=0;
+        sp_index++;
+        if (sp_index == 3)
+        {
+            sp_index = 0;    
+        }
     }
-    
     b=!b;
     
-    
-    
 }
 
 
 int main()
 {
+    //motor.speed(0);
+     //  Check error and reset
+      
     
+
     //LCD_cont=0;
-    if (lcd_.IsConnected()) printf("\r\nConnected");
-    else                    printf("\r\nDisconnected");
-
+    //if (lcd_.IsConnected()) printf("\r\nConnected");
+    //else                    printf("\r\nDisconnected");
 
     TimerIsr();
     //timer_.attach(&TimerIsr, 0.1);
     button1.fall(&flip);
 
-    while (true) {}
+    bool status = motor.status();
+    if (status & DRV8830_F_FAULT){
+        motor.reset();
+     } 
+    
+    while (true) {
+            motor.speed(motor_speed);
+            }
 }