lcd 20x04 dual stepper control with ascelerometer and tsi function by facuclop

Dependencies:   MMA8451Q TSI TextLCD mbed-rtos mbed sMotor

Fork of Stepper_4 by Nuno Sarmento

Revision:
1:e462e11e7ac5
Parent:
0:6999a083fb46
--- a/main.cpp	Thu Jun 14 12:18:18 2012 +0000
+++ b/main.cpp	Tue Aug 05 23:28:22 2014 +0000
@@ -1,62 +1,101 @@
 /*
 ############################################
 ##           sMotor v0.1 Test Program     ##
-##          created by Samuel Matildes    ##
+##                by Facuclop             ##
 ############################################
-        ---- 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 "TextLCD.h"
 #include "sMotor.h"
-
+#include "TSISensor.h"
+#include "MMA8451Q.h"
+#include "rtos.h"
 
-Serial pc(USBTX, USBRX);
-sMotor motor(p9, p10, p11, p12); // creates new stepper motor: IN1, IN2, IN3, IN4
-
-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
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+/////////////////////////////  io   ///////////////////////////////////////////////////////
+//Serial pc(USBTX, USBRX);
+TextLCD lcd(D5, D4, D0, D1, D2, D3, TextLCD::LCD20x4);
+sMotor motor(A1, A2, A0, A3); // creates new stepper motor: IN1, IN2, IN3, IN4
+DigitalIn boton(SW1);
+DigitalIn boton2(PTC12);
+AnalogIn light(PTE22);
+PwmOut led1(LED1);
+PwmOut led2(LED2);
+PwmOut led3(LED3);
+MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
+TSISensor tsi;
+/////////////////////////////////////////////////////////////////////////////////////////////    
+int step_speed = 1300 ; // set default motor speed 1200 es la maxima mientras mas chico mas rapido
+int numstep = 50 ; // defines full turn of 360 degree
+int step_speed2 = 1200 ;
+int selector;
+//***********************************************************************************************************
 
-
-int main() {
-
-    //Credits
-    printf("4 Phase Stepper Motor v0.1 - Test Program\r\n");
-    printf("developed by Samuel Matildes\r\n");
-    printf("\n\r");
+int main()
+ {
+float tp=0;
+float ejex;
+float ejey;
+float pipo;
+float luz;
+luz=light;
+////////////////////////////////////////
+lcd.locate(0,0);
+lcd.printf("__Facuclop Program__");
+////////////////////////////////////////
+    
+////************************************************************************************
 
-    // 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");
-
-    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
-            }
-        }
-    }
+/*switch(selector) {
+                case 1:if(pipo>=0.6){motor.step(numstep/2,0,step_speed);}break;
+                case 2:if(pipo<=0.4){motor.step(numstep/2,1,step_speed);}break;
+                  }*/
+                  
+                  
+while (1) {
+    ejex=acc.getAccX(); //esto hay que METERLO en el while
+    ejey=acc.getAccY(); 
+    pipo=tsi.readPercentage();// meto la funcion en pipo
+tp  = (tsi.readPercentage()>0)?tsi.readPercentage():tp;
+          lcd.locate(13,1);
+          lcd.printf("x:%0.2f", ejex);
+          lcd.locate(13,2);
+          lcd.printf("y:%0.2f", ejey);
+          lcd.locate(12,3);
+          lcd.printf("tsi:%0.2f", pipo);
+          lcd.locate(0,3);
+          lcd.printf("luz: %0.2f", luz);
+/////////////////////////////////////////////////////////////////////////////////////////
+    if(pipo>=0.6){motor.step(numstep/46,0,step_speed2);}
+    //if(pipo<=0.4){motor.step(numstep/2,1,step_speed);}
+    if(ejex>=0.5){motor.step(numstep/46,1,step_speed2);}
+    if(ejex<=-0.5){motor.step(numstep/46,0,step_speed2);}
+    
+    if(ejey>=0.5){step_speed2=step_speed2 + 100;}
+    if(ejey<=-0.5){step_speed2=step_speed2 - 100;}
+//////////////////////////////////////////////////////////////////////////////////////// 
+                            lcd.locate(0,1);
+                            lcd.printf("Speed:%d", step_speed);
+                            lcd.locate(0,2);
+                            lcd.printf("Speed2:%d", step_speed2);
+                    if (!boton){motor.step(numstep/2,0,step_speed);
+                             //wait (0.2);
+                             lcd.locate(10,3);
+                             lcd.printf("<");//alt 16
+                            }
+                    if (!boton2){motor.step(numstep/2,1,step_speed);
+                             lcd.locate(10,3);
+                             lcd.printf(">");//alt 17
+                             }
+                    if(light<0.3){motor.step(numstep/2,0,step_speed);
+                    //wait (0.5);
+                            step_speed=step_speed - 100; //1370 hace un ruidito como campanita
+                             }
+                    else if (light>0.3){
+                                     //lcd.locate(0,3);
+                                    //lcd.printf("poca luz");
+                                    }
+                    
+                             //wait (0.2);
+}
 }
\ No newline at end of file