// n3dohmslimiteatualizaquedeuerrado.c - Criado pelo studio
// UNO 2.3
// 4 July 2016 11:38:19 am
#include <uno.h>
// Defines
#define VariaGarra 5
#define tempo 10
// Declaracao das Variaveis
unsigned char AceX;
unsigned char AceY;
unsigned char AceZ;
unsigned char BtC;
unsigned char BtZ;
unsigned char Garra;
unsigned int Graus;
unsigned char Ir1Limite;
unsigned char Ir5Limite;
unsigned char JoyX;
unsigned char JoyY;
int Motor_D;
int Motor_E;
unsigned int Pulsos;
int eixo_x;
int eixo_y;
// Prototipos
void AjusteLimites(void);
void ControleJoy(void);
void Freio(void);
void GiraEsquerda(void);
void ZeraEncoders(void);
void ControleMotores(void);
void GiraDireita(void);
void LeRF(void);
void ControleRF(void);
//Funcoes do Usuario
void AjusteLimites(void)
{
if ((Motor_E < -1023)) {
Motor_E = -1023;
}
if ((Motor_E > 1023)) {
Motor_E = 1023;
}
if ((Motor_D < -1023)) {
Motor_D = -1023;
}
if ((Motor_D > 1023)) {
Motor_D = 1023;
}
}
void ControleJoy(void)
{
if ((JoyX < 126)) {
Graus = 5;
GiraEsquerda();
}
if ((JoyX > 130)) {
Graus = 5;
GiraDireita();
}
if ((JoyY < 126)) {
__motor_curso(__BACKWARD);
}
if ((JoyY > 130)) {
if (((__SeguidorGetAnalog(1, 2) > 20) || (__SeguidorGetAnalog(1, 4) > 20))) {
__motor_curso(__FORWARD);
}
else {
Freio();
}
}
if (((JoyX == 128) && (JoyY == 128))) {
__motor_curso(__STOP);
}
if ((BtC == 1)) {
__PWM1 = 180;
}
if ((BtZ == 1)) {
__PWM1 = 40;
}
}
void Freio(void)
{
__motor_curso(__REVERSE);
__delay(30);
__motor_curso(__STOP);
}
void GiraEsquerda(void)
{
ZeraEncoders();
Pulsos = (Graus / 4.9);
__motor_curso(__LEFT_ROTATE);
while((__EncoderGetValue(2) < Pulsos)) {
while(!(__EncoderGetValue(2) >= Pulsos)) { __delay(10); }
__buzzer(65, 10);
__lcd_goto(0xbf, 1);
__lcd_puts("%d", __EncoderGetValue(2));
__delay(tempo);
}
}
void ZeraEncoders(void)
{
__SmartSensorCmd(1, 1, 9, 0);
__delay(tempo);
__SmartSensorCmd(1, 2, 9, 0);
__delay(tempo);
}
void ControleMotores(void)
{
__motor(1, Motor_E);
__motor(2, Motor_D);
if (((BtC == 1) && (Garra < 180))) {
Garra += VariaGarra;
}
if (((BtZ == 1) && (Garra > 0))) {
Garra -= VariaGarra;
}
__PWM1 = Garra;
}
void GiraDireita(void)
{
ZeraEncoders();
Pulsos = (Graus / 4.9);
__motor_curso(__RIGHT_ROTATE);
while((__EncoderGetValue(1) < Pulsos)) {
while(!(__EncoderGetValue(1) >= Pulsos)) { __delay(10); }
__buzzer(60, 10);
__lcd_goto(0xbf, 9);
__lcd_puts("%d", __EncoderGetValue(2));
__delay(tempo);
}
}
void LeRF(void)
{
JoyX = __ControllerGetJoystick_X(1);
__delay(tempo);
JoyY = __ControllerGetJoystick_Y(1);
__delay(tempo);
AceX = __ControllerGetAccelerometer_X(1);
__delay(tempo);
AceY = __ControllerGetAccelerometer_Y(1);
__delay(tempo);
AceZ = __ControllerGetAccelerometer_Z(1);
__delay(tempo);
BtC = __ControllerGetButton_C(1);
__delay(tempo);
BtZ = __ControllerGetButton_Z(1);
__delay(tempo);
}
void ControleRF(void)
{
eixo_x = ((JoyX - 128) * 8);
eixo_y = ((JoyY - 128) * 8);
Motor_E = (eixo_x + eixo_y);
Motor_D = (eixo_y - eixo_x);
AjusteLimites();
ControleMotores();
}
//Funcao Principal
void main(void)
{
__inicializa;
__backlight(__ON);
__SmartSensorCmd(1, 1, 8, 0);
bitset(__TRIS_PWM, __DIGITAL1);
__PWM1 = 180;
Garra = 180;
Graus = 90;
Ir1Limite = (__SeguidorGetAnalog(1, 1) - 10);
Ir5Limite = (__SeguidorGetAnalog(1, 5) - 10);
while(TRUE) {
__motor_curso(__FORWARD);
if ((__SeguidorGetAnalog(1, 1) < Ir1Limite)) {
__motor_curso(__BACKWARD);
__delay(400);
GiraDireita();
}
if ((__SeguidorGetAnalog(1, 5) < Ir5Limite)) {
__motor_curso(__BACKWARD);
__delay(400);
}
__delay(400);
GiraEsquerda();
__delay(400);
Freio();
}
while(TRUE) { }
}