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

main.cpp

Committer:
facuclop
Date:
2014-08-05
Revision:
1:e462e11e7ac5
Parent:
0:6999a083fb46

File content as of revision 1:e462e11e7ac5:

/*
############################################
##           sMotor v0.1 Test Program     ##
##                by Facuclop             ##
############################################
*/
#include "mbed.h"
#include "TextLCD.h"
#include "sMotor.h"
#include "TSISensor.h"
#include "MMA8451Q.h"
#include "rtos.h"

#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()
 {
float tp=0;
float ejex;
float ejey;
float pipo;
float luz;
luz=light;
////////////////////////////////////////
lcd.locate(0,0);
lcd.printf("__Facuclop Program__");
////////////////////////////////////////
    
////************************************************************************************

/*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);
}
}